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