Repository navigation
Expand file tree
/
Copy pathattributes.js
More file actions
173 lines (150 loc) · 4.96 KB
/
Copy pathattributes.js
File metadata and controls
173 lines (150 loc) · 4.96 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
import ipaddr from "https://cdn.jsdelivr.net/npm/ipaddr.js@2/+esm";
/*
'fmtp': {},
'msid': {},
'sendrecv': {},
'ssrc': {},
*/
const attrParserMap = Object.freeze({
'candidate': parseCandidate,
'end-of-candidates': v => { },
'fingerprint': parseFingerprint,
'group': parseGroup,
'ice-pwd': parseIcePassword,
'ice-ufrag': parseIceUsername,
'mid': parseMid,
'rtcp-mux': parseRTCPOption,
'rtcp-rsize': parseRTCPOption,
'rtpmap': parseRTPMap,
'setup': parseSetup,
});
export const CONSTANT = {
CandidateType: Object.freeze({
Unknown: 'unknown',
Host: 'host',
ServerReflexive: 'srflx',
PeerReflexive: 'prflx',
Relayed: 'relay',
}),
}
class Candidate {
foundation = null;
componentId = null;
transport = null;
priority = null;
type = CONSTANT.CandidateType.Unknown;
address = null;
port = null;
raddress = null;
rport = null;
}
class RTPMap {
payloadType = null;
encodingName = null;
clockRate = null;
channels = null;
constructor(pt, encname, clock, ch) {
this.payloadType = pt;
this.encodingName = encname;
this.clockRate = clock;
this.channels = ch;
}
}
function parseIPAddressIfValid(addr) {
if (addr === undefined || addr == null) {
return null;
}
try {
return ipaddr.parse(addr);
} catch {
console.error(`Unable to parse IP address ${addr}`);
return null;
}
}
function parseCandidate(_key, value, attributes) {
// Candidate attribute is defined in sec 5.1 of rfc-8839.
const ipaddress = `[0-9a-fA-F\\.\\:]`;
const CandidateRegex = new RegExp(
`(?<foundation>[a-zA-Z\\d\+\/]{1,32})`
+ ` (?<component_id>\\d{1,3})`
+ ` (?<transport>[^ ]+)`
+ ` (?<priority>\\d{1,10})`
+ ` (?<ipaddress>${ipaddress}+)`
+ ` (?<port>\\d+)`
+ ` typ (?<type>host|srflx|prflx|relay|[^ ]+)`
+ ` (?:raddr (?<reladdr>${ipaddress}+) rport (?<relport>\\d+))?`
);
const results = value.match(CandidateRegex)
var c = new Candidate();
c.foundation = results.groups.foundation;
c.componentId = results.groups.component_id;
c.transport = results.groups.transport;
c.priority = results.groups.priority;
c.address = parseIPAddressIfValid(results.groups.ipaddress);
c.port = results.groups.port;
c.type = results.groups.type;
c.raddress = parseIPAddressIfValid(results.groups.reladdr);
c.rport = results.groups.relport;
attributes['candidates'] ??= []
attributes['candidates'].push(c);
}
function parseRTPMap(_key, value, attributes) {
// rtpmap attribute is defined in section 6.6 of rfc-8866.
const RTPMapRegex = new RegExp(
`(?<payload_type>\\d+) (?<encoding_name>[^\\/]+)\\/(?<clock_rate>\\d+)\\/?(?<channels>\\d+)?`
);
const results = value.match(RTPMapRegex);
attributes['rtpmap'] = new RTPMap(results.groups.payload_type,
results.groups.encoding_name,
results.groups.clock_rate,
results.groups.channels);
}
function parseRTCPOption(key, _value, attributes) {
if (key == "rtcp-mux") {
attributes['rtcp-mux'] = true;
} else if (key == 'rtcp-rsize') {
attributes['rtcp-rsize'] = true;
}
}
function parseFingerprint(_key, value, attributes) {
const [, hashfunc, hashvalstr] = value.match(/([^ ]*) (.*)/);
const hashval = hashvalstr.match(/[0-9a-fA-F]+/g);
attributes['hash-algorithm'] = hashfunc;
attributes['hash-digest'] = hashval.map(hex => parseInt(hex, 16));
}
function parseGroup(_key, value, attributes) {
const [, idliststr] = value.match(/BUNDLE (.*)/);
attributes['bundle'] = idliststr.match(/[0-9]+/g);
}
function parseSetup(_key, value, attributes) {
attributes['setup-role'] = value;
}
function parseMid(_key, value, attributes) {
attributes['mid'] = value;
}
function parseIceUsername(_key, value, attributes) {
attributes['ice-ufrag'] = value;
}
function parseIcePassword(_key, value, attributes) {
attributes['ice-pass'] = value;
}
function parseAttribute(key, value, attributes) {
const fn = attrParserMap[key];
if (fn) {
fn(key, value, attributes);
} else {
console.log(`unknown key ${key}`);
}
}
export class Attributes extends Map {
constructor(attrlines) {
super();
const AttributeLineRegex = /([^:]*):?(.*)?/;
attrlines
.filter(line => line[1] === "a")
.map(line => {
let [, attrkey, attrvalue] = line[2].match(AttributeLineRegex);
parseAttribute(attrkey, attrvalue, this);
})
}
}