@microsoft/live-share-react 1.2.2-preview.0 → 1.3.1-preview.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 +9 -9
- package/LICENSE +86 -86
- package/README.md +608 -608
- package/bin/internal/index.d.ts +0 -1
- package/bin/internal/index.d.ts.map +1 -1
- package/bin/internal/index.js +0 -1
- package/bin/internal/index.js.map +1 -1
- package/bin/providers/AzureProvider.d.ts +1 -1
- package/bin/providers/AzureProvider.d.ts.map +1 -1
- package/bin/providers/AzureProvider.js +1 -1
- package/bin/providers/AzureProvider.js.map +1 -1
- package/bin/providers/LiveShareProvider.js +1 -1
- package/bin/providers/LiveShareProvider.js.map +1 -1
- package/bin/shared-hooks/index.d.ts +1 -0
- package/bin/shared-hooks/index.d.ts.map +1 -1
- package/bin/shared-hooks/index.js +1 -0
- package/bin/shared-hooks/index.js.map +1 -1
- package/bin/{internal → shared-hooks}/useSharedStateRegistry.d.ts +6 -0
- package/bin/shared-hooks/useSharedStateRegistry.d.ts.map +1 -0
- package/bin/{internal → shared-hooks}/useSharedStateRegistry.js +3 -0
- package/bin/shared-hooks/useSharedStateRegistry.js.map +1 -0
- package/package.json +50 -50
- package/src/index.ts +6 -6
- package/src/interfaces/LiveInterfaceExtensions.ts +26 -26
- package/src/interfaces/index.ts +1 -1
- package/src/internal/errors.ts +57 -57
- package/src/internal/index.ts +2 -3
- package/src/internal/type-guards.ts +9 -9
- package/src/live-hooks/index.ts +7 -7
- package/src/live-hooks/useLiveCanvas.ts +191 -191
- package/src/live-hooks/useLiveEvent.ts +140 -140
- package/src/live-hooks/useLiveFollowMode.ts +316 -316
- package/src/live-hooks/useLivePresence.ts +147 -147
- package/src/live-hooks/useLiveState.ts +110 -110
- package/src/live-hooks/useLiveTimer.ts +213 -213
- package/src/live-hooks/useMediaSynchronizer.ts +295 -295
- package/src/providers/AzureProvider.tsx +261 -261
- package/src/providers/LiveShareProvider.tsx +210 -210
- package/src/providers/index.ts +2 -2
- package/src/shared-hooks/index.ts +4 -3
- package/src/shared-hooks/useDynamicDDS.ts +82 -82
- package/src/shared-hooks/useSharedMap.ts +159 -159
- package/src/shared-hooks/useSharedState.ts +96 -96
- package/src/{internal → shared-hooks}/useSharedStateRegistry.ts +170 -164
- package/src/types/ActionTypes.ts +161 -161
- package/src/types/PropTypes.ts +10 -10
- package/src/types/ResultTypes.ts +332 -332
- package/src/types/index.ts +3 -3
- package/src/utils/index.ts +1 -1
- package/src/utils/type-check.ts +69 -69
- package/tsconfig.build.json +5 -5
- package/tsconfig.json +72 -72
- package/bin/internal/useSharedStateRegistry.d.ts.map +0 -1
- package/bin/internal/useSharedStateRegistry.js.map +0 -1
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
-
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
LiveDataObjectInitializeState,
|
|
8
|
-
UserMeetingRole,
|
|
9
|
-
} from "@microsoft/live-share";
|
|
10
|
-
import React from "react";
|
|
11
|
-
import {
|
|
12
|
-
CoordinationWaitPoint,
|
|
13
|
-
ExtendedMediaMetadata,
|
|
14
|
-
IMediaPlayer,
|
|
15
|
-
LiveMediaSession,
|
|
16
|
-
MediaPlayerSynchronizer,
|
|
17
|
-
MediaSessionCoordinatorSuspension,
|
|
18
|
-
} from "@microsoft/live-share-media";
|
|
19
|
-
import { isExtendedMediaMetadata, isMediaElement, isRefObject } from "../utils";
|
|
20
|
-
import { useDynamicDDS } from "../shared-hooks";
|
|
21
|
-
import { IUseMediaSynchronizerResults } from "../types";
|
|
22
|
-
import { useFluidObjectsContext } from "../providers";
|
|
23
|
-
import {
|
|
24
|
-
ActionContainerNotJoinedError,
|
|
25
|
-
ActionLiveDataObjectUndefinedError,
|
|
26
|
-
} from "../internal";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* React hook for using a Live Share media `MediaPlayerSynchronizer`.
|
|
30
|
-
*
|
|
31
|
-
* @remarks
|
|
32
|
-
* Use this hook if you want to synchronize the playback position of a video or audio element during a Live Share session.
|
|
33
|
-
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
34
|
-
*
|
|
35
|
-
* @param uniqueKey uniqueKey value for the `LiveMediaSession` DDS object used by `MediaPlayerSynchronizer`.
|
|
36
|
-
* @param mediaPlayerElement React RefObject containing object/element conforming to IMediaPlayer interface, `IMediaPlayer` object, or string id for <video> / <audio> element.
|
|
37
|
-
* @param initialTrack initial track to load. Either ExtendedMediaMetadata, trackId string, or null.
|
|
38
|
-
* @param allowedRoles Optional. Array of user roles that are eligible to modify group playback state.
|
|
39
|
-
* @param viewOnly Optional. Flag for whether or not the media synchronizer should be in viewOnly mode.
|
|
40
|
-
* @param canSendPositionUpdates Optional. Controls whether or not the local client is allowed to send position updates to the group.
|
|
41
|
-
* @returns IUseMediaSynchronizerResults object.
|
|
42
|
-
*/
|
|
43
|
-
export function useMediaSynchronizer(
|
|
44
|
-
uniqueKey: string,
|
|
45
|
-
mediaPlayerElement:
|
|
46
|
-
| React.RefObject<IMediaPlayer>
|
|
47
|
-
| IMediaPlayer
|
|
48
|
-
| string
|
|
49
|
-
| null,
|
|
50
|
-
initialTrack: Partial<ExtendedMediaMetadata> | string | null,
|
|
51
|
-
allowedRoles?: UserMeetingRole[],
|
|
52
|
-
viewOnly?: boolean,
|
|
53
|
-
canSendPositionUpdates?: boolean
|
|
54
|
-
): IUseMediaSynchronizerResults {
|
|
55
|
-
/**
|
|
56
|
-
* User facing media synchronizer and non-user facing setter
|
|
57
|
-
*/
|
|
58
|
-
const [mediaSynchronizer, setMediaSynchronizer] = React.useState<
|
|
59
|
-
MediaPlayerSynchronizer | undefined
|
|
60
|
-
>();
|
|
61
|
-
/**
|
|
62
|
-
* Suspension object and setter
|
|
63
|
-
*/
|
|
64
|
-
const [suspension, setSuspension] = React.useState<
|
|
65
|
-
MediaSessionCoordinatorSuspension | undefined
|
|
66
|
-
>();
|
|
67
|
-
/**
|
|
68
|
-
* User facing: dynamically load the LiveMediaSession DDS for the given unique key.
|
|
69
|
-
*/
|
|
70
|
-
const { dds: mediaSession } = useDynamicDDS<LiveMediaSession>(
|
|
71
|
-
uniqueKey,
|
|
72
|
-
LiveMediaSession
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const { container } = useFluidObjectsContext();
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Play callback
|
|
79
|
-
*/
|
|
80
|
-
const play = React.useCallback(async () => {
|
|
81
|
-
if (!container) {
|
|
82
|
-
throw new ActionContainerNotJoinedError(
|
|
83
|
-
"mediaSynchronizer",
|
|
84
|
-
"play"
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
if (mediaSynchronizer === undefined) {
|
|
88
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
89
|
-
"mediaSynchronizer",
|
|
90
|
-
"play"
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
return await mediaSynchronizer.play();
|
|
94
|
-
}, [container, mediaSynchronizer]);
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Pause callback
|
|
98
|
-
*/
|
|
99
|
-
const pause = React.useCallback(async () => {
|
|
100
|
-
if (!container) {
|
|
101
|
-
throw new ActionContainerNotJoinedError(
|
|
102
|
-
"mediaSynchronizer",
|
|
103
|
-
"pause"
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
if (mediaSynchronizer === undefined) {
|
|
107
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
108
|
-
"mediaSynchronizer",
|
|
109
|
-
"pause"
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
return await mediaSynchronizer.pause();
|
|
113
|
-
}, [container, mediaSynchronizer]);
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* User facing: seek callback
|
|
117
|
-
*/
|
|
118
|
-
const seekTo = React.useCallback(
|
|
119
|
-
async (time: number) => {
|
|
120
|
-
if (!container) {
|
|
121
|
-
throw new ActionContainerNotJoinedError(
|
|
122
|
-
"mediaSynchronizer",
|
|
123
|
-
"seekTo"
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
if (mediaSynchronizer === undefined) {
|
|
127
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
128
|
-
"mediaSynchronizer",
|
|
129
|
-
"seekTo"
|
|
130
|
-
);
|
|
131
|
-
}
|
|
132
|
-
return await mediaSynchronizer.seekTo(time);
|
|
133
|
-
},
|
|
134
|
-
[container, mediaSynchronizer]
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* User facing: set track callback
|
|
139
|
-
*/
|
|
140
|
-
const setTrack = React.useCallback(
|
|
141
|
-
async (track: Partial<ExtendedMediaMetadata> | string | null) => {
|
|
142
|
-
if (!container) {
|
|
143
|
-
throw new ActionContainerNotJoinedError(
|
|
144
|
-
"mediaSynchronizer",
|
|
145
|
-
"setTrack"
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
if (mediaSynchronizer === undefined) {
|
|
149
|
-
throw new ActionLiveDataObjectUndefinedError(
|
|
150
|
-
"mediaSynchronizer",
|
|
151
|
-
"setTrack"
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
if (isExtendedMediaMetadata(track)) {
|
|
155
|
-
return await mediaSynchronizer.setTrack(track);
|
|
156
|
-
} else if (typeof track === "string") {
|
|
157
|
-
return await mediaSynchronizer.setTrack({
|
|
158
|
-
trackIdentifier: track,
|
|
159
|
-
} as ExtendedMediaMetadata);
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
[container, mediaSynchronizer]
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* User facing: create a new suspension and set it locally to state
|
|
167
|
-
*/
|
|
168
|
-
const beginSuspension = React.useCallback(
|
|
169
|
-
(waitPoint?: CoordinationWaitPoint) => {
|
|
170
|
-
const suspension =
|
|
171
|
-
mediaSession?.coordinator.beginSuspension(waitPoint);
|
|
172
|
-
setSuspension(suspension);
|
|
173
|
-
},
|
|
174
|
-
[mediaSession]
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* User facing: if a suspension is active, end it. Called when "Follow presenter" button is clicked.
|
|
179
|
-
*/
|
|
180
|
-
const endSuspension = React.useCallback(() => {
|
|
181
|
-
suspension?.end();
|
|
182
|
-
setSuspension(undefined);
|
|
183
|
-
}, [suspension]);
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Setup change listeners and start `LiveMediaSession` if needed
|
|
187
|
-
*/
|
|
188
|
-
React.useEffect(() => {
|
|
189
|
-
if (mediaSession === undefined || !mediaPlayerElement) return;
|
|
190
|
-
let mounted = true;
|
|
191
|
-
// Query the HTML5 media element from the document and set reference
|
|
192
|
-
let mediaPlayer: IMediaPlayer | undefined;
|
|
193
|
-
if (isRefObject<IMediaPlayer>(mediaPlayerElement)) {
|
|
194
|
-
if (!mediaPlayerElement.current) return;
|
|
195
|
-
mediaPlayer = mediaPlayerElement.current;
|
|
196
|
-
} else if (typeof mediaPlayerElement === "string") {
|
|
197
|
-
const mediaElement = document.getElementById(
|
|
198
|
-
mediaPlayerElement
|
|
199
|
-
) as any;
|
|
200
|
-
if (!isMediaElement(mediaElement)) return;
|
|
201
|
-
mediaPlayer = mediaElement;
|
|
202
|
-
} else {
|
|
203
|
-
mediaPlayer = mediaPlayerElement;
|
|
204
|
-
}
|
|
205
|
-
if (initialTrack) {
|
|
206
|
-
// If supported type and not null, set the initial src
|
|
207
|
-
if (isExtendedMediaMetadata(initialTrack)) {
|
|
208
|
-
mediaPlayer.src = initialTrack.trackIdentifier;
|
|
209
|
-
} else if (typeof initialTrack === "string") {
|
|
210
|
-
mediaPlayer.src = initialTrack;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
// Begin synchronizing a MediaSynchronizer for the player
|
|
214
|
-
const synchronizer = mediaSession.synchronize(mediaPlayer);
|
|
215
|
-
if (viewOnly !== undefined) {
|
|
216
|
-
// Set synchronizer to view only mode provided by developer
|
|
217
|
-
synchronizer.viewOnly = viewOnly;
|
|
218
|
-
}
|
|
219
|
-
if (canSendPositionUpdates !== undefined) {
|
|
220
|
-
synchronizer.mediaSession.coordinator.canSendPositionUpdates =
|
|
221
|
-
canSendPositionUpdates;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (
|
|
225
|
-
mediaSession.initializeState ===
|
|
226
|
-
LiveDataObjectInitializeState.needed
|
|
227
|
-
) {
|
|
228
|
-
// Start synchronizing the media session
|
|
229
|
-
mediaSession.initialize(allowedRoles ?? []);
|
|
230
|
-
} else if (initialTrack) {
|
|
231
|
-
mediaSession.onLocalUserAllowed(async () => {
|
|
232
|
-
if (!mounted) return;
|
|
233
|
-
try {
|
|
234
|
-
if (isExtendedMediaMetadata(initialTrack)) {
|
|
235
|
-
await synchronizer.setTrack(initialTrack);
|
|
236
|
-
} else if (typeof initialTrack === "string") {
|
|
237
|
-
await synchronizer.setTrack({
|
|
238
|
-
trackIdentifier: initialTrack,
|
|
239
|
-
} as ExtendedMediaMetadata);
|
|
240
|
-
}
|
|
241
|
-
} catch (err) {
|
|
242
|
-
console.error(err);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
setMediaSynchronizer(synchronizer);
|
|
247
|
-
|
|
248
|
-
return () => {
|
|
249
|
-
mounted = false;
|
|
250
|
-
synchronizer.removeAllListeners();
|
|
251
|
-
mediaSession.removeAllListeners();
|
|
252
|
-
synchronizer?.end();
|
|
253
|
-
mediaSession?.dispose();
|
|
254
|
-
};
|
|
255
|
-
}, [mediaSession, mediaPlayerElement]);
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Change view in media synchronizer only if prop changes
|
|
259
|
-
*/
|
|
260
|
-
React.useEffect(() => {
|
|
261
|
-
if (!mediaSynchronizer) return;
|
|
262
|
-
if (viewOnly === undefined) return;
|
|
263
|
-
if (mediaSynchronizer.viewOnly === viewOnly) return;
|
|
264
|
-
mediaSynchronizer.viewOnly = viewOnly;
|
|
265
|
-
}, [viewOnly, mediaSynchronizer?.viewOnly]);
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Change canSendPositionUpdates when prop changes
|
|
269
|
-
*/
|
|
270
|
-
React.useEffect(() => {
|
|
271
|
-
if (!mediaSynchronizer) return;
|
|
272
|
-
if (canSendPositionUpdates === undefined) return;
|
|
273
|
-
if (
|
|
274
|
-
mediaSynchronizer.mediaSession.coordinator
|
|
275
|
-
.canSendPositionUpdates === canSendPositionUpdates
|
|
276
|
-
)
|
|
277
|
-
return;
|
|
278
|
-
mediaSynchronizer.mediaSession.coordinator.canSendPositionUpdates =
|
|
279
|
-
canSendPositionUpdates;
|
|
280
|
-
}, [
|
|
281
|
-
canSendPositionUpdates,
|
|
282
|
-
mediaSynchronizer?.mediaSession.coordinator.canSendPositionUpdates,
|
|
283
|
-
]);
|
|
284
|
-
|
|
285
|
-
return {
|
|
286
|
-
suspended: !!suspension,
|
|
287
|
-
mediaSynchronizer,
|
|
288
|
-
play,
|
|
289
|
-
pause,
|
|
290
|
-
seekTo,
|
|
291
|
-
setTrack,
|
|
292
|
-
beginSuspension,
|
|
293
|
-
endSuspension,
|
|
294
|
-
};
|
|
295
|
-
}
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the Microsoft Live Share SDK License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
LiveDataObjectInitializeState,
|
|
8
|
+
UserMeetingRole,
|
|
9
|
+
} from "@microsoft/live-share";
|
|
10
|
+
import React from "react";
|
|
11
|
+
import {
|
|
12
|
+
CoordinationWaitPoint,
|
|
13
|
+
ExtendedMediaMetadata,
|
|
14
|
+
IMediaPlayer,
|
|
15
|
+
LiveMediaSession,
|
|
16
|
+
MediaPlayerSynchronizer,
|
|
17
|
+
MediaSessionCoordinatorSuspension,
|
|
18
|
+
} from "@microsoft/live-share-media";
|
|
19
|
+
import { isExtendedMediaMetadata, isMediaElement, isRefObject } from "../utils";
|
|
20
|
+
import { useDynamicDDS } from "../shared-hooks";
|
|
21
|
+
import { IUseMediaSynchronizerResults } from "../types";
|
|
22
|
+
import { useFluidObjectsContext } from "../providers";
|
|
23
|
+
import {
|
|
24
|
+
ActionContainerNotJoinedError,
|
|
25
|
+
ActionLiveDataObjectUndefinedError,
|
|
26
|
+
} from "../internal";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* React hook for using a Live Share media `MediaPlayerSynchronizer`.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* Use this hook if you want to synchronize the playback position of a video or audio element during a Live Share session.
|
|
33
|
+
* This hook can only be used in a child component of `<LiveShareProvider>` or `<AzureProvider>`.
|
|
34
|
+
*
|
|
35
|
+
* @param uniqueKey uniqueKey value for the `LiveMediaSession` DDS object used by `MediaPlayerSynchronizer`.
|
|
36
|
+
* @param mediaPlayerElement React RefObject containing object/element conforming to IMediaPlayer interface, `IMediaPlayer` object, or string id for <video> / <audio> element.
|
|
37
|
+
* @param initialTrack initial track to load. Either ExtendedMediaMetadata, trackId string, or null.
|
|
38
|
+
* @param allowedRoles Optional. Array of user roles that are eligible to modify group playback state.
|
|
39
|
+
* @param viewOnly Optional. Flag for whether or not the media synchronizer should be in viewOnly mode.
|
|
40
|
+
* @param canSendPositionUpdates Optional. Controls whether or not the local client is allowed to send position updates to the group.
|
|
41
|
+
* @returns IUseMediaSynchronizerResults object.
|
|
42
|
+
*/
|
|
43
|
+
export function useMediaSynchronizer(
|
|
44
|
+
uniqueKey: string,
|
|
45
|
+
mediaPlayerElement:
|
|
46
|
+
| React.RefObject<IMediaPlayer>
|
|
47
|
+
| IMediaPlayer
|
|
48
|
+
| string
|
|
49
|
+
| null,
|
|
50
|
+
initialTrack: Partial<ExtendedMediaMetadata> | string | null,
|
|
51
|
+
allowedRoles?: UserMeetingRole[],
|
|
52
|
+
viewOnly?: boolean,
|
|
53
|
+
canSendPositionUpdates?: boolean
|
|
54
|
+
): IUseMediaSynchronizerResults {
|
|
55
|
+
/**
|
|
56
|
+
* User facing media synchronizer and non-user facing setter
|
|
57
|
+
*/
|
|
58
|
+
const [mediaSynchronizer, setMediaSynchronizer] = React.useState<
|
|
59
|
+
MediaPlayerSynchronizer | undefined
|
|
60
|
+
>();
|
|
61
|
+
/**
|
|
62
|
+
* Suspension object and setter
|
|
63
|
+
*/
|
|
64
|
+
const [suspension, setSuspension] = React.useState<
|
|
65
|
+
MediaSessionCoordinatorSuspension | undefined
|
|
66
|
+
>();
|
|
67
|
+
/**
|
|
68
|
+
* User facing: dynamically load the LiveMediaSession DDS for the given unique key.
|
|
69
|
+
*/
|
|
70
|
+
const { dds: mediaSession } = useDynamicDDS<LiveMediaSession>(
|
|
71
|
+
uniqueKey,
|
|
72
|
+
LiveMediaSession
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const { container } = useFluidObjectsContext();
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Play callback
|
|
79
|
+
*/
|
|
80
|
+
const play = React.useCallback(async () => {
|
|
81
|
+
if (!container) {
|
|
82
|
+
throw new ActionContainerNotJoinedError(
|
|
83
|
+
"mediaSynchronizer",
|
|
84
|
+
"play"
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
if (mediaSynchronizer === undefined) {
|
|
88
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
89
|
+
"mediaSynchronizer",
|
|
90
|
+
"play"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return await mediaSynchronizer.play();
|
|
94
|
+
}, [container, mediaSynchronizer]);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Pause callback
|
|
98
|
+
*/
|
|
99
|
+
const pause = React.useCallback(async () => {
|
|
100
|
+
if (!container) {
|
|
101
|
+
throw new ActionContainerNotJoinedError(
|
|
102
|
+
"mediaSynchronizer",
|
|
103
|
+
"pause"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (mediaSynchronizer === undefined) {
|
|
107
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
108
|
+
"mediaSynchronizer",
|
|
109
|
+
"pause"
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return await mediaSynchronizer.pause();
|
|
113
|
+
}, [container, mediaSynchronizer]);
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* User facing: seek callback
|
|
117
|
+
*/
|
|
118
|
+
const seekTo = React.useCallback(
|
|
119
|
+
async (time: number) => {
|
|
120
|
+
if (!container) {
|
|
121
|
+
throw new ActionContainerNotJoinedError(
|
|
122
|
+
"mediaSynchronizer",
|
|
123
|
+
"seekTo"
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
if (mediaSynchronizer === undefined) {
|
|
127
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
128
|
+
"mediaSynchronizer",
|
|
129
|
+
"seekTo"
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return await mediaSynchronizer.seekTo(time);
|
|
133
|
+
},
|
|
134
|
+
[container, mediaSynchronizer]
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* User facing: set track callback
|
|
139
|
+
*/
|
|
140
|
+
const setTrack = React.useCallback(
|
|
141
|
+
async (track: Partial<ExtendedMediaMetadata> | string | null) => {
|
|
142
|
+
if (!container) {
|
|
143
|
+
throw new ActionContainerNotJoinedError(
|
|
144
|
+
"mediaSynchronizer",
|
|
145
|
+
"setTrack"
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
if (mediaSynchronizer === undefined) {
|
|
149
|
+
throw new ActionLiveDataObjectUndefinedError(
|
|
150
|
+
"mediaSynchronizer",
|
|
151
|
+
"setTrack"
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
if (isExtendedMediaMetadata(track)) {
|
|
155
|
+
return await mediaSynchronizer.setTrack(track);
|
|
156
|
+
} else if (typeof track === "string") {
|
|
157
|
+
return await mediaSynchronizer.setTrack({
|
|
158
|
+
trackIdentifier: track,
|
|
159
|
+
} as ExtendedMediaMetadata);
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
[container, mediaSynchronizer]
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* User facing: create a new suspension and set it locally to state
|
|
167
|
+
*/
|
|
168
|
+
const beginSuspension = React.useCallback(
|
|
169
|
+
(waitPoint?: CoordinationWaitPoint) => {
|
|
170
|
+
const suspension =
|
|
171
|
+
mediaSession?.coordinator.beginSuspension(waitPoint);
|
|
172
|
+
setSuspension(suspension);
|
|
173
|
+
},
|
|
174
|
+
[mediaSession]
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* User facing: if a suspension is active, end it. Called when "Follow presenter" button is clicked.
|
|
179
|
+
*/
|
|
180
|
+
const endSuspension = React.useCallback(() => {
|
|
181
|
+
suspension?.end();
|
|
182
|
+
setSuspension(undefined);
|
|
183
|
+
}, [suspension]);
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Setup change listeners and start `LiveMediaSession` if needed
|
|
187
|
+
*/
|
|
188
|
+
React.useEffect(() => {
|
|
189
|
+
if (mediaSession === undefined || !mediaPlayerElement) return;
|
|
190
|
+
let mounted = true;
|
|
191
|
+
// Query the HTML5 media element from the document and set reference
|
|
192
|
+
let mediaPlayer: IMediaPlayer | undefined;
|
|
193
|
+
if (isRefObject<IMediaPlayer>(mediaPlayerElement)) {
|
|
194
|
+
if (!mediaPlayerElement.current) return;
|
|
195
|
+
mediaPlayer = mediaPlayerElement.current;
|
|
196
|
+
} else if (typeof mediaPlayerElement === "string") {
|
|
197
|
+
const mediaElement = document.getElementById(
|
|
198
|
+
mediaPlayerElement
|
|
199
|
+
) as any;
|
|
200
|
+
if (!isMediaElement(mediaElement)) return;
|
|
201
|
+
mediaPlayer = mediaElement;
|
|
202
|
+
} else {
|
|
203
|
+
mediaPlayer = mediaPlayerElement;
|
|
204
|
+
}
|
|
205
|
+
if (initialTrack) {
|
|
206
|
+
// If supported type and not null, set the initial src
|
|
207
|
+
if (isExtendedMediaMetadata(initialTrack)) {
|
|
208
|
+
mediaPlayer.src = initialTrack.trackIdentifier;
|
|
209
|
+
} else if (typeof initialTrack === "string") {
|
|
210
|
+
mediaPlayer.src = initialTrack;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Begin synchronizing a MediaSynchronizer for the player
|
|
214
|
+
const synchronizer = mediaSession.synchronize(mediaPlayer);
|
|
215
|
+
if (viewOnly !== undefined) {
|
|
216
|
+
// Set synchronizer to view only mode provided by developer
|
|
217
|
+
synchronizer.viewOnly = viewOnly;
|
|
218
|
+
}
|
|
219
|
+
if (canSendPositionUpdates !== undefined) {
|
|
220
|
+
synchronizer.mediaSession.coordinator.canSendPositionUpdates =
|
|
221
|
+
canSendPositionUpdates;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (
|
|
225
|
+
mediaSession.initializeState ===
|
|
226
|
+
LiveDataObjectInitializeState.needed
|
|
227
|
+
) {
|
|
228
|
+
// Start synchronizing the media session
|
|
229
|
+
mediaSession.initialize(allowedRoles ?? []);
|
|
230
|
+
} else if (initialTrack) {
|
|
231
|
+
mediaSession.onLocalUserAllowed(async () => {
|
|
232
|
+
if (!mounted) return;
|
|
233
|
+
try {
|
|
234
|
+
if (isExtendedMediaMetadata(initialTrack)) {
|
|
235
|
+
await synchronizer.setTrack(initialTrack);
|
|
236
|
+
} else if (typeof initialTrack === "string") {
|
|
237
|
+
await synchronizer.setTrack({
|
|
238
|
+
trackIdentifier: initialTrack,
|
|
239
|
+
} as ExtendedMediaMetadata);
|
|
240
|
+
}
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.error(err);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
setMediaSynchronizer(synchronizer);
|
|
247
|
+
|
|
248
|
+
return () => {
|
|
249
|
+
mounted = false;
|
|
250
|
+
synchronizer.removeAllListeners();
|
|
251
|
+
mediaSession.removeAllListeners();
|
|
252
|
+
synchronizer?.end();
|
|
253
|
+
mediaSession?.dispose();
|
|
254
|
+
};
|
|
255
|
+
}, [mediaSession, mediaPlayerElement]);
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Change view in media synchronizer only if prop changes
|
|
259
|
+
*/
|
|
260
|
+
React.useEffect(() => {
|
|
261
|
+
if (!mediaSynchronizer) return;
|
|
262
|
+
if (viewOnly === undefined) return;
|
|
263
|
+
if (mediaSynchronizer.viewOnly === viewOnly) return;
|
|
264
|
+
mediaSynchronizer.viewOnly = viewOnly;
|
|
265
|
+
}, [viewOnly, mediaSynchronizer?.viewOnly]);
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Change canSendPositionUpdates when prop changes
|
|
269
|
+
*/
|
|
270
|
+
React.useEffect(() => {
|
|
271
|
+
if (!mediaSynchronizer) return;
|
|
272
|
+
if (canSendPositionUpdates === undefined) return;
|
|
273
|
+
if (
|
|
274
|
+
mediaSynchronizer.mediaSession.coordinator
|
|
275
|
+
.canSendPositionUpdates === canSendPositionUpdates
|
|
276
|
+
)
|
|
277
|
+
return;
|
|
278
|
+
mediaSynchronizer.mediaSession.coordinator.canSendPositionUpdates =
|
|
279
|
+
canSendPositionUpdates;
|
|
280
|
+
}, [
|
|
281
|
+
canSendPositionUpdates,
|
|
282
|
+
mediaSynchronizer?.mediaSession.coordinator.canSendPositionUpdates,
|
|
283
|
+
]);
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
suspended: !!suspension,
|
|
287
|
+
mediaSynchronizer,
|
|
288
|
+
play,
|
|
289
|
+
pause,
|
|
290
|
+
seekTo,
|
|
291
|
+
setTrack,
|
|
292
|
+
beginSuspension,
|
|
293
|
+
endSuspension,
|
|
294
|
+
};
|
|
295
|
+
}
|