Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
207 lines (173 loc) · 4.9 KB
/
Copy pathindex.ts
File metadata and controls
207 lines (173 loc) · 4.9 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import EventEmitter from "node:events";
import WebSocket, { type RawData } from "ws";
import { createLogger } from "../../util/log.js";
import type { RTCIceCandidate } from "../util/validation/receive.js";
import type {
JANUS_REQUEST,
JANUS_RESPONSE,
JANUS_RESPONSE_DATA,
RESPONSE_ATTACH_HANDLE,
RESPONSE_CONFIGURE,
RESPONSE_CREATE_ROOM,
RESPONSE_CREATE_SESSION,
RESPONSE_JOIN_ROOM,
RESPONSE_LEAVE_ROOM,
} from "./types.js";
const HEARTBEAT_INTERVAL = 10 * 1000;
const Log = createLogger("JANUS");
type JanusConfiguration = {
address: {
url: string;
token?: string;
};
};
export class Janus extends EventEmitter {
private socket: WebSocket;
private heartbeatInterval: NodeJS.Timeout;
private token?: string;
private sequence = 0;
private adminSession: number;
private adminHandle: number;
get session() {
return this.adminSession;
}
public connect = (config: JanusConfiguration): Promise<void> => {
this.token = config.address.token;
this.socket = new WebSocket(new URL(config.address.
CD34
url), "janus-protocol");
this.socket.on("close", this.onClose);
this.socket.on("error", this.onError);
return new Promise((resolve, reject) => {
const open = async () => {
this.socket.removeListener("error", error);
await this.onOpen();
resolve();
};
const error = (err: Error) => {
this.onError(err);
this.socket.removeListener("open", open);
reject(err);
};
this.socket.once("open", open);
this.socket.once("error", error);
});
};
private () => {
Log.msg("Connected");
this.startHeartbeat();
this.adminSession = (await this.createSession()).id;
this.adminHandle = (await this.attachHandle()).id;
};
public createSession = () => this.send<RESPONSE_CREATE_SESSION>({ janus: "create" });
public attachHandle = () =>
this.send<RESPONSE_ATTACH_HANDLE>({
janus: "attach",
session_id: this.session,
plugin: "janus.plugin.audiobridge",
});
public createRoom = () =>
this.send<RESPONSE_CREATE_ROOM>({
janus: "message",
body: {
request: "create",
audiolevel_event: true,
},
session_id: this.session,
handle_id: this.adminHandle,
});
public joinRoom = (handle_id: number, room_id: number, display: string) =>
this.send<RESPONSE_JOIN_ROOM>({
janus: "message",
body: {
request: "join",
display,
room: room_id,
},
session_id: this.session,
handle_id,
});
public leaveRoom = (handle_id: number) =>
this.send<RESPONSE_LEAVE_ROOM>({
janus: "message",
body: {
request: "leave",
},
session_id: this.session,
handle_id,
});
public configure = (handle_id: number, jsep?: { type: string; sdp: string }) =>
this.send<RESPONSE_CONFIGURE>({
janus: "message",
body: {
request: "configure",
},
jsep,
session_id: this.session,
handle_id,
});
public trickle = (handle_id: number, candidates: RTCIceCandidate[]) =>
this.send({
janus: "trickle",
session_id: this.session,
handle_id,
candidates: [...candidates, null],
});
private number, reason: string) => {
Log.msg(`Closed with code ${code} : ${reason}`);
clearTimeout(this.heartbeatInterval);
};
private Error) => {
// TODO
};
// TODO: reject on timeout?
// TODO: rewrite
private send = <O extends JANUS_RESPONSE_DATA>(payload: JANUS_REQUEST): Promise<O> =>
new Promise((resolve) => {
const transaction = `${this.sequence++}`;
const decorated = { ...payload, token: this.token, transaction };
const listener = (data: RawData) => {
// TODO: is `listener` unique for each call to send?
// oxlint-disable-next-line typescript/no-base-to-string
const json: JANUS_RESPONSE<O> = JSON.parse(data.toString());
if (json.transaction !== transaction) return;
if ("plugindata" in json && json.plugindata) {
json.data = json.plugindata;
if ("data" in json.data) json.data = json.data.data as O;
json.plugindata = undefined;
}
if (
"data" in json &&
"audiobridge" in json.data &&
json.data.audiobridge === "event" &&
json.jsep
) {
json.data.jsep = json.jsep;
}
if ("data" in json) {
this.socket.removeListener("message", listener);
this.socket.setMaxListeners(this.socket.getMaxListeners() - 1);
return resolve(json.data);
}
if (
payload.janus === "trickle" ||
(payload.janus === "message" && payload.body.request !== "configure")
) {
this.socket.removeListener("message", listener);
this.socket.setMaxListeners(this.socket.getMaxListeners() - 1);
resolve({} as O);
}
};
this.socket.setMaxListeners(this.socket.getMaxListeners() + 1);
this.socket.on("message", listener);
this.socket.send(JSON.stringify(decorated));
});
private startHeartbeat = () => {
this.heartbeatInterval = setInterval(
() =>
this.send({
janus: "keepalive",
session_id: this.session,
}),
HEARTBEAT_INTERVAL,
); // 10 seconds
};
}