-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
159 lines (135 loc) · 4.31 KB
/
Copy pathparser.js
File metadata and controls
159 lines (135 loc) · 4.31 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
// SDP stands for Session Description Protocol, and is defined in RFC 8866.
// RFC 8829, which established the JavaScript Session Establishment Protocol,
// discussed the SDP keys relevant to WebRTC. RFC 9429 obsoleted RFC 8829.
const NewLineRegex = /\r?\n/;
const DescriptionLineRegex = /([^=]*)=(.*)/;
class Description {
constructor(lines) {
this.lines = structuredClone(lines);
}
firstLineNumber() {
return this.lines[0][0];
}
lastLineNumber() {
return this.lines.at(-1)[0];
}
toString() {
return this.lines.map((line) => `${line[1]}=${line[2]}`).join("\n");
}
explain() {
const info = {
heading: "Unknown Section"
};
return JSON.stringify(info);
}
};
class SessionDescription extends Description {
constructor(lines) {
super(lines);
this.media = [];
}
addMedia(media) {
if (!(media instanceof MediaDescription)) {
throw new Error("Adding non-MediaDescription object");
}
this.media.push(media);
}
explain() {
let mediaitems = []
for (const [ , m] of Object.entries(this.media)) {
console.log(m);
console.log(Object.getOwnPropertyNames(m))
mediaitems.push( { type: m.mediatype } );
}
const info = {
heading: "Session Description",
media: {
subheading: `${this.media.length} media descriptions`,
items: mediaitems,
}
};
return JSON.stringify(info, null, 2);
}
}
class MediaDescription extends Description {
constructor(session, lines) {
super(lines);
this.session = session;
// From section 5.14 of RFC 8866
// m=<media> <port> <proto> <fmt> ...
let mline = /(?<media>[^ ]*) (?<port>\d*)\/?(?<numports>\d*)? (?<proto>[^ ]*) (?<fmt>.*)/
const results = this.lines[0][2].match(mline);
this.mediatype = results.groups.media;
this.port = results.groups.port;
this.numports = results.groups.numports ?? 1;
this.proto = results.groups.proto;
this.fmt = results.groups.fmt;
console.dir(this);
}
explain() {
const info = {
heading: "Media Description"
};
return JSON.stringify(info, null, 2);
}
}
export function parseSDP(sdp) {
if (sdp.length == 0) {
return;
}
let sections = [];
for (const [index, line] of sdp.split(NewLineRegex).entries()) {
if (line.length == 0) {
console.debug(`line ${index} empty`);
continue;
}
try {
let [, key, value] = line.match(DescriptionLineRegex);
if (key == "v") {
if (sections.length == 0) {
sections.push([ [index, key, value] ]);
} else {
// Only one 'v' line is allowed in a SDP
}
} else if (key == "m") {
sections.push([ [index, key, value] ]);
} else {
sections.at(-1).push([index, key, value]);
}
} catch (error) {
console.log(`error parsing line ${index}`)
}
}
let session = null;
let descriptions = [];
for (const s of (sections || [])) {
switch (s[0][1]) {
case 'v':
if (session != null) {
throw new Error("Found multiple session descriptions");
}
session = new SessionDescription(s);
descriptions[session.firstLineNumber()] = session;
break;
case 'm':
let media = new MediaDescription(session, s);
session.addMedia(media);
descriptions[media.firstLineNumber()] = media;
break
}
}
console.log(`Done parsing SDP, found ${sections.length} sections`);
return descriptions;
}
export function mapLineToDescription(descriptions, line) {
let prevDescription;
for (const [key, description] of Object.entries(descriptions)) {
if (line == key) {
return description;
} else if (line < key) {
return prevDescription;
}
prevDescription = description;
}
return prevDescription;
}