mirror of
https://github.com/ggerganov/wave-share.git
synced 2026-03-18 05:15:59 +08:00
Initial commit
This commit is contained in:
118
sdp-transform/parser.js
Normal file
118
sdp-transform/parser.js
Normal file
@@ -0,0 +1,118 @@
|
||||
var toIntIfInt = function (v) {
|
||||
return String(Number(v)) === v ? Number(v) : v;
|
||||
};
|
||||
|
||||
var attachProperties = function (match, location, names, rawName) {
|
||||
if (rawName && !names) {
|
||||
location[rawName] = toIntIfInt(match[1]);
|
||||
}
|
||||
else {
|
||||
for (var i = 0; i < names.length; i += 1) {
|
||||
if (match[i+1] != null) {
|
||||
location[names[i]] = toIntIfInt(match[i+1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var parseReg = function (obj, location, content) {
|
||||
var needsBlank = obj.name && obj.names;
|
||||
if (obj.push && !location[obj.push]) {
|
||||
location[obj.push] = [];
|
||||
}
|
||||
else if (needsBlank && !location[obj.name]) {
|
||||
location[obj.name] = {};
|
||||
}
|
||||
var keyLocation = obj.push ?
|
||||
{} : // blank object that will be pushed
|
||||
needsBlank ? location[obj.name] : location; // otherwise, named location or root
|
||||
|
||||
attachProperties(content.match(obj.reg), keyLocation, obj.names, obj.name);
|
||||
|
||||
if (obj.push) {
|
||||
location[obj.push].push(keyLocation);
|
||||
}
|
||||
};
|
||||
|
||||
var validLine = RegExp.prototype.test.bind(/^([a-z])=(.*)/);
|
||||
|
||||
function parseSDP(sdp) {
|
||||
var session = {}
|
||||
, media = []
|
||||
, location = session; // points at where properties go under (one of the above)
|
||||
|
||||
// parse lines we understand
|
||||
sdp.split(/(\r\n|\r|\n)/).filter(validLine).forEach(function (l) {
|
||||
var type = l[0];
|
||||
var content = l.slice(2);
|
||||
if (type === 'm') {
|
||||
media.push({rtp: [], fmtp: []});
|
||||
location = media[media.length-1]; // point at latest media line
|
||||
}
|
||||
|
||||
for (var j = 0; j < (grammar[type] || []).length; j += 1) {
|
||||
var obj = grammar[type][j];
|
||||
if (obj.reg.test(content)) {
|
||||
return parseReg(obj, location, content);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
session.media = media; // link it up
|
||||
return session;
|
||||
};
|
||||
|
||||
var paramReducer = function (acc, expr) {
|
||||
var s = expr.split(/=(.+)/, 2);
|
||||
if (s.length === 2) {
|
||||
acc[s[0]] = toIntIfInt(s[1]);
|
||||
}
|
||||
return acc;
|
||||
};
|
||||
|
||||
function parseParamsSDP(str) {
|
||||
return str.split(/\;\s?/).reduce(paramReducer, {});
|
||||
};
|
||||
|
||||
function parsePayloadsSDP(str) {
|
||||
return str.split(' ').map(Number);
|
||||
};
|
||||
|
||||
function parseRemoteCandidatesSDP(str) {
|
||||
var candidates = [];
|
||||
var parts = str.split(' ').map(toIntIfInt);
|
||||
for (var i = 0; i < parts.length; i += 3) {
|
||||
candidates.push({
|
||||
component: parts[i],
|
||||
ip: parts[i + 1],
|
||||
port: parts[i + 2]
|
||||
});
|
||||
}
|
||||
return candidates;
|
||||
};
|
||||
|
||||
function parseImageAttributesSDP(str) {
|
||||
return str.split(' ').map(function (item) {
|
||||
return item.substring(1, item.length-1).split(',').reduce(paramReducer, {});
|
||||
});
|
||||
};
|
||||
|
||||
function parseSimulcastStreamListSDP(str) {
|
||||
return str.split(';').map(function (stream) {
|
||||
return stream.split(',').map(function (format) {
|
||||
var scid, paused = false;
|
||||
|
||||
if (format[0] !== '~') {
|
||||
scid = toIntIfInt(format);
|
||||
} else {
|
||||
scid = toIntIfInt(format.substring(1, format.length));
|
||||
paused = true;
|
||||
}
|
||||
|
||||
return {
|
||||
scid: scid,
|
||||
paused: paused
|
||||
};
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user