-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrtc_streamer.ts
More file actions
239 lines (220 loc) · 6.69 KB
/
rtc_streamer.ts
File metadata and controls
239 lines (220 loc) · 6.69 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import SdpTransform from "sdp-transform";
import * as later from "later";
import * as Sentry from "@sentry/react";
import * as BroadcastState from "./state";
import { Streamer, ConnectionStateEnum } from "./streamer";
import { Dispatch } from "redux";
type StreamerState = "HELLO" | "OFFER" | "ANSWER" | "CONNECTED";
export class WebRTCStreamer extends Streamer {
stream: MediaStream;
pc: RTCPeerConnection | null = null;
ws: WebSocket | undefined;
state: StreamerState = "HELLO";
isActive = false;
dispatch: Dispatch<any>;
unexpectedDeath = false;
newsInterval?: later.Timer;
constructor(stream: MediaStream, dispatch: Dispatch<any>) {
super();
this.stream = stream;
this.dispatch = dispatch;
}
async start(): Promise<void> {
console.log("RTCStreamer start");
this.ws = new WebSocket(process.env.REACT_APP_WS_URL!);
this.ws.onopen = (e) => {
console.log("WS open");
this.onStateChange(this.mapStateToConnectionState());
};
this.ws.onclose = async (e) => {
console.log("WS close");
await this.stop("WS close");
this.onStateChange(this.mapStateToConnectionState());
};
this.ws.addEventListener("message", this.onMessage.bind(this));
console.log("WS created");
}
async stop(reason?: string): Promise<void> {
Sentry.captureMessage(`Connection STOP due to ${reason}`);
if (this.ws) {
this.ws.close();
this.ws = null as any;
}
if (this.pc) {
this.pc.close();
this.pc = null;
}
this.unexpectedDeath = false;
}
async onMessage(evt: MessageEvent) {
const data = JSON.parse(evt.data);
switch (data.kind) {
case "HELLO":
this.onStateChange("CONNECTING");
console.log("WS HELLO, our client ID is " + data.connectionId);
this.dispatch(BroadcastState.setWsID(data.connectionId));
if (this.state !== "HELLO") {
this.ws!.close();
}
this.createPeerConnection(data.iceServers);
if (!this.pc) {
throw new Error(
"Tried to do websocket fuckery with a null PeerConnection!"
);
}
const offer = await this.pc.createOffer();
// Do some fun SDP fuckery to get better quality
const parsed = SdpTransform.parse(offer.sdp!);
console.log("Old SDP", parsed);
parsed.media.forEach((track) => {
let opusIndex = 0;
for (let i = 0; i < track.rtp.length; i++) {
if (track.rtp[i].codec === "opus") {
opusIndex = i;
}
// TODO: maybe delete non-Opus candidates?
}
track.fmtp[opusIndex].config += `; maxaveragebitrate=${192 *
2 *
1024}; stereo=1; sprop-stereo=1 ; cbr=1`;
});
offer.sdp = SdpTransform.write(parsed);
console.log("New SDP", offer.sdp);
await this.pc.setLocalDescription(offer);
await this.waitForIceCandidates();
this.ws!.send(
JSON.stringify({
kind: "OFFER",
type: this.pc.localDescription!.type,
sdp: this.pc.localDescription!.sdp,
})
);
this.state = "OFFER";
break;
case "ANSWER":
if (!this.pc) {
throw new Error("Tried to ANSWER with a null PeerConnection!");
}
const answer = new RTCSessionDescription({
type: data.type,
sdp: data.sdp,
});
await this.pc.setRemoteDescription(answer);
this.state = "ANSWER";
break;
case "ACTIVATED":
this.isActive = true;
this.onStateChange("LIVE");
this.dispatch(BroadcastState.setTracklisting(true));
break;
case "DEACTIVATED":
this.isActive = false;
this.onStateChange(this.mapStateToConnectionState());
this.dispatch(BroadcastState.setTracklisting(false));
break;
case "DIED":
// oo-er
// server thinks we've lost connection
// kill it on our end and trigger a reconnect
await this.stop("server DIED");
await this.start();
this.unexpectedDeath = true;
break;
}
}
createPeerConnection(iceServers: RTCIceServer[]) {
this.pc = new RTCPeerConnection({
iceServers,
});
this.pc.oniceconnectionstatechange = async (e) => {
if (!this.pc) {
throw new Error(
"Received ICEConnectionStateChange but PC was null?????"
);
}
console.log("ICE Connection state change: " + this.pc.iceConnectionState);
if (this.pc.iceConnectionState === "failed") {
await this.stop("ICE failure");
}
this.onStateChange(this.mapStateToConnectionState());
};
this.stream.getAudioTracks().forEach((track) => this.pc!.addTrack(track));
console.log("PC created");
}
async getStatistics() {
if (this.pc) {
return await this.pc.getStats();
} else {
return null;
}
}
// TODO: supporting trickle ICE would be nICE
waitForIceCandidates() {
return new Promise<void>((resolve) => {
if (!this.pc) {
throw new Error(
"Tried to gather ICE Candidates with a null PeerConnection!"
);
}
if (this.pc.iceGatheringState === "complete") {
resolve();
} else {
const check = () => {
if (!this.pc) {
throw new Error(
"Received iceGatheringStateChange but PC was null???"
);
}
if (this.pc.iceGatheringState === "complete") {
this.pc.removeEventListener("icegatheringstatechange", check);
resolve();
}
};
this.pc.addEventListener("icegatheringstatechange", check);
}
});
}
mapStateToConnectionState(): ConnectionStateEnum {
if (!this.pc) {
if (this.unexpectedDeath) {
return "CONNECTION_LOST";
} else {
return "NOT_CONNECTED";
}
}
console.log(
"Relevant values: ",
this.pc?.iceConnectionState,
this.ws?.readyState
);
switch (this.pc.iceConnectionState) {
case "connected":
case "completed":
return "CONNECTED";
case "new":
if (this.ws) {
switch (this.ws.readyState) {
case 0:
return "NOT_CONNECTED";
case 1:
return "CONNECTING";
case 2:
case 3:
return "CONNECTION_LOST";
default:
throw new Error();
}
} else {
return "NOT_CONNECTED";
}
case "checking":
return "CONNECTING";
case "disconnected":
return "CONNECTION_LOST_RECONNECTING";
case "failed":
return "CONNECTION_LOST";
case "closed":
return "NOT_CONNECTED";
}
}
}