-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel-streaming-wrapper.tsx
More file actions
83 lines (69 loc) · 2.43 KB
/
pixel-streaming-wrapper.tsx
File metadata and controls
83 lines (69 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useEffect, useRef, useState } from "react";
import {
type AllSettings,
Config,
PixelStreaming,
} from "@epicgames-ps/lib-pixelstreamingfrontend-ue5.6";
export interface PixelStreamingWrapperProps {
initialSettings?: Partial<AllSettings>;
}
const PixelStreamingWrapper = ({
initialSettings,
}: PixelStreamingWrapperProps) => {
// A reference to parent div element that the Pixel Streaming library attaches into:
const videoParent = useRef<HTMLDivElement>(null);
// Pixel streaming library instance is stored into this state variable after initialization:
const [pixelStreaming, setPixelStreaming] = useState<PixelStreaming>();
// A boolean state variable that determines if the Click to play overlay is shown:
const [clickToPlayVisible, setClickToPlayVisible] = useState(true);
// Run on component mount:
useEffect(() => {
if (!videoParent.current) return;
// Attach Pixel Streaming library to videoParent element:
const config = new Config({ initialSettings });
const streaming = new PixelStreaming(config, {
videoElementParent: videoParent.current,
});
const onPlayStreamRejected = () => {
setClickToPlayVisible(true);
};
// register a playStreamRejected handler to show Click to play overlay if needed:
streaming.addEventListener("playStreamRejected", onPlayStreamRejected);
// Save the library instance into component state so that it can be accessed later:
setPixelStreaming(streaming);
// Clean up on component unmount:
return () => {
try {
streaming.removeEventListener(
"playStreamRejected",
onPlayStreamRejected
);
streaming.disconnect();
} catch (error) {
if (error instanceof Error) {
console.error("Error during Pixel Streaming cleanup:", error.message);
} else {
console.error(error);
}
}
};
}, []);
function handleClickToPlay() {
pixelStreaming?.play();
setClickToPlayVisible(false);
}
return (
<div className="relative h-full w-full ">
<div className="w-full h-full" ref={videoParent} />
{clickToPlayVisible && (
<div
className="w-full h-full top-0 left-0 absolute flex items-center justify-center pointer"
onClick={handleClickToPlay}
>
<button className="text-foreground">Click to play</button>
</div>
)}
</div>
);
};
export default PixelStreamingWrapper;