forked from Nik-Hendricks/node.js-sip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDPParser.js
More file actions
47 lines (43 loc) · 1.52 KB
/
SDPParser.js
File metadata and controls
47 lines (43 loc) · 1.52 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
const SDPParser = {
parse(sdpString) {
var parsedData = {};
const lines = sdpString.split('\r\n');
let sessionInfo = {};
let mediaInfo = [];
let currentMedia = {};
lines.forEach((line) => {
if (line.startsWith('v=')) {
sessionInfo.version = line.substring(2);
} else if (line.startsWith('o=')) {
sessionInfo.origin = line.substring(2);
} else if (line.startsWith('s=')) {
sessionInfo.sessionName = line.substring(2);
} else if (line.startsWith('m=')) {
if (currentMedia.media) {
mediaInfo.push(currentMedia);
currentMedia = {};
}
const [mediaType, port, protocol, format] = line.substring(2).split(' ');
currentMedia = {
media: mediaType,
port: parseInt(port),
protocol,
format,
};
} else if (line.startsWith('a=')) {
const attribute = line.substring(2);
currentMedia.attributes = currentMedia.attributes || [];
currentMedia.attributes.push(attribute);
}
});
if (currentMedia.media) {
mediaInfo.push(currentMedia);
}
parsedData = {
session: sessionInfo,
media: mediaInfo,
};
return parsedData;
}
}
module.exports = SDPParser;