@microsoft/live-share-media 1.0.0-preview.8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/LICENSE +64 -61
  2. package/README.md +11 -15
  3. package/bin/IMediaPlayer.d.ts +20 -20
  4. package/bin/IMediaPlayer.js +6 -6
  5. package/bin/LiveMediaSession.d.ts +90 -94
  6. package/bin/LiveMediaSession.d.ts.map +1 -1
  7. package/bin/LiveMediaSession.js +228 -231
  8. package/bin/LiveMediaSession.js.map +1 -1
  9. package/bin/LiveMediaSessionCoordinator.d.ts +238 -227
  10. package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
  11. package/bin/LiveMediaSessionCoordinator.js +476 -407
  12. package/bin/LiveMediaSessionCoordinator.js.map +1 -1
  13. package/bin/LiveMediaSessionCoordinatorSuspension.d.ts +19 -19
  14. package/bin/LiveMediaSessionCoordinatorSuspension.js +27 -27
  15. package/bin/MediaPlayerSynchronizer.d.ts +173 -163
  16. package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
  17. package/bin/MediaPlayerSynchronizer.js +491 -468
  18. package/bin/MediaPlayerSynchronizer.js.map +1 -1
  19. package/bin/MediaSessionActionThrottler.d.ts +11 -11
  20. package/bin/MediaSessionActionThrottler.js +13 -13
  21. package/bin/MediaSessionExtensions.d.ts +34 -34
  22. package/bin/MediaSessionExtensions.d.ts.map +1 -1
  23. package/bin/MediaSessionExtensions.js +12 -12
  24. package/bin/RepeatedActionThrottler.d.ts +19 -19
  25. package/bin/RepeatedActionThrottler.js +62 -62
  26. package/bin/RepeatedActionThrottler.js.map +1 -1
  27. package/bin/VolumeManager.d.ts +83 -83
  28. package/bin/VolumeManager.js +174 -174
  29. package/bin/index.d.ts +13 -13
  30. package/bin/index.js +29 -29
  31. package/bin/internals/GroupCoordinatorState.d.ts +93 -92
  32. package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
  33. package/bin/internals/GroupCoordinatorState.js +407 -404
  34. package/bin/internals/GroupCoordinatorState.js.map +1 -1
  35. package/bin/internals/GroupPlaybackPosition.d.ts +67 -66
  36. package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
  37. package/bin/internals/GroupPlaybackPosition.js +166 -166
  38. package/bin/internals/GroupPlaybackPosition.js.map +1 -1
  39. package/bin/internals/GroupPlaybackTrack.d.ts +45 -45
  40. package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
  41. package/bin/internals/GroupPlaybackTrack.js +121 -121
  42. package/bin/internals/GroupPlaybackTrackData.d.ts +39 -39
  43. package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
  44. package/bin/internals/GroupPlaybackTrackData.js +69 -69
  45. package/bin/internals/GroupTransportState.d.ts +49 -48
  46. package/bin/internals/GroupTransportState.d.ts.map +1 -1
  47. package/bin/internals/GroupTransportState.js +111 -111
  48. package/bin/internals/GroupTransportState.js.map +1 -1
  49. package/bin/internals/consts.d.ts +75 -72
  50. package/bin/internals/consts.d.ts.map +1 -1
  51. package/bin/internals/consts.js +84 -81
  52. package/bin/internals/consts.js.map +1 -1
  53. package/bin/internals/index.d.ts +10 -10
  54. package/bin/internals/index.js +26 -26
  55. package/package.json +9 -7
  56. package/src/LiveMediaSession.ts +24 -20
  57. package/src/LiveMediaSessionCoordinator.ts +101 -29
  58. package/src/MediaPlayerSynchronizer.ts +30 -14
  59. package/src/internals/GroupCoordinatorState.ts +49 -34
  60. package/src/internals/GroupPlaybackPosition.ts +7 -3
  61. package/src/internals/GroupPlaybackTrack.ts +2 -2
  62. package/src/internals/GroupPlaybackTrackData.ts +2 -2
  63. package/src/internals/GroupTransportState.ts +11 -4
  64. package/src/internals/consts.ts +4 -0
  65. package/typedoc.json +10 -0
  66. package/bin/EphemeralMediaSession.d.ts +0 -102
  67. package/bin/EphemeralMediaSession.d.ts.map +0 -1
  68. package/bin/EphemeralMediaSession.js +0 -226
  69. package/bin/EphemeralMediaSession.js.map +0 -1
  70. package/bin/EphemeralMediaSessionCoordinator.d.ts +0 -228
  71. package/bin/EphemeralMediaSessionCoordinator.d.ts.map +0 -1
  72. package/bin/EphemeralMediaSessionCoordinator.js +0 -396
  73. package/bin/EphemeralMediaSessionCoordinator.js.map +0 -1
  74. package/bin/EphemeralMediaSessionCoordinatorSuspension.d.ts +0 -20
  75. package/bin/EphemeralMediaSessionCoordinatorSuspension.d.ts.map +0 -1
  76. package/bin/EphemeralMediaSessionCoordinatorSuspension.js +0 -28
  77. package/bin/EphemeralMediaSessionCoordinatorSuspension.js.map +0 -1
  78. package/bin/VolumeLimiter.d.ts +0 -70
  79. package/bin/VolumeLimiter.d.ts.map +0 -1
  80. package/bin/VolumeLimiter.js +0 -170
  81. package/bin/VolumeLimiter.js.map +0 -1
  82. package/src/test/GroupPlaybackPosition.spec.ts +0 -600
  83. package/src/test/GroupPlaybackTrack.spec.ts +0 -122
  84. package/src/test/SharedMediaSession.spec.ts +0 -149
  85. package/src/test/SharedMediaSessionCoordinator.spec.ts +0 -271
  86. package/src/test/VolumeManager.spec.ts +0 -219
@@ -1,469 +1,492 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.MediaPlayerSynchronizer = exports.MediaPlayerSynchronizerEvents = void 0;
16
- const events_1 = __importDefault(require("events"));
17
- const MediaSessionExtensions_1 = require("./MediaSessionExtensions");
18
- const VolumeManager_1 = require("./VolumeManager");
19
- const internals_1 = require("./internals");
20
- /**
21
- * Events supported by `MediaPlayerSynchronizer` object.
22
- */
23
- var MediaPlayerSynchronizerEvents;
24
- (function (MediaPlayerSynchronizerEvents) {
25
- /**
26
- *
27
- */
28
- MediaPlayerSynchronizerEvents["coordinatorstatechange"] = "coordinatorstatechange";
29
- MediaPlayerSynchronizerEvents["groupaction"] = "groupaction";
30
- MediaPlayerSynchronizerEvents["useraction"] = "useraction";
31
- })(MediaPlayerSynchronizerEvents = exports.MediaPlayerSynchronizerEvents || (exports.MediaPlayerSynchronizerEvents = {}));
32
- /**
33
- * Synchronizes a local HTML Media Element with a group of remote HTML Media Elements.
34
- *
35
- * @remarks
36
- * All of an apps transport control commands should be routed through the synchronizer. If the
37
- * app is not currently joined to the group media session, the commands will be applied directly
38
- * to the local player. When the group session is joined the commands will be broadcast to the
39
- * group in addition to being applied to the local player.
40
- */
41
- class MediaPlayerSynchronizer extends events_1.default {
42
- /**
43
- * Creates a new `MediaElementSynchronizer` instance.
44
- * @param player Media player element. This can be an HTML Media Element or any player that looks like an HTML Media Element.
45
- * @param mediaSession Group MediaSession object being used.
46
- * @param onEnd Optional. Function to call when synchronizers `end()` method is called.
47
- */
48
- constructor(player, mediaSession, onEnd) {
49
- super();
50
- this._viewOnly = false;
51
- this._expectedPlaybackState = "none";
52
- this._trackData = null;
53
- this._player = player;
54
- this._mediaSession = mediaSession;
55
- this._logger = mediaSession.logger;
56
- this._volumeManager = new VolumeManager_1.VolumeManager(player);
57
- this._onEnd = onEnd;
58
- // Listen for player state requests
59
- mediaSession.setRequestPlayerStateHandler(() => {
60
- // Find playback state
61
- let playbackState;
62
- const src = this._player.currentSrc || this._player.src;
63
- if (this._player.ended) {
64
- playbackState = "ended";
65
- }
66
- else if (!src ||
67
- (this._player.paused && this._player.currentTime < 1.0)) {
68
- playbackState = "none";
69
- }
70
- else if (this._player.paused) {
71
- playbackState = "paused";
72
- }
73
- else {
74
- playbackState = "playing";
75
- }
76
- const state = {
77
- metadata: { trackIdentifier: src },
78
- playbackState: playbackState,
79
- positionState: {
80
- position: this._player.currentTime,
81
- playbackRate: this._player.playbackRate,
82
- duration: this._player.duration,
83
- },
84
- trackData: this._trackData,
85
- };
86
- return state;
87
- });
88
- // Listen for player events
89
- this._onPlayerEvent = (evt) => {
90
- if (evt.type != "timeupdate") {
91
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PlayerEvent, null, {
92
- type: evt.type,
93
- });
94
- }
95
- switch (evt.type) {
96
- case "loadedmetadata":
97
- // eslint-disable-next-line no-case-declarations
98
- const src = this._player.currentSrc || this._player.src;
99
- this._trackData = null;
100
- break;
101
- case "playing":
102
- // Handle case for YouTube player where user can pause/play video by clicking on it.
103
- // - Videos don't always start at 0.0 seconds.
104
- if (this._expectedPlaybackState != "playing") {
105
- if (this._mediaSession.coordinator.canPlayPause &&
106
- this._player.currentTime < 1.0) {
107
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
108
- .UserTappedVideoToPlay);
109
- this.play();
110
- }
111
- }
112
- // block play if player state is playing when expected synced state is paused and coordinator is not suspended.
113
- // needed because cannot tell if its a user initiated event, so disallow play
114
- if (this._expectedPlaybackState === "paused" &&
115
- !this._mediaSession.coordinator.isSuspended) {
116
- this._player.pause();
117
- }
118
- // block play if player state is playing when expected synced state is none and coordinator is not suspended.
119
- // needed because user who is not in control should not be able to start, so disallow play
120
- if (this._expectedPlaybackState === "none" &&
121
- !this._mediaSession.coordinator.isSuspended) {
122
- this._player.pause();
123
- }
124
- break;
125
- case "pause":
126
- // block pause if player state is paused when expected synced state is playing and coordinator is not suspended.
127
- // needed because cannot tell if its a user initiated event, so disallow pause
128
- if (this._expectedPlaybackState === "playing" &&
129
- !this._mediaSession.coordinator.isSuspended) {
130
- this._player.play();
131
- }
132
- break;
133
- case "ratechange":
134
- // Block rate changes unless suspended.
135
- if (this._player.playbackRate != 1.0 &&
136
- !this._mediaSession.coordinator.isSuspended) {
137
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
138
- .PlaybackRateChangeBlocked);
139
- this._player.playbackRate = 1.0;
140
- }
141
- break;
142
- case "blocked":
143
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
144
- .PlaybackRateChangeBlocked);
145
- break;
146
- }
147
- };
148
- // Register for coordinator state changes
149
- this._mediaSession.coordinator.on(MediaSessionExtensions_1.MediaSessionCoordinatorEvents.coordinatorstatechange, (evt) => this.emit(evt.type, evt));
150
- // Register media session actions
151
- for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
152
- this._mediaSession.setActionHandler(action, (details) => __awaiter(this, void 0, void 0, function* () {
153
- var _a;
154
- let error;
155
- try {
156
- switch (details.action) {
157
- case "play":
158
- this._expectedPlaybackState = "playing";
159
- if (this._player.paused) {
160
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
161
- .PlayAction);
162
- if (typeof details.seekTime == "number" &&
163
- this._player.currentTime < 1.0) {
164
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents
165
- .MediaPlayerSynchronizer
166
- .SeekingPlayerToStartPosition, null, { position: details.seekTime });
167
- this._player.currentTime =
168
- details.seekTime;
169
- }
170
- // Reference: https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/play#exceptions
171
- yield this._player.play();
172
- }
173
- break;
174
- case "pause":
175
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
176
- .PauseAction);
177
- this._expectedPlaybackState = "paused";
178
- if (typeof details.seekTime == "number" &&
179
- this._player.currentTime < 1.0) {
180
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
181
- .SeekingPlayerToStartPosition, null, { position: details.seekTime });
182
- this._player.currentTime =
183
- details.seekTime;
184
- }
185
- this._player.pause();
186
- break;
187
- case "seekto":
188
- if (typeof details.seekTime == "number") {
189
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
190
- .SeekToAction, null, { position: details.seekTime });
191
- this._player.currentTime =
192
- details.seekTime;
193
- }
194
- break;
195
- case "settrack":
196
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
197
- .SetTrackAction);
198
- this._expectedPlaybackState = "none";
199
- this._player.src =
200
- details.metadata.trackIdentifier;
201
- this._player.load();
202
- break;
203
- case "datachange":
204
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
205
- .DataChangeAction);
206
- break;
207
- case "catchup":
208
- if (typeof details.seekTime == "number") {
209
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
210
- .CatchupAction, null, { position: details.seekTime });
211
- this.catchupPlayer(details.seekTime);
212
- }
213
- break;
214
- }
215
- }
216
- catch (err) {
217
- if (err instanceof Error) {
218
- error = err;
219
- }
220
- else {
221
- error = new Error((_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : "An unknown error occurred after processing the group session action.");
222
- }
223
- }
224
- this.dispatchGroupAction(details, false, error);
225
- }));
226
- }
227
- // Subscribe to player events
228
- for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
229
- this._player.addEventListener(event, this._onPlayerEvent);
230
- }
231
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SynchronizationStarted);
232
- }
233
- /**
234
- * Media player being synchronized.
235
- */
236
- get player() {
237
- return this._player;
238
- }
239
- /**
240
- * Synchronizers media session.
241
- */
242
- get mediaSession() {
243
- return this._mediaSession;
244
- }
245
- /**
246
- * If true the client is in a view only mode.
247
- *
248
- * @remarks
249
- * Toggling this value to true results in `mediaSession.coordinator.canPlayPause`,
250
- * `mediaSession.coordinator.canSeek`, `mediaSession.coordinator.canSetTrack`, and
251
- * `mediaSession.coordinator.canSetTrackData` all being set to false. For more fine
252
- * grained control over the local clients policies, call the `mediaSession.coordinator`
253
- * directly.
254
- */
255
- get viewOnly() {
256
- return this._viewOnly;
257
- }
258
- set viewOnly(value) {
259
- this._viewOnly = value;
260
- this.mediaSession.coordinator.canPlayPause = !value;
261
- this.mediaSession.coordinator.canSeek = !value;
262
- this.mediaSession.coordinator.canSetTrack = !value;
263
- this.mediaSession.coordinator.canSetTrackData = !value;
264
- }
265
- /**
266
- * Volume limiter used to temporarily reduce the videos volume when someone speaks in a meeting.
267
- */
268
- get volumeManager() {
269
- return this._volumeManager;
270
- }
271
- /**
272
- * Registers a new event listener.
273
- * @param event Name of the event to add.
274
- * @param listener Function to call when the event is triggered.
275
- */
276
- addEventListener(event, listener) {
277
- this.on(event, listener);
278
- return this;
279
- }
280
- /**
281
- * Un-registers an existing event listener.
282
- * @param event Name of the event to remove.
283
- * @param listener Function that was registered in call to `addEventListener()`.
284
- */
285
- removeEventListener(event, listener) {
286
- this.off(event, listener);
287
- return this;
288
- }
289
- /**
290
- * Ends synchronization of the current media player.
291
- */
292
- end() {
293
- if (this._onEnd) {
294
- try {
295
- // Clear media session actions
296
- for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
297
- this._mediaSession.setActionHandler(action, null);
298
- }
299
- // Subscribe to player events
300
- for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
301
- this._player.removeEventListener(event, this._onPlayerEvent);
302
- }
303
- // Notify parent
304
- this._onEnd();
305
- }
306
- finally {
307
- this._onEnd = undefined;
308
- }
309
- }
310
- }
311
- /**
312
- * Begin a local seek operation.
313
- *
314
- * @remarks
315
- * UI can call this when a user grabs a timeline scrubber and starts scrubbing the video to a
316
- * new playback position. The synchronizer will being a new suspension which temporarily
317
- * disconnects the client for the rest of the group for synchronization purposes. Calling
318
- * `endSeek()` will end the suspension and seek the group to the users final seek position.
319
- */
320
- beginSeek() {
321
- if (this._seekSuspension) {
322
- throw new Error(`MediaPlayerSynchronizer: cannot begin seek. A seek is already in progress.`);
323
- }
324
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.BeginSeekCalled);
325
- this._seekSuspension = this._mediaSession.coordinator.beginSuspension();
326
- }
327
- /**
328
- * Ends a seek operation that was started by calling `beginSeek()`.
329
- * @param seekTo Playback position in seconds to seek the group to.
330
- */
331
- endSeek(seekTo) {
332
- if (!this._seekSuspension) {
333
- throw new Error(`MediaPlayerSynchronizer: cannot end seek. No seek is in progress.`);
334
- }
335
- // Seek player to new time immediately.
336
- // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
337
- // location.
338
- this._player.currentTime = seekTo;
339
- const suspension = this._seekSuspension;
340
- this._seekSuspension = undefined;
341
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.EndSeekCalled, null, {
342
- position: seekTo,
343
- });
344
- suspension.end(seekTo);
345
- this.dispatchUserAction({ action: "seekto", seekTime: seekTo });
346
- }
347
- /**
348
- * Tells the group to begin playing the current video.
349
- *
350
- * @remarks
351
- * For proper operation apps should avoid calling `mediaSession.coordinator.play()` directly
352
- * and instead use the synchronizers `play()` method.
353
- */
354
- play() {
355
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PlayCalled);
356
- this._expectedPlaybackState = "playing";
357
- this._mediaSession.coordinator.play();
358
- this.dispatchUserAction({ action: "play" });
359
- }
360
- /**
361
- * Tells the group to pause the current video.
362
- *
363
- * @remarks
364
- * For proper operation apps should avoid calling `mediaSession.coordinator.pause()` directly
365
- * and instead use the synchronizers `pause()` method.
366
- */
367
- pause() {
368
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PauseCalled);
369
- this._expectedPlaybackState = "paused";
370
- this._mediaSession.coordinator.pause();
371
- this.dispatchUserAction({ action: "pause" });
372
- }
373
- /**
374
- * Tells the group to seek the current video to a new playback position.
375
- *
376
- * @remarks
377
- * For proper operation apps should avoid calling `mediaSession.coordinator.seekTo()` directly
378
- * and instead use the synchronizers `seekTo()` method.
379
- */
380
- seekTo(time) {
381
- // Always seek player to new time.
382
- // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
383
- // location.
384
- this._player.currentTime = time;
385
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SeekToCalled, null, { position: time });
386
- this._mediaSession.coordinator.seekTo(time);
387
- this.dispatchUserAction({ action: "seekto", seekTime: time });
388
- }
389
- /**
390
- * Tells the group to change to a new track.
391
- *
392
- * @remarks
393
- * For proper operation apps should avoid calling `mediaSession.coordinator.setTrack()` directly
394
- * and instead use the synchronizers `setTrack()` method.
395
- */
396
- setTrack(track, waitPoints) {
397
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SetTrackCalled);
398
- this._mediaSession.coordinator.setTrack(track, waitPoints);
399
- this.dispatchUserAction({ action: "settrack", metadata: track });
400
- }
401
- /**
402
- * Updates the current tracks data object.
403
- *
404
- * @remarks
405
- * For proper operation apps should avoid calling `mediaSession.coordinator.setTrackData()` directly
406
- * and instead use the synchronizers `setTrackData()` method.
407
- */
408
- setTrackData(data) {
409
- this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SetTrackDataCalled);
410
- this._trackData = data;
411
- this._mediaSession.coordinator.setTrackData(data);
412
- this.dispatchUserAction({ action: "datachange", data: data });
413
- }
414
- dispatchGroupAction(details, delay = false, error) {
415
- if (delay) {
416
- setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.groupaction, {
417
- type: MediaPlayerSynchronizerEvents.groupaction,
418
- details: details,
419
- playerError: error,
420
- }), 50);
421
- }
422
- else {
423
- this.emit(MediaPlayerSynchronizerEvents.groupaction, {
424
- type: MediaPlayerSynchronizerEvents.groupaction,
425
- details: details,
426
- error,
427
- });
428
- }
429
- }
430
- dispatchUserAction(details, delay = false) {
431
- if (delay) {
432
- setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.useraction, {
433
- type: MediaPlayerSynchronizerEvents.useraction,
434
- details: details,
435
- }), 50);
436
- }
437
- else {
438
- this.emit(MediaPlayerSynchronizerEvents.useraction, {
439
- type: MediaPlayerSynchronizerEvents.useraction,
440
- details: details,
441
- });
442
- }
443
- }
444
- catchupPlayer(time) {
445
- return __awaiter(this, void 0, void 0, function* () {
446
- this._player.currentTime = time;
447
- });
448
- }
449
- }
450
- exports.MediaPlayerSynchronizer = MediaPlayerSynchronizer;
451
- MediaPlayerSynchronizer.SESSION_ACTIONS = [
452
- "play",
453
- "pause",
454
- "seekto",
455
- "settrack",
456
- "datachange",
457
- "catchup",
458
- "wait",
459
- ];
460
- MediaPlayerSynchronizer.PLAYER_EVENTS = [
461
- "playing",
462
- "pause",
463
- "ratechange",
464
- "timeupdate",
465
- "ended",
466
- "loadedmetadata",
467
- "blocked",
468
- ];
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MediaPlayerSynchronizer = exports.MediaPlayerSynchronizerEvents = void 0;
16
+ const events_1 = __importDefault(require("events"));
17
+ const MediaSessionExtensions_1 = require("./MediaSessionExtensions");
18
+ const VolumeManager_1 = require("./VolumeManager");
19
+ const internals_1 = require("./internals");
20
+ /**
21
+ * Events supported by `MediaPlayerSynchronizer` object.
22
+ */
23
+ var MediaPlayerSynchronizerEvents;
24
+ (function (MediaPlayerSynchronizerEvents) {
25
+ /**
26
+ *
27
+ */
28
+ MediaPlayerSynchronizerEvents["coordinatorstatechange"] = "coordinatorstatechange";
29
+ MediaPlayerSynchronizerEvents["groupaction"] = "groupaction";
30
+ MediaPlayerSynchronizerEvents["useraction"] = "useraction";
31
+ })(MediaPlayerSynchronizerEvents = exports.MediaPlayerSynchronizerEvents || (exports.MediaPlayerSynchronizerEvents = {}));
32
+ /**
33
+ * Synchronizes a local HTML Media Element with a group of remote HTML Media Elements.
34
+ *
35
+ * @remarks
36
+ * All of an apps transport control commands should be routed through the synchronizer. If the
37
+ * app is not currently joined to the group media session, the commands will be applied directly
38
+ * to the local player. When the group session is joined the commands will be broadcast to the
39
+ * group in addition to being applied to the local player.
40
+ */
41
+ class MediaPlayerSynchronizer extends events_1.default {
42
+ /**
43
+ * Creates a new `MediaElementSynchronizer` instance.
44
+ * @param player Media player element. This can be an HTML Media Element or any player that looks like an HTML Media Element.
45
+ * @param mediaSession Group MediaSession object being used.
46
+ * @param onEnd Optional. Function to call when synchronizers `end()` method is called.
47
+ */
48
+ constructor(player, mediaSession, onEnd) {
49
+ super();
50
+ this._viewOnly = false;
51
+ this._expectedPlaybackState = "none";
52
+ this._trackData = null;
53
+ this._player = player;
54
+ this._mediaSession = mediaSession;
55
+ this._logger = mediaSession.logger;
56
+ this._volumeManager = new VolumeManager_1.VolumeManager(player);
57
+ this._onEnd = onEnd;
58
+ // Listen for player state requests
59
+ mediaSession.setRequestPlayerStateHandler(() => {
60
+ // Find playback state
61
+ let playbackState;
62
+ const src = this._player.currentSrc || this._player.src;
63
+ if (this._player.ended) {
64
+ playbackState = "ended";
65
+ }
66
+ else if (!src ||
67
+ (this._player.paused && this._player.currentTime < 1.0)) {
68
+ playbackState = "none";
69
+ }
70
+ else if (this._player.paused) {
71
+ playbackState = "paused";
72
+ }
73
+ else {
74
+ playbackState = "playing";
75
+ }
76
+ const state = {
77
+ metadata: { trackIdentifier: src },
78
+ playbackState: playbackState,
79
+ positionState: {
80
+ position: this._player.currentTime,
81
+ playbackRate: this._player.playbackRate,
82
+ duration: this._player.duration,
83
+ },
84
+ trackData: this._trackData,
85
+ };
86
+ return state;
87
+ });
88
+ // Listen for player events
89
+ this._onPlayerEvent = (evt) => {
90
+ if (evt.type != "timeupdate") {
91
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PlayerEvent, null, {
92
+ type: evt.type,
93
+ });
94
+ }
95
+ switch (evt.type) {
96
+ case "loadedmetadata":
97
+ // eslint-disable-next-line no-case-declarations
98
+ const src = this._player.currentSrc || this._player.src;
99
+ this._trackData = null;
100
+ break;
101
+ case "playing":
102
+ // Handle case for YouTube player where user can pause/play video by clicking on it.
103
+ // - Videos don't always start at 0.0 seconds.
104
+ if (this._expectedPlaybackState != "playing") {
105
+ if (this._mediaSession.coordinator.canPlayPause &&
106
+ this._player.currentTime < 1.0) {
107
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
108
+ .UserTappedVideoToPlay);
109
+ this.play().catch((err) => {
110
+ this._logger.sendErrorEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
111
+ .UserTappedVideoToPlayError, err);
112
+ });
113
+ }
114
+ }
115
+ // block play if player state is playing when expected synced state is paused and coordinator is not suspended.
116
+ // needed because cannot tell if its a user initiated event, so disallow play
117
+ if (this._expectedPlaybackState === "paused" &&
118
+ !this._mediaSession.coordinator.isSuspended) {
119
+ this._player.pause();
120
+ }
121
+ // block play if player state is playing when expected synced state is none and coordinator is not suspended.
122
+ // needed because user who is not in control should not be able to start, so disallow play
123
+ if (this._expectedPlaybackState === "none" &&
124
+ !this._mediaSession.coordinator.isSuspended) {
125
+ this._player.pause();
126
+ }
127
+ break;
128
+ case "pause":
129
+ // block pause if player state is paused when expected synced state is playing and coordinator is not suspended.
130
+ // needed because cannot tell if its a user initiated event, so disallow pause
131
+ if (this._expectedPlaybackState === "playing" &&
132
+ !this._mediaSession.coordinator.isSuspended) {
133
+ this._player.play();
134
+ }
135
+ break;
136
+ case "ratechange":
137
+ // Block rate changes unless suspended.
138
+ if (this._player.playbackRate != 1.0 &&
139
+ !this._mediaSession.coordinator.isSuspended) {
140
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
141
+ .PlaybackRateChangeBlocked);
142
+ this._player.playbackRate = 1.0;
143
+ }
144
+ break;
145
+ case "blocked":
146
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
147
+ .PlaybackRateChangeBlocked);
148
+ break;
149
+ }
150
+ };
151
+ // Register for coordinator state changes
152
+ this._mediaSession.coordinator.on(MediaSessionExtensions_1.MediaSessionCoordinatorEvents.coordinatorstatechange, (evt) => this.emit(evt.type, evt));
153
+ // Register media session actions
154
+ for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
155
+ this._mediaSession.setActionHandler(action, (details) => __awaiter(this, void 0, void 0, function* () {
156
+ var _a;
157
+ let error;
158
+ try {
159
+ switch (details.action) {
160
+ case "play":
161
+ this._expectedPlaybackState = "playing";
162
+ if (this._player.paused) {
163
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
164
+ .PlayAction);
165
+ if (typeof details.seekTime == "number" &&
166
+ this._player.currentTime < 1.0) {
167
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents
168
+ .MediaPlayerSynchronizer
169
+ .SeekingPlayerToStartPosition, null, { position: details.seekTime });
170
+ this._player.currentTime =
171
+ details.seekTime;
172
+ }
173
+ // Reference: https://developer.mozilla.org/docs/Web/API/HTMLMediaElement/play#exceptions
174
+ yield this._player.play();
175
+ }
176
+ break;
177
+ case "pause":
178
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
179
+ .PauseAction);
180
+ this._expectedPlaybackState = "paused";
181
+ if (typeof details.seekTime == "number" &&
182
+ this._player.currentTime < 1.0) {
183
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
184
+ .SeekingPlayerToStartPosition, null, { position: details.seekTime });
185
+ this._player.currentTime =
186
+ details.seekTime;
187
+ }
188
+ this._player.pause();
189
+ break;
190
+ case "seekto":
191
+ if (typeof details.seekTime == "number") {
192
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
193
+ .SeekToAction, null, { position: details.seekTime });
194
+ this._player.currentTime =
195
+ details.seekTime;
196
+ }
197
+ break;
198
+ case "settrack":
199
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
200
+ .SetTrackAction);
201
+ this._expectedPlaybackState = "none";
202
+ this._player.src =
203
+ details.metadata.trackIdentifier;
204
+ this._player.load();
205
+ break;
206
+ case "datachange":
207
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
208
+ .DataChangeAction);
209
+ break;
210
+ case "catchup":
211
+ if (typeof details.seekTime == "number") {
212
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer
213
+ .CatchupAction, null, { position: details.seekTime });
214
+ this.catchupPlayer(details.seekTime);
215
+ }
216
+ break;
217
+ }
218
+ }
219
+ catch (err) {
220
+ if (err instanceof Error) {
221
+ error = err;
222
+ }
223
+ else {
224
+ error = new Error((_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : "An unknown error occurred after processing the group session action.");
225
+ }
226
+ }
227
+ this.dispatchGroupAction(details, false, error);
228
+ }));
229
+ }
230
+ // Subscribe to player events
231
+ for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
232
+ this._player.addEventListener(event, this._onPlayerEvent);
233
+ }
234
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SynchronizationStarted);
235
+ }
236
+ /**
237
+ * Media player being synchronized.
238
+ */
239
+ get player() {
240
+ return this._player;
241
+ }
242
+ /**
243
+ * Synchronizers media session.
244
+ */
245
+ get mediaSession() {
246
+ return this._mediaSession;
247
+ }
248
+ /**
249
+ * If true the client is in a view only mode.
250
+ *
251
+ * @remarks
252
+ * Toggling this value to true results in `mediaSession.coordinator.canPlayPause`,
253
+ * `mediaSession.coordinator.canSeek`, `mediaSession.coordinator.canSetTrack`, and
254
+ * `mediaSession.coordinator.canSetTrackData` all being set to false. For more fine
255
+ * grained control over the local clients policies, call the `mediaSession.coordinator`
256
+ * directly.
257
+ */
258
+ get viewOnly() {
259
+ return this._viewOnly;
260
+ }
261
+ set viewOnly(value) {
262
+ this._viewOnly = value;
263
+ this.mediaSession.coordinator.canPlayPause = !value;
264
+ this.mediaSession.coordinator.canSeek = !value;
265
+ this.mediaSession.coordinator.canSetTrack = !value;
266
+ this.mediaSession.coordinator.canSetTrackData = !value;
267
+ }
268
+ /**
269
+ * Volume limiter used to temporarily reduce the videos volume when someone speaks in a meeting.
270
+ */
271
+ get volumeManager() {
272
+ return this._volumeManager;
273
+ }
274
+ /**
275
+ * Registers a new event listener.
276
+ * @param event Name of the event to add.
277
+ * @param listener Function to call when the event is triggered.
278
+ */
279
+ addEventListener(event, listener) {
280
+ this.on(event, listener);
281
+ return this;
282
+ }
283
+ /**
284
+ * Un-registers an existing event listener.
285
+ * @param event Name of the event to remove.
286
+ * @param listener Function that was registered in call to `addEventListener()`.
287
+ */
288
+ removeEventListener(event, listener) {
289
+ this.off(event, listener);
290
+ return this;
291
+ }
292
+ /**
293
+ * Ends synchronization of the current media player.
294
+ */
295
+ end() {
296
+ if (this._onEnd) {
297
+ try {
298
+ // Clear media session actions
299
+ for (const action of MediaPlayerSynchronizer.SESSION_ACTIONS) {
300
+ this._mediaSession.setActionHandler(action, null);
301
+ }
302
+ // Subscribe to player events
303
+ for (const event of MediaPlayerSynchronizer.PLAYER_EVENTS) {
304
+ this._player.removeEventListener(event, this._onPlayerEvent);
305
+ }
306
+ // Notify parent
307
+ this._onEnd();
308
+ }
309
+ finally {
310
+ this._onEnd = undefined;
311
+ }
312
+ }
313
+ }
314
+ /**
315
+ * Begin a local seek operation.
316
+ *
317
+ * @remarks
318
+ * UI can call this when a user grabs a timeline scrubber and starts scrubbing the video to a
319
+ * new playback position. The synchronizer will being a new suspension which temporarily
320
+ * disconnects the client for the rest of the group for synchronization purposes. Calling
321
+ * `endSeek()` will end the suspension and seek the group to the users final seek position.
322
+ */
323
+ beginSeek() {
324
+ if (this._seekSuspension) {
325
+ throw new Error(`MediaPlayerSynchronizer: cannot begin seek. A seek is already in progress.`);
326
+ }
327
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.BeginSeekCalled);
328
+ this._seekSuspension = this._mediaSession.coordinator.beginSuspension();
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
+ endSeek(seekTo) {
335
+ if (!this._seekSuspension) {
336
+ throw new Error(`MediaPlayerSynchronizer: cannot end seek. No seek is in progress.`);
337
+ }
338
+ // Seek player to new time immediately.
339
+ // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
340
+ // location.
341
+ this._player.currentTime = seekTo;
342
+ const suspension = this._seekSuspension;
343
+ this._seekSuspension = undefined;
344
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.EndSeekCalled, null, {
345
+ position: seekTo,
346
+ });
347
+ suspension.end(seekTo);
348
+ this.dispatchUserAction({ action: "seekto", seekTime: seekTo });
349
+ }
350
+ /**
351
+ * Tells the group to begin playing the current video.
352
+ *
353
+ * @remarks
354
+ * For proper operation apps should avoid calling `mediaSession.coordinator.play()` directly
355
+ * and instead use the synchronizers `play()` method.
356
+ *
357
+ * @returns a void promise that resolves once complete, throws if user does not have proper roles
358
+ */
359
+ play() {
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PlayCalled);
362
+ this._expectedPlaybackState = "playing";
363
+ yield this._mediaSession.coordinator.play();
364
+ this.dispatchUserAction({ action: "play" });
365
+ });
366
+ }
367
+ /**
368
+ * Tells the group to pause the current video.
369
+ *
370
+ * @remarks
371
+ * For proper operation apps should avoid calling `mediaSession.coordinator.pause()` directly
372
+ * and instead use the synchronizers `pause()` method.
373
+ *
374
+ * @returns a void promise that resolves once complete, throws if user does not have proper roles
375
+ */
376
+ pause() {
377
+ return __awaiter(this, void 0, void 0, function* () {
378
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.PauseCalled);
379
+ this._expectedPlaybackState = "paused";
380
+ yield this._mediaSession.coordinator.pause();
381
+ this.dispatchUserAction({ action: "pause" });
382
+ });
383
+ }
384
+ /**
385
+ * Tells the group to seek the current video to a new playback position.
386
+ *
387
+ * @remarks
388
+ * For proper operation apps should avoid calling `mediaSession.coordinator.seekTo()` directly
389
+ * and instead use the synchronizers `seekTo()` method.
390
+ *
391
+ * @returns a void promise that resolves once complete, throws if user does not have proper roles
392
+ */
393
+ seekTo(time) {
394
+ return __awaiter(this, void 0, void 0, function* () {
395
+ // Always seek player to new time.
396
+ // - This resolves an issue where the timeline scrubber can temporarily snap back to the original
397
+ // location.
398
+ this._player.currentTime = time;
399
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SeekToCalled, null, { position: time });
400
+ yield this._mediaSession.coordinator.seekTo(time);
401
+ this.dispatchUserAction({ action: "seekto", seekTime: time });
402
+ });
403
+ }
404
+ /**
405
+ * Tells the group to change to a new track.
406
+ *
407
+ * @remarks
408
+ * For proper operation apps should avoid calling `mediaSession.coordinator.setTrack()` directly
409
+ * and instead use the synchronizers `setTrack()` method.
410
+ *
411
+ * @returns a void promise that resolves once complete, throws if user does not have proper roles
412
+ */
413
+ setTrack(track, waitPoints) {
414
+ return __awaiter(this, void 0, void 0, function* () {
415
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SetTrackCalled);
416
+ yield this._mediaSession.coordinator.setTrack(track, waitPoints);
417
+ this.dispatchUserAction({ action: "settrack", metadata: track });
418
+ });
419
+ }
420
+ /**
421
+ * Updates the current tracks data object.
422
+ *
423
+ * @remarks
424
+ * For proper operation apps should avoid calling `mediaSession.coordinator.setTrackData()` directly
425
+ * and instead use the synchronizers `setTrackData()` method.
426
+ *
427
+ * @returns a void promise that resolves once complete, throws if user does not have proper roles
428
+ */
429
+ setTrackData(data) {
430
+ return __awaiter(this, void 0, void 0, function* () {
431
+ this._logger.sendTelemetryEvent(internals_1.TelemetryEvents.MediaPlayerSynchronizer.SetTrackDataCalled);
432
+ this._trackData = data;
433
+ yield this._mediaSession.coordinator.setTrackData(data);
434
+ this.dispatchUserAction({ action: "datachange", data: data });
435
+ });
436
+ }
437
+ dispatchGroupAction(details, delay = false, error) {
438
+ if (delay) {
439
+ setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.groupaction, {
440
+ type: MediaPlayerSynchronizerEvents.groupaction,
441
+ details: details,
442
+ playerError: error,
443
+ }), 50);
444
+ }
445
+ else {
446
+ this.emit(MediaPlayerSynchronizerEvents.groupaction, {
447
+ type: MediaPlayerSynchronizerEvents.groupaction,
448
+ details: details,
449
+ error,
450
+ });
451
+ }
452
+ }
453
+ dispatchUserAction(details, delay = false) {
454
+ if (delay) {
455
+ setTimeout(() => this.emit(MediaPlayerSynchronizerEvents.useraction, {
456
+ type: MediaPlayerSynchronizerEvents.useraction,
457
+ details: details,
458
+ }), 50);
459
+ }
460
+ else {
461
+ this.emit(MediaPlayerSynchronizerEvents.useraction, {
462
+ type: MediaPlayerSynchronizerEvents.useraction,
463
+ details: details,
464
+ });
465
+ }
466
+ }
467
+ catchupPlayer(time) {
468
+ return __awaiter(this, void 0, void 0, function* () {
469
+ this._player.currentTime = time;
470
+ });
471
+ }
472
+ }
473
+ MediaPlayerSynchronizer.SESSION_ACTIONS = [
474
+ "play",
475
+ "pause",
476
+ "seekto",
477
+ "settrack",
478
+ "datachange",
479
+ "catchup",
480
+ "wait",
481
+ ];
482
+ MediaPlayerSynchronizer.PLAYER_EVENTS = [
483
+ "playing",
484
+ "pause",
485
+ "ratechange",
486
+ "timeupdate",
487
+ "ended",
488
+ "loadedmetadata",
489
+ "blocked",
490
+ ];
491
+ exports.MediaPlayerSynchronizer = MediaPlayerSynchronizer;
469
492
  //# sourceMappingURL=MediaPlayerSynchronizer.js.map