@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.
- package/.nycrc.json +10 -10
- package/LICENSE +86 -86
- package/README.md +141 -137
- package/bin/IMediaPlayer.d.ts.map +1 -1
- package/bin/LiveMediaSession.d.ts +95 -0
- package/bin/LiveMediaSession.d.ts.map +1 -0
- package/bin/LiveMediaSession.js +232 -0
- package/bin/LiveMediaSession.js.map +1 -0
- package/bin/LiveMediaSessionCoordinator.d.ts +228 -0
- package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -0
- package/bin/LiveMediaSessionCoordinator.js +408 -0
- package/bin/LiveMediaSessionCoordinator.js.map +1 -0
- package/bin/LiveMediaSessionCoordinatorSuspension.d.ts +20 -0
- package/bin/LiveMediaSessionCoordinatorSuspension.d.ts.map +1 -0
- package/bin/LiveMediaSessionCoordinatorSuspension.js +28 -0
- package/bin/LiveMediaSessionCoordinatorSuspension.js.map +1 -0
- package/bin/MediaPlayerSynchronizer.d.ts +11 -11
- package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
- package/bin/MediaPlayerSynchronizer.js +136 -76
- package/bin/MediaPlayerSynchronizer.js.map +1 -1
- package/bin/MediaSessionActionThrottler.d.ts +1 -1
- package/bin/MediaSessionActionThrottler.d.ts.map +1 -1
- package/bin/MediaSessionActionThrottler.js.map +1 -1
- package/bin/MediaSessionExtensions.d.ts.map +1 -1
- package/bin/RepeatedActionThrottler.d.ts +2 -2
- package/bin/RepeatedActionThrottler.d.ts.map +1 -1
- package/bin/RepeatedActionThrottler.js +9 -3
- package/bin/RepeatedActionThrottler.js.map +1 -1
- package/bin/VolumeManager.d.ts +84 -0
- package/bin/VolumeManager.d.ts.map +1 -0
- package/bin/VolumeManager.js +175 -0
- package/bin/VolumeManager.js.map +1 -0
- package/bin/index.d.ts +9 -9
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +4 -4
- package/bin/index.js.map +1 -1
- package/bin/internals/GroupCoordinatorState.d.ts +12 -12
- package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
- package/bin/internals/GroupCoordinatorState.js +94 -56
- package/bin/internals/GroupCoordinatorState.js.map +1 -1
- package/bin/internals/GroupPlaybackPosition.d.ts +3 -3
- package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
- package/bin/internals/GroupPlaybackPosition.js +24 -15
- package/bin/internals/GroupPlaybackPosition.js.map +1 -1
- package/bin/internals/GroupPlaybackTrack.d.ts +4 -4
- package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
- package/bin/internals/GroupPlaybackTrack.js +14 -4
- package/bin/internals/GroupPlaybackTrack.js.map +1 -1
- package/bin/internals/GroupPlaybackTrackData.d.ts +3 -3
- package/bin/internals/GroupPlaybackTrackData.d.ts.map +1 -1
- package/bin/internals/GroupPlaybackTrackData.js +8 -4
- package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
- package/bin/internals/GroupTransportState.d.ts +5 -5
- package/bin/internals/GroupTransportState.d.ts.map +1 -1
- package/bin/internals/GroupTransportState.js +28 -13
- package/bin/internals/GroupTransportState.js.map +1 -1
- package/bin/internals/consts.d.ts.map +1 -1
- package/bin/internals/consts.js +54 -54
- package/bin/internals/consts.js.map +1 -1
- package/bin/internals/index.d.ts +6 -6
- package/package.json +38 -38
- package/src/IMediaPlayer.ts +29 -21
- package/src/LiveMediaSession.ts +305 -0
- package/src/LiveMediaSessionCoordinator.ts +621 -0
- package/src/{EphemeralMediaSessionCoordinatorSuspension.ts → LiveMediaSessionCoordinatorSuspension.ts} +39 -31
- package/src/MediaPlayerSynchronizer.ts +636 -450
- package/src/MediaSessionActionThrottler.ts +16 -14
- package/src/MediaSessionExtensions.ts +52 -42
- package/src/RepeatedActionThrottler.ts +74 -56
- package/src/VolumeManager.ts +197 -0
- package/src/index.ts +14 -15
- package/src/internals/GroupCoordinatorState.ts +606 -446
- package/src/internals/GroupPlaybackPosition.ts +227 -193
- package/src/internals/GroupPlaybackTrack.ts +176 -147
- package/src/internals/GroupPlaybackTrackData.ts +98 -90
- package/src/internals/GroupTransportState.ts +157 -127
- package/src/internals/consts.ts +98 -80
- package/src/internals/index.ts +11 -11
- package/src/test/GroupPlaybackPosition.spec.ts +600 -340
- package/src/test/GroupPlaybackTrack.spec.ts +122 -66
- package/src/test/SharedMediaSession.spec.ts +149 -113
- package/src/test/SharedMediaSessionCoordinator.spec.ts +271 -231
- package/src/test/VolumeManager.spec.ts +219 -0
- package/tsconfig.build.json +8 -8
- package/tsconfig.json +76 -75
- package/src/EphemeralMediaSession.ts +0 -248
- package/src/EphemeralMediaSessionCoordinator.ts +0 -461
- 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
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
this.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
this.
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
*
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
this.
|
|
377
|
-
this.
|
|
378
|
-
this.
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
this
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
* @
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
this.
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
+
}
|