-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseSandbox.ts
More file actions
114 lines (89 loc) · 3.44 KB
/
Copy pathuseSandbox.ts
File metadata and controls
114 lines (89 loc) · 3.44 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { useCallback } from "react";
import { MissingSandboxApiUrlError } from "../utils/errors";
type BasicInfo = { id: string; name: string };
type RoomManagerResponse = {
peerToken: string;
url: string;
room: BasicInfo;
peer: BasicInfo;
};
export type UseSandboxProps = {
sandboxApiUrl: string;
};
export type RoomType = "conference" | "livestream" | "audio_only";
export const useSandbox = (props: UseSandboxProps) => {
const sandboxApiUrl = props?.sandboxApiUrl;
const getSandboxPeerToken = useCallback(
async (roomName: string, peerName: string, roomType: RoomType = "conference") => {
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();
const url = new URL(sandboxApiUrl);
url.searchParams.set("roomName", roomName);
url.searchParams.set("peerName", peerName);
url.searchParams.set("roomType", roomType);
const res = await fetch(url);
if (!res.ok) {
const message = `Failed to retrieve peer token for peer '${peerName}' in ${roomType} room '${roomName}'.`;
throw new Error(message);
}
const data: RoomManagerResponse = await res.json();
return data.peerToken;
},
[sandboxApiUrl],
);
const getSandboxViewerToken = useCallback(
async (roomName: string) => {
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();
const url = new URL(`${sandboxApiUrl}/${roomName}/livestream-viewer-token`);
const res = await fetch(url);
if (!res.ok) {
let message = `Failed to retrieve viewer token for '${roomName}' livestream room.`;
if (res.status === 404) {
message = `A livestream room of name '${roomName}' does not exist.`;
}
throw new Error(message);
}
const data: { token: string } = await res.json();
return data.token;
},
[sandboxApiUrl],
);
const getSandboxLivestream = useCallback(
async (roomName: string, isPublic: boolean = false) => {
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();
const url = new URL(`${sandboxApiUrl}/livestream`);
url.searchParams.set("roomName", roomName);
url.searchParams.set("public", isPublic.toString());
const res = await fetch(url);
if (!res.ok) throw new Error(`Failed to retrieve streamer token for '${roomName}' livestream room.`);
const data: { streamerToken: string; room: { id: string; name: string } } = await res.json();
return data;
},
[sandboxApiUrl],
);
const fetchMoqToken = useCallback(
async (streamName: string, type: "subscriber" | "publisher") => {
if (!sandboxApiUrl) throw new MissingSandboxApiUrlError();
const urlEncodedStreamName = encodeURIComponent(streamName);
const res = await fetch(`${sandboxApiUrl}/moq/${urlEncodedStreamName}/${type}`);
if (!res.ok) throw new Error(`Failed to retrieve MoQ ${type} token for stream '${streamName}'.`);
const data: { token: string } = await res.json();
return data.token;
},
[sandboxApiUrl],
);
const getSandboxMoqPublisherToken = useCallback(
async (streamName: string) => fetchMoqToken(streamName, "publisher"),
[fetchMoqToken],
);
const getSandboxMoqSubscriberToken = useCallback(
async (streamName: string) => fetchMoqToken(streamName, "subscriber"),
[fetchMoqToken],
);
return {
getSandboxPeerToken,
getSandboxViewerToken,
getSandboxLivestream,
getSandboxMoqPublisherToken,
getSandboxMoqSubscriberToken,
};
};