-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathRTCIceCandidate.ts
124 lines (102 loc) · 3.87 KB
/
RTCIceCandidate.ts
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
// https://developer.mozilla.org/docs/Web/API/RTCIceCandidate
//
// Example: candidate:123456 1 UDP 123456 192.168.1.1 12345 typ host raddr=10.0.0.1 rport=54321 generation 0
export default class RTCIceCandidate implements globalThis.RTCIceCandidate {
#address: string | null;
#candidate: string;
#component: globalThis.RTCIceComponent | null;
#foundation: string | null;
#port: number | null;
#priority: number | null;
#protocol: globalThis.RTCIceProtocol | null;
#relatedAddress: string | null;
#relatedPort: number | null;
#sdpMLineIndex: number | null;
#sdpMid: string | null;
#tcpType: globalThis.RTCIceTcpCandidateType | null;
#type: globalThis.RTCIceCandidateType | null;
#usernameFragment: string | null;
constructor({ candidate, sdpMLineIndex, sdpMid, usernameFragment }: globalThis.RTCIceCandidateInit) {
if (sdpMLineIndex == null && sdpMid == null)
throw new TypeError('At least one of sdpMLineIndex or sdpMid must be specified');
this.#candidate = candidate === null ? 'null' : candidate ?? '';
this.#sdpMLineIndex = sdpMLineIndex ?? null;
this.#sdpMid = sdpMid ?? null;
this.#usernameFragment = usernameFragment ?? null;
if (candidate) {
const fields = candidate.split(' ');
this.#foundation = fields[0].replace('candidate:', ''); // remove text candidate:
this.#component = fields[1] == '1' ? 'rtp' : 'rtcp';
this.#protocol = fields[2] as globalThis.RTCIceProtocol;
this.#priority = parseInt(fields[3], 10);
this.#address = fields[4];
this.#port = parseInt(fields[5], 10);
this.#type = fields[7] as globalThis.RTCIceCandidateType;
this.#tcpType = null;
this.#relatedAddress = null;
this.#relatedPort = null;
// Parse the candidate string to extract relatedPort and relatedAddress
for (let i = 8; i < fields.length; i++) {
const field = fields[i];
if (field === 'raddr') {
this.#relatedAddress = fields[i + 1];
} else if (field === 'rport') {
this.#relatedPort = parseInt(fields[i + 1], 10);
}
if (this.#protocol === 'tcp' && field === 'tcptype') {
this.#tcpType = fields[i + 1] as globalThis.RTCIceTcpCandidateType;
}
}
}
}
get address(): string | null {
return this.#address || null;
}
get candidate(): string {
return this.#candidate;
}
get component(): globalThis.RTCIceComponent | null {
return this.#component;
}
get foundation(): string | null {
return this.#foundation || null;
}
get port(): number | null {
return this.#port || null;
}
get priority(): number | null {
return this.#priority || null;
}
get protocol(): globalThis.RTCIceProtocol | null {
return this.#protocol || null;
}
get relatedAddress(): string | null {
return this.#relatedAddress;
}
get relatedPort(): number | null {
return this.#relatedPort || null;
}
get sdpMLineIndex(): number | null {
return this.#sdpMLineIndex;
}
get sdpMid(): string | null {
return this.#sdpMid;
}
get tcpType(): globalThis.RTCIceTcpCandidateType | null {
return this.#tcpType;
}
get type(): globalThis.RTCIceCandidateType | null {
return this.#type || null;
}
get usernameFragment(): string | null {
return this.#usernameFragment;
}
toJSON(): globalThis.RTCIceCandidateInit {
return {
candidate: this.#candidate,
sdpMLineIndex: this.#sdpMLineIndex,
sdpMid: this.#sdpMid,
usernameFragment: this.#usernameFragment,
};
}
}