@microsoft/live-share-media 1.3.0 → 1.4.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 +90 -90
- package/README.md +135 -135
- package/bin/LiveMediaSession.d.ts.map +1 -1
- package/bin/LiveMediaSession.js +9 -1
- package/bin/LiveMediaSession.js.map +1 -1
- package/bin/LiveMediaSessionCoordinator.d.ts +18 -2
- package/bin/LiveMediaSessionCoordinator.d.ts.map +1 -1
- package/bin/LiveMediaSessionCoordinator.js +52 -13
- package/bin/LiveMediaSessionCoordinator.js.map +1 -1
- package/bin/MediaPlayerSynchronizer.d.ts +6 -4
- package/bin/MediaPlayerSynchronizer.d.ts.map +1 -1
- package/bin/MediaPlayerSynchronizer.js +41 -12
- package/bin/MediaPlayerSynchronizer.js.map +1 -1
- package/bin/MediaSessionExtensions.d.ts +17 -8
- package/bin/MediaSessionExtensions.d.ts.map +1 -1
- package/bin/MediaSessionExtensions.js +4 -0
- package/bin/MediaSessionExtensions.js.map +1 -1
- package/bin/internals/GroupCoordinatorState.d.ts +15 -1
- package/bin/internals/GroupCoordinatorState.d.ts.map +1 -1
- package/bin/internals/GroupCoordinatorState.js +123 -57
- package/bin/internals/GroupCoordinatorState.js.map +1 -1
- package/bin/internals/GroupPlaybackPosition.d.ts +3 -1
- package/bin/internals/GroupPlaybackPosition.d.ts.map +1 -1
- package/bin/internals/GroupPlaybackPosition.js +7 -3
- package/bin/internals/GroupPlaybackPosition.js.map +1 -1
- package/bin/internals/GroupPlaybackRate.d.ts +39 -0
- package/bin/internals/GroupPlaybackRate.d.ts.map +1 -0
- package/bin/internals/GroupPlaybackRate.js +62 -0
- package/bin/internals/GroupPlaybackRate.js.map +1 -0
- package/bin/internals/GroupPlaybackTrack.d.ts +2 -2
- package/bin/internals/GroupPlaybackTrack.d.ts.map +1 -1
- package/bin/internals/GroupPlaybackTrack.js +3 -2
- 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 +12 -11
- package/bin/internals/GroupPlaybackTrackData.js.map +1 -1
- package/bin/internals/GroupTransportState.d.ts +6 -3
- package/bin/internals/GroupTransportState.d.ts.map +1 -1
- package/bin/internals/GroupTransportState.js +16 -11
- package/bin/internals/GroupTransportState.js.map +1 -1
- package/bin/internals/consts.d.ts +10 -3
- package/bin/internals/consts.d.ts.map +1 -1
- package/bin/internals/consts.js +10 -3
- package/bin/internals/consts.js.map +1 -1
- package/bin/internals/index.d.ts +1 -0
- package/bin/internals/index.d.ts.map +1 -1
- package/bin/internals/index.js +1 -0
- package/bin/internals/index.js.map +1 -1
- package/bin/internals/interfaces.d.ts +10 -0
- package/bin/internals/interfaces.d.ts.map +1 -0
- package/bin/internals/interfaces.js +3 -0
- package/bin/internals/interfaces.js.map +1 -0
- package/package.json +45 -45
- package/src/IMediaPlayer.ts +29 -29
- package/src/LiveMediaSession.ts +349 -336
- package/src/LiveMediaSessionCoordinator.ts +760 -678
- package/src/LiveMediaSessionCoordinatorSuspension.ts +39 -39
- package/src/MediaPlayerSynchronizer.ts +754 -714
- package/src/MediaSessionActionThrottler.ts +19 -19
- package/src/MediaSessionExtensions.ts +198 -187
- package/src/RepeatedActionThrottler.ts +76 -76
- package/src/VolumeManager.ts +200 -200
- package/src/index.ts +14 -14
- package/src/internals/GroupCoordinatorState.ts +802 -685
- package/src/internals/GroupPlaybackPosition.ts +235 -228
- package/src/internals/GroupPlaybackRate.ts +90 -0
- package/src/internals/GroupPlaybackTrack.ts +183 -182
- package/src/internals/GroupPlaybackTrackData.ts +105 -104
- package/src/internals/GroupTransportState.ts +179 -170
- package/src/internals/consts.ts +115 -102
- package/src/internals/errors.ts +42 -42
- package/src/internals/index.ts +13 -12
- package/src/internals/interfaces.ts +10 -0
- package/tsconfig.build.json +8 -8
- package/tsconfig.json +76 -76
- package/typedoc.json +10 -10
package/src/LiveMediaSession.ts
CHANGED
|
@@ -1,336 +1,349 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { DataObjectFactory } from "@fluidframework/aqueduct";
|
|
7
|
-
import {
|
|
8
|
-
DynamicObjectRegistry,
|
|
9
|
-
LiveDataObject,
|
|
10
|
-
LiveDataObjectInitializeNotNeededError,
|
|
11
|
-
LiveDataObjectInitializeState,
|
|
12
|
-
LiveTelemetryLogger,
|
|
13
|
-
UserMeetingRole,
|
|
14
|
-
} from "@microsoft/live-share";
|
|
15
|
-
import { MediaPlayerSynchronizer } from "./MediaPlayerSynchronizer";
|
|
16
|
-
import { ITriggerActionEvent, TelemetryEvents } from "./internals";
|
|
17
|
-
import {
|
|
18
|
-
MediaSessionCoordinatorEvents,
|
|
19
|
-
ExtendedMediaSessionAction,
|
|
20
|
-
ExtendedMediaSessionActionDetails,
|
|
21
|
-
ExtendedMediaSessionActionHandler,
|
|
22
|
-
} from "./MediaSessionExtensions";
|
|
23
|
-
import {
|
|
24
|
-
LiveMediaSessionCoordinator,
|
|
25
|
-
IMediaPlayerState,
|
|
26
|
-
} from "./LiveMediaSessionCoordinator";
|
|
27
|
-
import { MediaSessionActionThrottler } from "./MediaSessionActionThrottler";
|
|
28
|
-
import { RepeatedActionThrottler } from "./RepeatedActionThrottler";
|
|
29
|
-
import { IMediaPlayer } from "./IMediaPlayer";
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Live fluid object that synchronizes media playback across multiple clients.
|
|
33
|
-
*/
|
|
34
|
-
export class LiveMediaSession extends LiveDataObject {
|
|
35
|
-
private _actionThrottler: MediaSessionActionThrottler =
|
|
36
|
-
new RepeatedActionThrottler();
|
|
37
|
-
private _logger?: LiveTelemetryLogger;
|
|
38
|
-
private _requestPlayerStateHandler?: () => IMediaPlayerState;
|
|
39
|
-
private _coordinator?: LiveMediaSessionCoordinator;
|
|
40
|
-
private readonly _actionHandlers: Map<
|
|
41
|
-
string,
|
|
42
|
-
MediaSessionActionHandler | ExtendedMediaSessionActionHandler
|
|
43
|
-
> = new Map();
|
|
44
|
-
private _synchronizing?: MediaPlayerSynchronizer;
|
|
45
|
-
|
|
46
|
-
// Position update timer
|
|
47
|
-
private _updateTimer: any;
|
|
48
|
-
private _lastUpdateTime: number = 0;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The objects fluid type name.
|
|
52
|
-
*/
|
|
53
|
-
public static readonly TypeName = `@microsoft/fluid-media:SharedMediaSession`;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The objects fluid type factory.
|
|
57
|
-
*/
|
|
58
|
-
public static readonly factory = new DataObjectFactory(
|
|
59
|
-
LiveMediaSession.TypeName,
|
|
60
|
-
LiveMediaSession,
|
|
61
|
-
[],
|
|
62
|
-
{}
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Extension point that lets applications replace the default logic for throttling the sessions
|
|
67
|
-
* local sync behavior.
|
|
68
|
-
*
|
|
69
|
-
* @remarks
|
|
70
|
-
* The `LiveMediaCoordinator` is fairly aggressive at wanting to keep the local media player
|
|
71
|
-
* in sync with the rest of the group. This aggressiveness can result in the coordinator sending
|
|
72
|
-
* new sync actions before the local player has finished responding to the previous sync action.
|
|
73
|
-
* The `ActionThrottler` gives apps fine grain control over how aggressive they want sync to be.
|
|
74
|
-
*
|
|
75
|
-
* By default, a `RepeatedAction` throttler is used which prevents the same sync action from
|
|
76
|
-
* being sent within an adjustable time period.
|
|
77
|
-
*/
|
|
78
|
-
public get actionThrottler(): MediaSessionActionThrottler {
|
|
79
|
-
return this._actionThrottler;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public set actionThrottler(value: MediaSessionActionThrottler) {
|
|
83
|
-
this._actionThrottler = value;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The group coordinator for the session.
|
|
88
|
-
*/
|
|
89
|
-
public get coordinator(): LiveMediaSessionCoordinator {
|
|
90
|
-
return this._coordinator!;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Returns the logger used by the session and coordinator.
|
|
95
|
-
*
|
|
96
|
-
* @remarks
|
|
97
|
-
* This is used by the `MediaPlayerSynchronizer` to log events.
|
|
98
|
-
*/
|
|
99
|
-
public get logger(): LiveTelemetryLogger {
|
|
100
|
-
return this._logger!;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Initialize the object to begin sending/receiving group playback updates through this DDS.
|
|
105
|
-
*
|
|
106
|
-
* @param allowedRoles Optional. List of roles allowed to group transport operations like play/pause/seek/setTrack.
|
|
107
|
-
*
|
|
108
|
-
* @returns a void promise that resolves once complete.
|
|
109
|
-
*
|
|
110
|
-
* @throws error when `.initialize()` has already been called for this class instance.
|
|
111
|
-
*/
|
|
112
|
-
public async initialize(allowedRoles?: UserMeetingRole[]): Promise<void> {
|
|
113
|
-
LiveDataObjectInitializeNotNeededError.assert(
|
|
114
|
-
"LiveMediaSession:initialize",
|
|
115
|
-
this.initializeState
|
|
116
|
-
);
|
|
117
|
-
// Update initialize state as pending
|
|
118
|
-
this.initializeState = LiveDataObjectInitializeState.pending;
|
|
119
|
-
if (allowedRoles) {
|
|
120
|
-
this._allowedRoles = allowedRoles;
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
await this.coordinator.initialize(allowedRoles);
|
|
124
|
-
} catch (error: unknown) {
|
|
125
|
-
this.initializeState = LiveDataObjectInitializeState.needed;
|
|
126
|
-
throw error;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Update initialize state as succeeded
|
|
130
|
-
this.initializeState = LiveDataObjectInitializeState.succeeded;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Registers an action handler with the session.
|
|
135
|
-
* @param action Name of the action to register a handler for.
|
|
136
|
-
* @param handler Function called when the action is triggered.
|
|
137
|
-
*/
|
|
138
|
-
public setActionHandler(
|
|
139
|
-
action: ExtendedMediaSessionAction,
|
|
140
|
-
handler:
|
|
141
|
-
| ExtendedMediaSessionActionHandler
|
|
142
|
-
| MediaSessionActionHandler
|
|
143
|
-
| null
|
|
144
|
-
): void {
|
|
145
|
-
if (handler) {
|
|
146
|
-
// add handler
|
|
147
|
-
this._actionHandlers.set(action, handler);
|
|
148
|
-
} else if (this._actionHandlers.has(action)) {
|
|
149
|
-
// remove handler
|
|
150
|
-
this._actionHandlers.delete(action);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Registers a handler that will be queried anytime the group coordinate needs to know the
|
|
156
|
-
* local players transport state and position.
|
|
157
|
-
*/
|
|
158
|
-
public setRequestPlayerStateHandler(handler: () => IMediaPlayerState) {
|
|
159
|
-
this._requestPlayerStateHandler = handler;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Begins synchronizing the playback of a media element.
|
|
164
|
-
* @param player Something that "looks like" and HTML Media Element.
|
|
165
|
-
* @returns A new synchronizer instance. Call `synchronizer.end()` to stop synchronizing the elements playback.
|
|
166
|
-
*/
|
|
167
|
-
public synchronize(player: IMediaPlayer): MediaPlayerSynchronizer {
|
|
168
|
-
// A session can only synchronize one player at a time
|
|
169
|
-
if (this._synchronizing) {
|
|
170
|
-
this._synchronizing.end();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// Start position update timer
|
|
174
|
-
if (!this._updateTimer) {
|
|
175
|
-
this.logger.sendTelemetryEvent(
|
|
176
|
-
TelemetryEvents.MediaSession.BeginPositionUpdateTimer
|
|
177
|
-
);
|
|
178
|
-
this._updateTimer = setInterval(() => {
|
|
179
|
-
// Get current state
|
|
180
|
-
const state = this.getCurrentPlayerState();
|
|
181
|
-
|
|
182
|
-
// Check for hit wait point
|
|
183
|
-
this.checkWaitPointHit(state);
|
|
184
|
-
|
|
185
|
-
// Send position update if interval hit
|
|
186
|
-
const now = new Date().getTime();
|
|
187
|
-
const delta = (now - this._lastUpdateTime) / 1000;
|
|
188
|
-
if (
|
|
189
|
-
this.coordinator.isInitialized &&
|
|
190
|
-
delta >= this.coordinator.positionUpdateInterval
|
|
191
|
-
) {
|
|
192
|
-
this._lastUpdateTime = now;
|
|
193
|
-
this.coordinator.sendPositionUpdate(state);
|
|
194
|
-
}
|
|
195
|
-
}, 500);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return new MediaPlayerSynchronizer(player, this, this.runtime, () => {
|
|
199
|
-
this._synchronizing = undefined;
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
protected async hasInitialized(): Promise<void> {
|
|
204
|
-
this._logger = new LiveTelemetryLogger(this.runtime, this.liveRuntime);
|
|
205
|
-
|
|
206
|
-
// Create coordinator and listen for triggered actions
|
|
207
|
-
this._coordinator = new LiveMediaSessionCoordinator(
|
|
208
|
-
this.
|
|
209
|
-
this.
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
const
|
|
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
|
-
break;
|
|
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
|
-
)
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { DataObjectFactory } from "@fluidframework/aqueduct";
|
|
7
|
+
import {
|
|
8
|
+
DynamicObjectRegistry,
|
|
9
|
+
LiveDataObject,
|
|
10
|
+
LiveDataObjectInitializeNotNeededError,
|
|
11
|
+
LiveDataObjectInitializeState,
|
|
12
|
+
LiveTelemetryLogger,
|
|
13
|
+
UserMeetingRole,
|
|
14
|
+
} from "@microsoft/live-share";
|
|
15
|
+
import { MediaPlayerSynchronizer } from "./MediaPlayerSynchronizer";
|
|
16
|
+
import { ITriggerActionEvent, TelemetryEvents } from "./internals";
|
|
17
|
+
import {
|
|
18
|
+
MediaSessionCoordinatorEvents,
|
|
19
|
+
ExtendedMediaSessionAction,
|
|
20
|
+
ExtendedMediaSessionActionDetails,
|
|
21
|
+
ExtendedMediaSessionActionHandler,
|
|
22
|
+
} from "./MediaSessionExtensions";
|
|
23
|
+
import {
|
|
24
|
+
LiveMediaSessionCoordinator,
|
|
25
|
+
IMediaPlayerState,
|
|
26
|
+
} from "./LiveMediaSessionCoordinator";
|
|
27
|
+
import { MediaSessionActionThrottler } from "./MediaSessionActionThrottler";
|
|
28
|
+
import { RepeatedActionThrottler } from "./RepeatedActionThrottler";
|
|
29
|
+
import { IMediaPlayer } from "./IMediaPlayer";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Live fluid object that synchronizes media playback across multiple clients.
|
|
33
|
+
*/
|
|
34
|
+
export class LiveMediaSession extends LiveDataObject {
|
|
35
|
+
private _actionThrottler: MediaSessionActionThrottler =
|
|
36
|
+
new RepeatedActionThrottler();
|
|
37
|
+
private _logger?: LiveTelemetryLogger;
|
|
38
|
+
private _requestPlayerStateHandler?: () => IMediaPlayerState;
|
|
39
|
+
private _coordinator?: LiveMediaSessionCoordinator;
|
|
40
|
+
private readonly _actionHandlers: Map<
|
|
41
|
+
string,
|
|
42
|
+
MediaSessionActionHandler | ExtendedMediaSessionActionHandler
|
|
43
|
+
> = new Map();
|
|
44
|
+
private _synchronizing?: MediaPlayerSynchronizer;
|
|
45
|
+
|
|
46
|
+
// Position update timer
|
|
47
|
+
private _updateTimer: any;
|
|
48
|
+
private _lastUpdateTime: number = 0;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The objects fluid type name.
|
|
52
|
+
*/
|
|
53
|
+
public static readonly TypeName = `@microsoft/fluid-media:SharedMediaSession`;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The objects fluid type factory.
|
|
57
|
+
*/
|
|
58
|
+
public static readonly factory = new DataObjectFactory(
|
|
59
|
+
LiveMediaSession.TypeName,
|
|
60
|
+
LiveMediaSession,
|
|
61
|
+
[],
|
|
62
|
+
{}
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Extension point that lets applications replace the default logic for throttling the sessions
|
|
67
|
+
* local sync behavior.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* The `LiveMediaCoordinator` is fairly aggressive at wanting to keep the local media player
|
|
71
|
+
* in sync with the rest of the group. This aggressiveness can result in the coordinator sending
|
|
72
|
+
* new sync actions before the local player has finished responding to the previous sync action.
|
|
73
|
+
* The `ActionThrottler` gives apps fine grain control over how aggressive they want sync to be.
|
|
74
|
+
*
|
|
75
|
+
* By default, a `RepeatedAction` throttler is used which prevents the same sync action from
|
|
76
|
+
* being sent within an adjustable time period.
|
|
77
|
+
*/
|
|
78
|
+
public get actionThrottler(): MediaSessionActionThrottler {
|
|
79
|
+
return this._actionThrottler;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public set actionThrottler(value: MediaSessionActionThrottler) {
|
|
83
|
+
this._actionThrottler = value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The group coordinator for the session.
|
|
88
|
+
*/
|
|
89
|
+
public get coordinator(): LiveMediaSessionCoordinator {
|
|
90
|
+
return this._coordinator!;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Returns the logger used by the session and coordinator.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* This is used by the `MediaPlayerSynchronizer` to log events.
|
|
98
|
+
*/
|
|
99
|
+
public get logger(): LiveTelemetryLogger {
|
|
100
|
+
return this._logger!;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Initialize the object to begin sending/receiving group playback updates through this DDS.
|
|
105
|
+
*
|
|
106
|
+
* @param allowedRoles Optional. List of roles allowed to group transport operations like play/pause/seek/setTrack.
|
|
107
|
+
*
|
|
108
|
+
* @returns a void promise that resolves once complete.
|
|
109
|
+
*
|
|
110
|
+
* @throws error when `.initialize()` has already been called for this class instance.
|
|
111
|
+
*/
|
|
112
|
+
public async initialize(allowedRoles?: UserMeetingRole[]): Promise<void> {
|
|
113
|
+
LiveDataObjectInitializeNotNeededError.assert(
|
|
114
|
+
"LiveMediaSession:initialize",
|
|
115
|
+
this.initializeState
|
|
116
|
+
);
|
|
117
|
+
// Update initialize state as pending
|
|
118
|
+
this.initializeState = LiveDataObjectInitializeState.pending;
|
|
119
|
+
if (allowedRoles) {
|
|
120
|
+
this._allowedRoles = allowedRoles;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
await this.coordinator.initialize(allowedRoles);
|
|
124
|
+
} catch (error: unknown) {
|
|
125
|
+
this.initializeState = LiveDataObjectInitializeState.needed;
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Update initialize state as succeeded
|
|
130
|
+
this.initializeState = LiveDataObjectInitializeState.succeeded;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Registers an action handler with the session.
|
|
135
|
+
* @param action Name of the action to register a handler for.
|
|
136
|
+
* @param handler Function called when the action is triggered.
|
|
137
|
+
*/
|
|
138
|
+
public setActionHandler(
|
|
139
|
+
action: ExtendedMediaSessionAction,
|
|
140
|
+
handler:
|
|
141
|
+
| ExtendedMediaSessionActionHandler
|
|
142
|
+
| MediaSessionActionHandler
|
|
143
|
+
| null
|
|
144
|
+
): void {
|
|
145
|
+
if (handler) {
|
|
146
|
+
// add handler
|
|
147
|
+
this._actionHandlers.set(action, handler);
|
|
148
|
+
} else if (this._actionHandlers.has(action)) {
|
|
149
|
+
// remove handler
|
|
150
|
+
this._actionHandlers.delete(action);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Registers a handler that will be queried anytime the group coordinate needs to know the
|
|
156
|
+
* local players transport state and position.
|
|
157
|
+
*/
|
|
158
|
+
public setRequestPlayerStateHandler(handler: () => IMediaPlayerState) {
|
|
159
|
+
this._requestPlayerStateHandler = handler;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Begins synchronizing the playback of a media element.
|
|
164
|
+
* @param player Something that "looks like" and HTML Media Element.
|
|
165
|
+
* @returns A new synchronizer instance. Call `synchronizer.end()` to stop synchronizing the elements playback.
|
|
166
|
+
*/
|
|
167
|
+
public synchronize(player: IMediaPlayer): MediaPlayerSynchronizer {
|
|
168
|
+
// A session can only synchronize one player at a time
|
|
169
|
+
if (this._synchronizing) {
|
|
170
|
+
this._synchronizing.end();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Start position update timer
|
|
174
|
+
if (!this._updateTimer) {
|
|
175
|
+
this.logger.sendTelemetryEvent(
|
|
176
|
+
TelemetryEvents.MediaSession.BeginPositionUpdateTimer
|
|
177
|
+
);
|
|
178
|
+
this._updateTimer = setInterval(() => {
|
|
179
|
+
// Get current state
|
|
180
|
+
const state = this.getCurrentPlayerState();
|
|
181
|
+
|
|
182
|
+
// Check for hit wait point
|
|
183
|
+
this.checkWaitPointHit(state);
|
|
184
|
+
|
|
185
|
+
// Send position update if interval hit
|
|
186
|
+
const now = new Date().getTime();
|
|
187
|
+
const delta = (now - this._lastUpdateTime) / 1000;
|
|
188
|
+
if (
|
|
189
|
+
this.coordinator.isInitialized &&
|
|
190
|
+
delta >= this.coordinator.positionUpdateInterval
|
|
191
|
+
) {
|
|
192
|
+
this._lastUpdateTime = now;
|
|
193
|
+
this.coordinator.sendPositionUpdate(state);
|
|
194
|
+
}
|
|
195
|
+
}, 500);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return new MediaPlayerSynchronizer(player, this, this.runtime, () => {
|
|
199
|
+
this._synchronizing = undefined;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
protected async hasInitialized(): Promise<void> {
|
|
204
|
+
this._logger = new LiveTelemetryLogger(this.runtime, this.liveRuntime);
|
|
205
|
+
|
|
206
|
+
// Create coordinator and listen for triggered actions
|
|
207
|
+
this._coordinator = new LiveMediaSessionCoordinator(
|
|
208
|
+
this.id,
|
|
209
|
+
this.runtime,
|
|
210
|
+
this.liveRuntime,
|
|
211
|
+
() => this.getCurrentPlayerState()
|
|
212
|
+
);
|
|
213
|
+
this._coordinator.on(
|
|
214
|
+
MediaSessionCoordinatorEvents.triggeraction,
|
|
215
|
+
(event: ITriggerActionEvent) => {
|
|
216
|
+
// Pre-process actions
|
|
217
|
+
const details = event.details;
|
|
218
|
+
const seekTime =
|
|
219
|
+
typeof details.seekTime == "number" ? details.seekTime : -1;
|
|
220
|
+
switch (details.action) {
|
|
221
|
+
case "play":
|
|
222
|
+
this.logger.sendTelemetryEvent(
|
|
223
|
+
TelemetryEvents.MediaSession.PlayAction,
|
|
224
|
+
null,
|
|
225
|
+
{
|
|
226
|
+
position: seekTime,
|
|
227
|
+
}
|
|
228
|
+
);
|
|
229
|
+
break;
|
|
230
|
+
case "pause":
|
|
231
|
+
this.logger.sendTelemetryEvent(
|
|
232
|
+
TelemetryEvents.MediaSession.PauseAction,
|
|
233
|
+
null,
|
|
234
|
+
{
|
|
235
|
+
position: seekTime,
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
break;
|
|
239
|
+
case "ratechange":
|
|
240
|
+
if (!details.playbackRate) {
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
this.logger.sendTelemetryEvent(
|
|
244
|
+
TelemetryEvents.MediaSession.RateChangeAction,
|
|
245
|
+
null,
|
|
246
|
+
{
|
|
247
|
+
playbackRate: details.playbackRate,
|
|
248
|
+
}
|
|
249
|
+
);
|
|
250
|
+
break;
|
|
251
|
+
case "seekto":
|
|
252
|
+
this.logger.sendTelemetryEvent(
|
|
253
|
+
TelemetryEvents.MediaSession.SeekToAction,
|
|
254
|
+
null,
|
|
255
|
+
{
|
|
256
|
+
position: seekTime,
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
break;
|
|
260
|
+
case "settrack":
|
|
261
|
+
this.logger.sendTelemetryEvent(
|
|
262
|
+
TelemetryEvents.MediaSession.SetTrackAction
|
|
263
|
+
);
|
|
264
|
+
break;
|
|
265
|
+
case "datachange":
|
|
266
|
+
this.logger.sendTelemetryEvent(
|
|
267
|
+
TelemetryEvents.MediaSession.DataChangeAction
|
|
268
|
+
);
|
|
269
|
+
break;
|
|
270
|
+
case "catchup":
|
|
271
|
+
// Default to seekto if catchup not implemented
|
|
272
|
+
this.logger.sendTelemetryEvent(
|
|
273
|
+
TelemetryEvents.MediaSession.CatchUpAction,
|
|
274
|
+
null,
|
|
275
|
+
{
|
|
276
|
+
position: seekTime,
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
if (!this._actionHandlers.has("catchup")) {
|
|
280
|
+
details.action = "seekto";
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Dispatch action
|
|
286
|
+
this.dispatchAction(details);
|
|
287
|
+
}
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
private checkWaitPointHit(state: IMediaPlayerState): void {
|
|
292
|
+
// Was a wait point hit?
|
|
293
|
+
if (state.positionState && this.coordinator.isInitialized) {
|
|
294
|
+
const waitPoint = this.coordinator.findNextWaitPoint();
|
|
295
|
+
if (
|
|
296
|
+
waitPoint &&
|
|
297
|
+
state.positionState.position != undefined &&
|
|
298
|
+
state.positionState.position >= waitPoint.position
|
|
299
|
+
) {
|
|
300
|
+
// Ensure handler registered
|
|
301
|
+
if (!this._actionHandlers.has("wait")) {
|
|
302
|
+
throw new Error(
|
|
303
|
+
`LiveMediaSession:checkWaitPointHit - wait point hit but no 'wait' action registered.\nTo fix this error, set a \`wait\` action using the \`setActionHandler()\` function.`
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Begin suspension for wait point
|
|
308
|
+
this.waitUntilConnected().then((clientId: string) => {
|
|
309
|
+
const suspension =
|
|
310
|
+
this.coordinator.beginSuspension(waitPoint);
|
|
311
|
+
this.dispatchAction({
|
|
312
|
+
action: "wait",
|
|
313
|
+
source: "system",
|
|
314
|
+
clientId,
|
|
315
|
+
local: true,
|
|
316
|
+
suspension,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private getCurrentPlayerState(): IMediaPlayerState {
|
|
324
|
+
if (!this._requestPlayerStateHandler) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
"LiveMediaSession:getCurrentPlayerState - no `requestPlayerStateHandler` callback configured.\nTo fix this error, ensure `setRequestPlayerStateHandler()` has been set with a delegate to retrieve your player's state, or use the `synchronize()` function to create a new `MediaPlayerSynchronizer` instance tied to your `IMediaPlayer` instance."
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return this._requestPlayerStateHandler();
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private dispatchAction(
|
|
334
|
+
details: MediaSessionActionDetails | ExtendedMediaSessionActionDetails
|
|
335
|
+
): void {
|
|
336
|
+
if (this._actionHandlers.has(details.action)) {
|
|
337
|
+
const handler = this._actionHandlers.get(details.action);
|
|
338
|
+
this._actionThrottler.throttled(details, handler);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Register `LiveMediaSession` as an available `LoadableObjectClass` for use in packages that support dynamic object loading, such as `@microsoft/live-share-turbo`.
|
|
345
|
+
*/
|
|
346
|
+
DynamicObjectRegistry.registerObjectClass(
|
|
347
|
+
LiveMediaSession,
|
|
348
|
+
LiveMediaSession.TypeName
|
|
349
|
+
);
|