@streamplace/components 0.0.1 → 0.6.37
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/LICENSE +18 -0
- package/README.md +35 -0
- package/dist/index.js +6 -0
- package/dist/livestream-provider/index.js +20 -0
- package/dist/livestream-provider/websocket.js +41 -0
- package/dist/livestream-store/chat.js +162 -0
- package/dist/livestream-store/context.js +2 -0
- package/dist/livestream-store/index.js +3 -0
- package/dist/livestream-store/livestream-state.js +1 -0
- package/dist/livestream-store/livestream-store.js +39 -0
- package/dist/livestream-store/websocket-consumer.js +55 -0
- package/dist/player-store/context.js +2 -0
- package/dist/player-store/index.js +6 -0
- package/dist/player-store/player-provider.js +53 -0
- package/dist/player-store/player-state.js +22 -0
- package/dist/player-store/player-store.js +146 -0
- package/dist/player-store/single-player-provider.js +109 -0
- package/dist/streamplace-provider/context.js +2 -0
- package/dist/streamplace-provider/index.js +16 -0
- package/dist/streamplace-provider/poller.js +46 -0
- package/dist/streamplace-provider/xrpc.js +0 -0
- package/dist/streamplace-store/index.js +2 -0
- package/dist/streamplace-store/streamplace-store.js +37 -0
- package/dist/streamplace-store/user.js +47 -0
- package/dist/streamplace-store/xrpc.js +12 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/37be0eec +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/56540125 +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/67b1eb60 +0 -0
- package/node-compile-cache/v22.15.0-x64-efe9a9df-0/7c275f90 +0 -0
- package/package.json +34 -8
- package/src/index.tsx +6 -0
- package/src/livestream-provider/index.tsx +37 -0
- package/src/livestream-provider/websocket.tsx +47 -0
- package/src/livestream-store/chat.tsx +224 -0
- package/src/livestream-store/context.tsx +10 -0
- package/src/livestream-store/index.tsx +3 -0
- package/src/livestream-store/livestream-state.tsx +18 -0
- package/src/livestream-store/livestream-store.tsx +56 -0
- package/src/livestream-store/websocket-consumer.tsx +62 -0
- package/src/player-store/context.tsx +11 -0
- package/src/player-store/index.tsx +6 -0
- package/src/player-store/player-provider.tsx +90 -0
- package/src/player-store/player-state.tsx +159 -0
- package/src/player-store/player-store.tsx +217 -0
- package/src/player-store/single-player-provider.tsx +181 -0
- package/src/streamplace-provider/context.tsx +10 -0
- package/src/streamplace-provider/index.tsx +32 -0
- package/src/streamplace-provider/poller.tsx +55 -0
- package/src/streamplace-provider/xrpc.tsx +0 -0
- package/src/streamplace-store/index.tsx +2 -0
- package/src/streamplace-store/streamplace-store.tsx +89 -0
- package/src/streamplace-store/user.tsx +57 -0
- package/src/streamplace-store/xrpc.tsx +15 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AppBskyActorDefs } from "@atproto/api";
|
|
2
|
+
import {
|
|
3
|
+
ChatMessageViewHydrated,
|
|
4
|
+
LivestreamViewHydrated,
|
|
5
|
+
PlaceStreamDefs,
|
|
6
|
+
PlaceStreamSegment,
|
|
7
|
+
} from "streamplace";
|
|
8
|
+
|
|
9
|
+
export interface LivestreamState {
|
|
10
|
+
profile: AppBskyActorDefs.ProfileViewBasic | null;
|
|
11
|
+
chatIndex: { [key: string]: ChatMessageViewHydrated };
|
|
12
|
+
chat: ChatMessageViewHydrated[];
|
|
13
|
+
livestream: LivestreamViewHydrated | null;
|
|
14
|
+
viewers: number | null;
|
|
15
|
+
segment: PlaceStreamSegment.Record | null;
|
|
16
|
+
renditions: PlaceStreamDefs.Rendition[];
|
|
17
|
+
replyToMessage: ChatMessageViewHydrated | null;
|
|
18
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { createStore, StoreApi, useStore } from "zustand";
|
|
3
|
+
import { LivestreamContext } from "./context";
|
|
4
|
+
import { LivestreamState } from "./livestream-state";
|
|
5
|
+
import { handleWebSocketMessages } from "./websocket-consumer";
|
|
6
|
+
|
|
7
|
+
export type LivestreamStore = StoreApi<LivestreamState>;
|
|
8
|
+
|
|
9
|
+
export const makeLivestreamStore = (): StoreApi<LivestreamState> => {
|
|
10
|
+
return createStore<LivestreamState>()((set) => ({
|
|
11
|
+
profile: null,
|
|
12
|
+
chatIndex: {},
|
|
13
|
+
chat: [],
|
|
14
|
+
livestream: null,
|
|
15
|
+
viewers: null,
|
|
16
|
+
segment: null,
|
|
17
|
+
renditions: [],
|
|
18
|
+
replyToMessage: null,
|
|
19
|
+
}));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export function getStoreFromContext(): LivestreamStore {
|
|
23
|
+
const context = useContext(LivestreamContext);
|
|
24
|
+
if (!context) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"useLivestreamStore must be used within a LivestreamProvider",
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return context.store;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useLivestreamStore<U>(
|
|
33
|
+
selector: (state: LivestreamState) => U,
|
|
34
|
+
): U {
|
|
35
|
+
const store = getStoreFromContext();
|
|
36
|
+
return useStore(store, selector);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const useHandleWebsocketMessages = () => {
|
|
40
|
+
const store = getStoreFromContext();
|
|
41
|
+
return (messages: any[]) => {
|
|
42
|
+
store.setState((state) => handleWebSocketMessages(state, messages));
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const useChat = () => useLivestreamStore((x) => x.chat);
|
|
47
|
+
|
|
48
|
+
export const useProfile = () => useLivestreamStore((x) => x.profile);
|
|
49
|
+
|
|
50
|
+
export const useViewers = () => useLivestreamStore((x) => x.viewers);
|
|
51
|
+
|
|
52
|
+
export const useLivestream = () => useLivestreamStore((x) => x.livestream);
|
|
53
|
+
|
|
54
|
+
export const useSegment = () => useLivestreamStore((x) => x.segment);
|
|
55
|
+
|
|
56
|
+
export const useRenditions = () => useLivestreamStore((x) => x.renditions);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AppBskyActorDefs } from "@atproto/api";
|
|
2
|
+
import {
|
|
3
|
+
ChatMessageViewHydrated,
|
|
4
|
+
LivestreamViewHydrated,
|
|
5
|
+
PlaceStreamChatDefs,
|
|
6
|
+
PlaceStreamChatMessage,
|
|
7
|
+
PlaceStreamDefs,
|
|
8
|
+
PlaceStreamLivestream,
|
|
9
|
+
PlaceStreamSegment,
|
|
10
|
+
} from "streamplace";
|
|
11
|
+
import { reduceChat } from "./chat";
|
|
12
|
+
import { LivestreamState } from "./livestream-state";
|
|
13
|
+
|
|
14
|
+
export const handleWebSocketMessages = (
|
|
15
|
+
state: LivestreamState,
|
|
16
|
+
messages: any[],
|
|
17
|
+
): LivestreamState => {
|
|
18
|
+
for (const message of messages) {
|
|
19
|
+
if (PlaceStreamLivestream.isLivestreamView(message)) {
|
|
20
|
+
state = {
|
|
21
|
+
...state,
|
|
22
|
+
livestream: message as LivestreamViewHydrated,
|
|
23
|
+
};
|
|
24
|
+
} else if (PlaceStreamLivestream.isViewerCount(message)) {
|
|
25
|
+
state = {
|
|
26
|
+
...state,
|
|
27
|
+
viewers: message.count,
|
|
28
|
+
};
|
|
29
|
+
} else if (PlaceStreamChatDefs.isMessageView(message)) {
|
|
30
|
+
// Explicitly map MessageView to MessageViewHydrated
|
|
31
|
+
const hydrated: ChatMessageViewHydrated = {
|
|
32
|
+
uri: message.uri,
|
|
33
|
+
cid: message.cid,
|
|
34
|
+
author: message.author,
|
|
35
|
+
record: message.record as PlaceStreamChatMessage.Record,
|
|
36
|
+
indexedAt: message.indexedAt,
|
|
37
|
+
chatProfile: (message as any).chatProfile,
|
|
38
|
+
replyTo: (message as any).replyTo,
|
|
39
|
+
};
|
|
40
|
+
state = reduceChat(state, [hydrated], []);
|
|
41
|
+
} else if (PlaceStreamSegment.isRecord(message)) {
|
|
42
|
+
state = {
|
|
43
|
+
...state,
|
|
44
|
+
segment: message as PlaceStreamSegment.Record,
|
|
45
|
+
};
|
|
46
|
+
} else if (PlaceStreamDefs.isBlockView(message)) {
|
|
47
|
+
const block = message as PlaceStreamDefs.BlockView;
|
|
48
|
+
state = reduceChat(state, [], [block]);
|
|
49
|
+
} else if (PlaceStreamDefs.isRenditions(message)) {
|
|
50
|
+
state = {
|
|
51
|
+
...state,
|
|
52
|
+
renditions: message.renditions,
|
|
53
|
+
};
|
|
54
|
+
} else if (AppBskyActorDefs.isProfileViewBasic(message)) {
|
|
55
|
+
state = {
|
|
56
|
+
...state,
|
|
57
|
+
profile: message,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return reduceChat(state, [], []);
|
|
62
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import { StoreApi } from "zustand";
|
|
3
|
+
import { PlayerState } from "./player-state";
|
|
4
|
+
|
|
5
|
+
type PlayerContextType = {
|
|
6
|
+
players: Record<string, StoreApi<PlayerState>>;
|
|
7
|
+
createPlayer: (id?: string) => string;
|
|
8
|
+
removePlayer: (id: string) => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const PlayerContext = createContext<PlayerContextType | null>(null);
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import React, { useCallback, useMemo, useState } from "react";
|
|
2
|
+
import { StoreApi } from "zustand";
|
|
3
|
+
import { PlayerContext } from "./context";
|
|
4
|
+
import { PlayerState } from "./player-state";
|
|
5
|
+
import { makePlayerStore } from "./player-store";
|
|
6
|
+
|
|
7
|
+
interface PlayerProviderProps {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
initialPlayers?: string[];
|
|
10
|
+
defaultId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const PlayerProvider: React.FC<PlayerProviderProps> = ({
|
|
14
|
+
children,
|
|
15
|
+
initialPlayers = [],
|
|
16
|
+
defaultId = Math.random().toString(36).slice(8),
|
|
17
|
+
}) => {
|
|
18
|
+
const [players, setPlayers] = useState<Record<string, StoreApi<PlayerState>>>(
|
|
19
|
+
() => {
|
|
20
|
+
// Initialize with any initial player IDs provided
|
|
21
|
+
const initialPlayerStores: Record<string, StoreApi<PlayerState>> = {};
|
|
22
|
+
for (const playerId of initialPlayers) {
|
|
23
|
+
initialPlayerStores[playerId] = makePlayerStore(playerId);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Always create at least one player by default
|
|
27
|
+
if (initialPlayers.length === 0) {
|
|
28
|
+
initialPlayerStores[defaultId] = makePlayerStore(defaultId);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return initialPlayerStores;
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const createPlayer = useCallback((id?: string) => {
|
|
36
|
+
console.log("Creating new player");
|
|
37
|
+
const playerId = id || Math.random().toString(36).slice(8);
|
|
38
|
+
const playerStore = makePlayerStore(playerId);
|
|
39
|
+
|
|
40
|
+
setPlayers((prev) => ({
|
|
41
|
+
...prev,
|
|
42
|
+
[playerId]: playerStore,
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
return playerId;
|
|
46
|
+
}, []);
|
|
47
|
+
|
|
48
|
+
const removePlayer = useCallback((id: string) => {
|
|
49
|
+
setPlayers((prev) => {
|
|
50
|
+
// Don't remove the last player
|
|
51
|
+
if (Object.keys(prev).length <= 1) {
|
|
52
|
+
console.warn("Cannot remove the last player");
|
|
53
|
+
return prev;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const newPlayers = { ...prev };
|
|
57
|
+
delete newPlayers[id];
|
|
58
|
+
return newPlayers;
|
|
59
|
+
});
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
const contextValue = useMemo(
|
|
63
|
+
() => ({
|
|
64
|
+
players,
|
|
65
|
+
createPlayer,
|
|
66
|
+
removePlayer,
|
|
67
|
+
}),
|
|
68
|
+
[players, createPlayer, removePlayer],
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<PlayerContext.Provider value={contextValue}>
|
|
73
|
+
{children}
|
|
74
|
+
</PlayerContext.Provider>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// HOC to wrap components that need player context
|
|
79
|
+
export function withPlayerProvider<P extends object>(
|
|
80
|
+
Component: React.ComponentType<P>,
|
|
81
|
+
): React.FC<P & { initialPlayers?: string[] }> {
|
|
82
|
+
return function WithPlayerProvider(props: P & { initialPlayers?: string[] }) {
|
|
83
|
+
const { initialPlayers, ...componentProps } = props;
|
|
84
|
+
return (
|
|
85
|
+
<PlayerProvider initialPlayers={initialPlayers}>
|
|
86
|
+
<Component {...(componentProps as P)} />
|
|
87
|
+
</PlayerProvider>
|
|
88
|
+
);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
export enum PlayerProtocol {
|
|
2
|
+
WEBRTC = "webrtc",
|
|
3
|
+
HLS = "hls",
|
|
4
|
+
PROGRESSIVE_MP4 = "progressive-mp4",
|
|
5
|
+
PROGRESSIVE_WEBM = "progressive-webm",
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export enum PlayerStatus {
|
|
9
|
+
START = "start",
|
|
10
|
+
PLAYING = "playing",
|
|
11
|
+
STALLED = "stalled",
|
|
12
|
+
SUSPEND = "suspend",
|
|
13
|
+
WAITING = "waiting",
|
|
14
|
+
PAUSE = "pause",
|
|
15
|
+
MUTE = "mute",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type PlayerStatusTracker = Partial<Record<PlayerStatus, number>>;
|
|
19
|
+
|
|
20
|
+
export enum IngestMediaSource {
|
|
21
|
+
USER = "user",
|
|
22
|
+
DISPLAY = "display",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface PlayerState {
|
|
26
|
+
id: string;
|
|
27
|
+
selectedRendition: string;
|
|
28
|
+
setSelectedRendition: (rendition: string) => void;
|
|
29
|
+
protocol: PlayerProtocol;
|
|
30
|
+
setProtocol: (protocol: PlayerProtocol) => void;
|
|
31
|
+
|
|
32
|
+
/** Source */
|
|
33
|
+
src: string;
|
|
34
|
+
|
|
35
|
+
/** Function to set the source URL */
|
|
36
|
+
setSrc: (src: string) => void;
|
|
37
|
+
|
|
38
|
+
/** Flag indicating if ingest (stream input) is currently starting */
|
|
39
|
+
ingestStarting: boolean;
|
|
40
|
+
|
|
41
|
+
/** Function to set the ingestStarting flag */
|
|
42
|
+
setIngestStarting: (ingestStarting: boolean) => void;
|
|
43
|
+
|
|
44
|
+
/** Current connection state of ingest RTP/RTC peer connection */
|
|
45
|
+
ingestConnectionState: RTCPeerConnectionState | null;
|
|
46
|
+
|
|
47
|
+
/** Function to update the ingest connection state */
|
|
48
|
+
setIngestConnectionState: (state: RTCPeerConnectionState | null) => void;
|
|
49
|
+
|
|
50
|
+
ingestMediaSource?: IngestMediaSource;
|
|
51
|
+
setIngestMediaSource?: (source: IngestMediaSource) => void;
|
|
52
|
+
|
|
53
|
+
ingestAutoStart?: boolean;
|
|
54
|
+
setIngestAutoStart?: (autoStart: boolean) => void;
|
|
55
|
+
|
|
56
|
+
/** Timestamp (number) when ingest started, or null if not started */
|
|
57
|
+
ingestStarted: number | null;
|
|
58
|
+
|
|
59
|
+
/** Function to set the ingestStarted timestamp */
|
|
60
|
+
setIngestStarted: (timestamp: number | null) => void;
|
|
61
|
+
|
|
62
|
+
/** Player muted state */
|
|
63
|
+
muted: boolean;
|
|
64
|
+
|
|
65
|
+
/** Function to set the muted state */
|
|
66
|
+
setMuted: (isMuted: boolean) => void;
|
|
67
|
+
|
|
68
|
+
/** Player volume level (0.0 to 1.0) */
|
|
69
|
+
volume: number;
|
|
70
|
+
|
|
71
|
+
/** Function to set the volume level */
|
|
72
|
+
setVolume: (volume: number) => void;
|
|
73
|
+
|
|
74
|
+
/** Player fullscreen state */
|
|
75
|
+
fullscreen: boolean;
|
|
76
|
+
|
|
77
|
+
/** Function to set the fullscreen state */
|
|
78
|
+
setFullscreen: (isFullscreen: boolean) => void;
|
|
79
|
+
|
|
80
|
+
/** Current player status */
|
|
81
|
+
status: PlayerStatus;
|
|
82
|
+
|
|
83
|
+
/** Function to update the player status */
|
|
84
|
+
setStatus: (status: PlayerStatus) => void;
|
|
85
|
+
|
|
86
|
+
/** Current playback time in seconds */
|
|
87
|
+
playTime: number;
|
|
88
|
+
|
|
89
|
+
/** Function to set the current playback time */
|
|
90
|
+
setPlayTime: (playTime: number) => void;
|
|
91
|
+
|
|
92
|
+
/** Flag indicating if player is in offline state */
|
|
93
|
+
offline: boolean;
|
|
94
|
+
|
|
95
|
+
/** Function to set the offline state */
|
|
96
|
+
setOffline: (offline: boolean) => void;
|
|
97
|
+
/** Reference to the video element for direct manipulation (used for PiP) */
|
|
98
|
+
videoRef:
|
|
99
|
+
| React.MutableRefObject<HTMLVideoElement | null>
|
|
100
|
+
| ((instance: HTMLVideoElement | null) => void)
|
|
101
|
+
| null
|
|
102
|
+
| undefined;
|
|
103
|
+
|
|
104
|
+
/** Function to set the video reference */
|
|
105
|
+
setVideoRef: (
|
|
106
|
+
videoRef:
|
|
107
|
+
| React.MutableRefObject<HTMLVideoElement | null>
|
|
108
|
+
| ((instance: HTMLVideoElement | null) => void)
|
|
109
|
+
| null
|
|
110
|
+
| undefined,
|
|
111
|
+
) => void;
|
|
112
|
+
|
|
113
|
+
/** Flag indicating if player is in Picture-in-Picture mode */
|
|
114
|
+
pipMode: boolean;
|
|
115
|
+
|
|
116
|
+
/** Function to set the Picture-in-Picture mode */
|
|
117
|
+
setPipMode: (pipMode: boolean) => void;
|
|
118
|
+
|
|
119
|
+
/** Flag indicating if mute was forced by system (e.g., autoplay policy) */
|
|
120
|
+
muteWasForced: boolean;
|
|
121
|
+
|
|
122
|
+
/** Function to set the muteWasForced flag */
|
|
123
|
+
setMuteWasForced: (muteWasForced: boolean) => void;
|
|
124
|
+
|
|
125
|
+
/** Flag indicating if the player is embedded in another context */
|
|
126
|
+
embedded: boolean;
|
|
127
|
+
|
|
128
|
+
/** Function to set the embedded flag */
|
|
129
|
+
setEmbedded: (embedded: boolean) => void;
|
|
130
|
+
|
|
131
|
+
/** Flag indicating if player controls should be shown */
|
|
132
|
+
showControls: boolean;
|
|
133
|
+
controlsTimeout?: NodeJS.Timeout | undefined;
|
|
134
|
+
|
|
135
|
+
/** Function to set the showControls flag */
|
|
136
|
+
setShowControls: (showControls: boolean) => void;
|
|
137
|
+
|
|
138
|
+
telemetry: boolean;
|
|
139
|
+
setTelemetry: (telemetry: boolean) => void;
|
|
140
|
+
|
|
141
|
+
playerEvent: (
|
|
142
|
+
url: string,
|
|
143
|
+
time: string,
|
|
144
|
+
eventType: string,
|
|
145
|
+
meta: { [key: string]: any },
|
|
146
|
+
) => void;
|
|
147
|
+
|
|
148
|
+
clearControlsTimeout: () => void;
|
|
149
|
+
|
|
150
|
+
setUserInteraction: () => void;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export type PlayerEvent = {
|
|
154
|
+
id?: string;
|
|
155
|
+
time: string;
|
|
156
|
+
playerId: string;
|
|
157
|
+
eventType: string;
|
|
158
|
+
meta: { [key: string]: any };
|
|
159
|
+
};
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { createStore, StoreApi, useStore } from "zustand";
|
|
3
|
+
import { PlayerContext } from "./context";
|
|
4
|
+
import {
|
|
5
|
+
IngestMediaSource,
|
|
6
|
+
PlayerEvent,
|
|
7
|
+
PlayerProtocol,
|
|
8
|
+
PlayerState,
|
|
9
|
+
PlayerStatus,
|
|
10
|
+
} from "./player-state";
|
|
11
|
+
|
|
12
|
+
export type PlayerStore = StoreApi<PlayerState>;
|
|
13
|
+
|
|
14
|
+
export const makePlayerStore = (id?: string): StoreApi<PlayerState> => {
|
|
15
|
+
return createStore<PlayerState>()((set) => ({
|
|
16
|
+
id: id || Math.random().toString(36).slice(8),
|
|
17
|
+
selectedRendition: "source",
|
|
18
|
+
setSelectedRendition: (rendition: string) =>
|
|
19
|
+
set((state) => ({ ...state, selectedRendition: rendition })),
|
|
20
|
+
protocol: PlayerProtocol.WEBRTC,
|
|
21
|
+
setProtocol: (protocol: PlayerProtocol) =>
|
|
22
|
+
set((state) => ({ ...state, protocol: protocol })),
|
|
23
|
+
|
|
24
|
+
src: "",
|
|
25
|
+
setSrc: (src: string) => set(() => ({ src })),
|
|
26
|
+
|
|
27
|
+
ingestStarting: false,
|
|
28
|
+
setIngestStarting: (ingestStarting: boolean) =>
|
|
29
|
+
set(() => ({ ingestStarting })),
|
|
30
|
+
|
|
31
|
+
ingestMediaSource: undefined,
|
|
32
|
+
setIngestMediaSource: (ingestMediaSource: IngestMediaSource | undefined) =>
|
|
33
|
+
set(() => ({ ingestMediaSource })),
|
|
34
|
+
|
|
35
|
+
ingestConnectionState: null,
|
|
36
|
+
setIngestConnectionState: (
|
|
37
|
+
ingestConnectionState: RTCPeerConnectionState | null,
|
|
38
|
+
) => set(() => ({ ingestConnectionState })),
|
|
39
|
+
|
|
40
|
+
ingestAutoStart: false,
|
|
41
|
+
setIngestAutoStart: (ingestAutoStart: boolean) =>
|
|
42
|
+
set(() => ({ ingestAutoStart })),
|
|
43
|
+
|
|
44
|
+
ingestStarted: null,
|
|
45
|
+
setIngestStarted: (timestamp: number | null) =>
|
|
46
|
+
set(() => ({ ingestStarted: timestamp })),
|
|
47
|
+
|
|
48
|
+
muted: false,
|
|
49
|
+
setMuted: (isMuted: boolean) =>
|
|
50
|
+
set(() => ({ muted: isMuted, muteWasForced: false })),
|
|
51
|
+
|
|
52
|
+
volume: 1.0,
|
|
53
|
+
setVolume: (volume: number) =>
|
|
54
|
+
set(() => ({ volume, muteWasForced: false })),
|
|
55
|
+
|
|
56
|
+
fullscreen: false,
|
|
57
|
+
setFullscreen: (isFullscreen: boolean) =>
|
|
58
|
+
set(() => ({ fullscreen: isFullscreen })),
|
|
59
|
+
|
|
60
|
+
status: PlayerStatus.START,
|
|
61
|
+
setStatus: (status: PlayerStatus) => set(() => ({ status })),
|
|
62
|
+
|
|
63
|
+
playTime: 0,
|
|
64
|
+
setPlayTime: (playTime: number) => set(() => ({ playTime })),
|
|
65
|
+
|
|
66
|
+
offline: false,
|
|
67
|
+
setOffline: (offline: boolean) => set(() => ({ offline })),
|
|
68
|
+
|
|
69
|
+
videoRef: undefined,
|
|
70
|
+
setVideoRef: (
|
|
71
|
+
videoRef:
|
|
72
|
+
| React.MutableRefObject<HTMLVideoElement | null>
|
|
73
|
+
| ((instance: HTMLVideoElement | null) => void)
|
|
74
|
+
| null
|
|
75
|
+
| undefined,
|
|
76
|
+
) => set(() => ({ videoRef })),
|
|
77
|
+
|
|
78
|
+
pipMode: false,
|
|
79
|
+
setPipMode: (pipMode: boolean) => set(() => ({ pipMode })),
|
|
80
|
+
|
|
81
|
+
// * Whether mute was forced by the browser or not for autoplay
|
|
82
|
+
// * Will get set to 'false' if the user has interacted with the volume
|
|
83
|
+
muteWasForced: false,
|
|
84
|
+
setMuteWasForced: (muteWasForced: boolean) =>
|
|
85
|
+
set(() => ({ muteWasForced })),
|
|
86
|
+
|
|
87
|
+
embedded: false,
|
|
88
|
+
setEmbedded: (embedded: boolean) => set(() => ({ embedded })),
|
|
89
|
+
|
|
90
|
+
showControls: true,
|
|
91
|
+
controlsTimeout: undefined,
|
|
92
|
+
setShowControls: (showControls: boolean) =>
|
|
93
|
+
set({ showControls, controlsTimeout: undefined }),
|
|
94
|
+
|
|
95
|
+
telemetry: true,
|
|
96
|
+
setTelemetry: (telemetry: boolean) => set(() => ({ telemetry })),
|
|
97
|
+
|
|
98
|
+
playerEvent: async (
|
|
99
|
+
url: string,
|
|
100
|
+
time: string,
|
|
101
|
+
eventType: string,
|
|
102
|
+
meta: { [key: string]: any },
|
|
103
|
+
) =>
|
|
104
|
+
set((x) => {
|
|
105
|
+
const data: PlayerEvent = {
|
|
106
|
+
time: time,
|
|
107
|
+
playerId: x.id,
|
|
108
|
+
eventType: eventType,
|
|
109
|
+
meta: {
|
|
110
|
+
...meta,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
// fetch url from sp provider
|
|
115
|
+
fetch(`${url}/api/player-event`, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
body: JSON.stringify(data),
|
|
118
|
+
});
|
|
119
|
+
} catch (e) {
|
|
120
|
+
console.error("error sending player telemetry", e);
|
|
121
|
+
}
|
|
122
|
+
return {};
|
|
123
|
+
}),
|
|
124
|
+
|
|
125
|
+
// Clear the controls timeout, if it exists.
|
|
126
|
+
// Should be called on player unmount.
|
|
127
|
+
clearControlsTimeout: () =>
|
|
128
|
+
set((state) => {
|
|
129
|
+
if (state.controlsTimeout) {
|
|
130
|
+
clearTimeout(state.controlsTimeout);
|
|
131
|
+
}
|
|
132
|
+
return { controlsTimeout: undefined };
|
|
133
|
+
}),
|
|
134
|
+
|
|
135
|
+
setUserInteraction: () =>
|
|
136
|
+
set((p) => {
|
|
137
|
+
// controls timeout
|
|
138
|
+
if (p.controlsTimeout) {
|
|
139
|
+
clearTimeout(p.controlsTimeout);
|
|
140
|
+
}
|
|
141
|
+
let controlsTimeout = setTimeout(() => p.setShowControls(false), 1000);
|
|
142
|
+
return { showControls: true, controlsTimeout };
|
|
143
|
+
}),
|
|
144
|
+
}));
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export function usePlayerContext() {
|
|
148
|
+
const context = useContext(PlayerContext);
|
|
149
|
+
if (!context) {
|
|
150
|
+
throw new Error("usePlayerContext must be used within a PlayerProvider");
|
|
151
|
+
}
|
|
152
|
+
return context;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Get a specific player store by ID
|
|
156
|
+
export function getPlayerStoreById(id: string): PlayerStore {
|
|
157
|
+
const { players } = usePlayerContext();
|
|
158
|
+
const playerStore = players[id];
|
|
159
|
+
if (!playerStore) {
|
|
160
|
+
throw new Error(`No player found with ID: ${id}`);
|
|
161
|
+
}
|
|
162
|
+
return playerStore;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Will get the first player ID in the context
|
|
166
|
+
export function getFirstPlayerID(): string {
|
|
167
|
+
const { players } = usePlayerContext();
|
|
168
|
+
const playerIds = Object.keys(players);
|
|
169
|
+
if (playerIds.length === 0) {
|
|
170
|
+
throw new Error("No players found in context");
|
|
171
|
+
}
|
|
172
|
+
return playerIds[0];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function getPlayerStoreFromContext(): PlayerStore {
|
|
176
|
+
console.warn(
|
|
177
|
+
"getPlayerStoreFromContext is deprecated. Use getPlayerStoreById instead.",
|
|
178
|
+
);
|
|
179
|
+
const { players } = usePlayerContext();
|
|
180
|
+
const playerIds = Object.keys(players);
|
|
181
|
+
if (playerIds.length === 0) {
|
|
182
|
+
throw new Error("No players found in context");
|
|
183
|
+
}
|
|
184
|
+
return players[playerIds[0]];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Use a specific player store by ID
|
|
188
|
+
// If no ID is provided, it will use the first player in the context
|
|
189
|
+
export function usePlayerStore<U>(
|
|
190
|
+
selector: (state: PlayerState) => U,
|
|
191
|
+
playerId?: string,
|
|
192
|
+
): U {
|
|
193
|
+
if (!playerId) {
|
|
194
|
+
playerId = Object.keys(usePlayerContext().players)[0];
|
|
195
|
+
}
|
|
196
|
+
const store = getPlayerStoreById(playerId);
|
|
197
|
+
return useStore(store, selector);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* Convenience selectors/hooks */
|
|
201
|
+
export const usePlayerProtocol = (
|
|
202
|
+
playerId?: string,
|
|
203
|
+
): [PlayerProtocol, (protocol: PlayerProtocol) => void] =>
|
|
204
|
+
usePlayerStore((x) => [x.protocol, x.setProtocol], playerId);
|
|
205
|
+
|
|
206
|
+
export const intoPlayerProtocol = (protocol: string): PlayerProtocol => {
|
|
207
|
+
switch (protocol) {
|
|
208
|
+
case "hls":
|
|
209
|
+
return PlayerProtocol.HLS;
|
|
210
|
+
case "progressive-mp4":
|
|
211
|
+
return PlayerProtocol.PROGRESSIVE_MP4;
|
|
212
|
+
case "progressive-webm":
|
|
213
|
+
return PlayerProtocol.PROGRESSIVE_WEBM;
|
|
214
|
+
default:
|
|
215
|
+
return PlayerProtocol.WEBRTC;
|
|
216
|
+
}
|
|
217
|
+
};
|