2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2014-03-06 14:28:36 +00:00
|
|
|
/* jshint -W117 */
|
2015-01-19 09:20:00 +00:00
|
|
|
var SDPUtil = require("./SDPUtil");
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
// SDP STUFF
|
|
|
|
function SDP(sdp) {
|
2015-09-21 16:28:49 +00:00
|
|
|
/**
|
|
|
|
* Whether or not to remove TCP ice candidates when translating from/to jingle.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.removeTcpCandidates = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to remove UDP ice candidates when translating from/to jingle.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.removeUdpCandidates = false;
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
this.media = sdp.split('\r\nm=');
|
|
|
|
for (var i = 1; i < this.media.length; i++) {
|
|
|
|
this.media[i] = 'm=' + this.media[i];
|
|
|
|
if (i != this.media.length - 1) {
|
|
|
|
this.media[i] += '\r\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.session = this.media.shift() + '\r\n';
|
|
|
|
this.raw = this.session + this.media.join('');
|
|
|
|
}
|
2015-09-21 16:28:49 +00:00
|
|
|
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
|
|
|
* Returns map of MediaChannel mapped per channel idx.
|
|
|
|
*/
|
|
|
|
SDP.prototype.getMediaSsrcMap = function() {
|
|
|
|
var self = this;
|
|
|
|
var media_ssrcs = {};
|
2014-12-26 12:17:25 +00:00
|
|
|
var tmp;
|
|
|
|
for (var mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
|
|
|
|
tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc:');
|
|
|
|
var mid = SDPUtil.parse_mid(SDPUtil.find_line(self.media[mediaindex], 'a=mid:'));
|
|
|
|
var media = {
|
|
|
|
mediaindex: mediaindex,
|
|
|
|
mid: mid,
|
|
|
|
ssrcs: {},
|
|
|
|
ssrcGroups: []
|
|
|
|
};
|
|
|
|
media_ssrcs[mediaindex] = media;
|
2014-03-13 12:22:54 +00:00
|
|
|
tmp.forEach(function (line) {
|
|
|
|
var linessrc = line.substring(7).split(' ')[0];
|
|
|
|
// allocate new ChannelSsrc
|
2014-12-26 12:17:25 +00:00
|
|
|
if(!media.ssrcs[linessrc]) {
|
|
|
|
media.ssrcs[linessrc] = {
|
|
|
|
ssrc: linessrc,
|
|
|
|
lines: []
|
|
|
|
};
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
2014-12-26 12:17:25 +00:00
|
|
|
media.ssrcs[linessrc].lines.push(line);
|
2014-03-13 12:22:54 +00:00
|
|
|
});
|
2014-12-26 12:17:25 +00:00
|
|
|
tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc-group:');
|
2014-08-21 09:58:33 +00:00
|
|
|
tmp.forEach(function(line){
|
|
|
|
var semantics = line.substr(0, idx).substr(13);
|
|
|
|
var ssrcs = line.substr(14 + semantics.length).split(' ');
|
2015-09-11 03:26:29 +00:00
|
|
|
if (ssrcs.length) {
|
2014-12-26 12:17:25 +00:00
|
|
|
media.ssrcGroups.push({
|
|
|
|
semantics: semantics,
|
|
|
|
ssrcs: ssrcs
|
|
|
|
});
|
2014-08-21 09:58:33 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
|
|
|
return media_ssrcs;
|
2014-11-11 14:46:13 +00:00
|
|
|
};
|
2014-03-13 12:22:54 +00:00
|
|
|
/**
|
|
|
|
* Returns <tt>true</tt> if this SDP contains given SSRC.
|
|
|
|
* @param ssrc the ssrc to check.
|
|
|
|
* @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
|
|
|
|
*/
|
|
|
|
SDP.prototype.containsSSRC = function(ssrc) {
|
2014-12-26 12:17:25 +00:00
|
|
|
var medias = this.getMediaSsrcMap();
|
|
|
|
Object.keys(medias).forEach(function(mediaindex){
|
|
|
|
var media = medias[mediaindex];
|
2014-03-13 12:22:54 +00:00
|
|
|
//console.log("Check", channel, ssrc);
|
2014-12-26 12:17:25 +00:00
|
|
|
if(Object.keys(media.ssrcs).indexOf(ssrc) != -1){
|
2015-09-21 16:28:49 +00:00
|
|
|
return true;
|
2014-03-13 12:22:54 +00:00
|
|
|
}
|
|
|
|
});
|
2015-09-21 16:28:49 +00:00
|
|
|
return false;
|
2014-11-11 14:46:13 +00:00
|
|
|
};
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
// remove iSAC and CN from SDP
|
|
|
|
SDP.prototype.mangle = function () {
|
|
|
|
var i, j, mline, lines, rtpmap, newdesc;
|
|
|
|
for (i = 0; i < this.media.length; i++) {
|
|
|
|
lines = this.media[i].split('\r\n');
|
|
|
|
lines.pop(); // remove empty last element
|
|
|
|
mline = SDPUtil.parse_mline(lines.shift());
|
|
|
|
if (mline.media != 'audio')
|
|
|
|
continue;
|
|
|
|
newdesc = '';
|
|
|
|
mline.fmt.length = 0;
|
|
|
|
for (j = 0; j < lines.length; j++) {
|
|
|
|
if (lines[j].substr(0, 9) == 'a=rtpmap:') {
|
|
|
|
rtpmap = SDPUtil.parse_rtpmap(lines[j]);
|
|
|
|
if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC')
|
|
|
|
continue;
|
|
|
|
mline.fmt.push(rtpmap.id);
|
|
|
|
newdesc += lines[j] + '\r\n';
|
|
|
|
} else {
|
|
|
|
newdesc += lines[j] + '\r\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.media[i] = SDPUtil.build_mline(mline) + '\r\n';
|
|
|
|
this.media[i] += newdesc;
|
|
|
|
}
|
|
|
|
this.raw = this.session + this.media.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
// remove lines matching prefix from session section
|
|
|
|
SDP.prototype.removeSessionLines = function(prefix) {
|
|
|
|
var self = this;
|
|
|
|
var lines = SDPUtil.find_lines(this.session, prefix);
|
|
|
|
lines.forEach(function(line) {
|
|
|
|
self.session = self.session.replace(line + '\r\n', '');
|
|
|
|
});
|
|
|
|
this.raw = this.session + this.media.join('');
|
|
|
|
return lines;
|
2015-09-11 03:05:53 +00:00
|
|
|
};
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
// remove lines matching prefix from a media section specified by mediaindex
|
|
|
|
// TODO: non-numeric mediaindex could match mid
|
|
|
|
SDP.prototype.removeMediaLines = function(mediaindex, prefix) {
|
|
|
|
var self = this;
|
|
|
|
var lines = SDPUtil.find_lines(this.media[mediaindex], prefix);
|
|
|
|
lines.forEach(function(line) {
|
|
|
|
self.media[mediaindex] = self.media[mediaindex].replace(line + '\r\n', '');
|
|
|
|
});
|
|
|
|
this.raw = this.session + this.media.join('');
|
|
|
|
return lines;
|
2015-09-11 03:05:53 +00:00
|
|
|
};
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
// add content's to a jingle element
|
2015-08-05 12:09:12 +00:00
|
|
|
SDP.prototype.toJingle = function (elem, thecreator, ssrcs) {
|
2014-11-14 10:13:26 +00:00
|
|
|
// console.log("SSRC" + ssrcs["audio"] + " - " + ssrcs["video"]);
|
2015-09-21 16:28:49 +00:00
|
|
|
var i, j, k, mline, ssrc, rtpmap, tmp, lines;
|
2014-03-06 14:28:36 +00:00
|
|
|
// new bundle plan
|
|
|
|
if (SDPUtil.find_line(this.session, 'a=group:')) {
|
|
|
|
lines = SDPUtil.find_lines(this.session, 'a=group:');
|
|
|
|
for (i = 0; i < lines.length; i++) {
|
|
|
|
tmp = lines[i].split(' ');
|
|
|
|
var semantics = tmp.shift().substr(8);
|
|
|
|
elem.c('group', {xmlns: 'urn:xmpp:jingle:apps:grouping:0', semantics:semantics});
|
|
|
|
for (j = 0; j < tmp.length; j++) {
|
|
|
|
elem.c('content', {name: tmp[j]}).up();
|
|
|
|
}
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < this.media.length; i++) {
|
|
|
|
mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
|
2014-05-08 09:26:15 +00:00
|
|
|
if (!(mline.media === 'audio' ||
|
|
|
|
mline.media === 'video' ||
|
|
|
|
mline.media === 'application'))
|
|
|
|
{
|
2014-03-06 14:28:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=ssrc:')) {
|
|
|
|
ssrc = SDPUtil.find_line(this.media[i], 'a=ssrc:').substring(7).split(' ')[0]; // take the first
|
|
|
|
} else {
|
2015-09-21 16:28:49 +00:00
|
|
|
if(ssrcs && ssrcs[mline.media]) {
|
2014-11-14 10:13:26 +00:00
|
|
|
ssrc = ssrcs[mline.media];
|
2015-09-21 16:28:49 +00:00
|
|
|
} else {
|
2014-11-14 10:13:26 +00:00
|
|
|
ssrc = false;
|
2015-09-21 16:28:49 +00:00
|
|
|
}
|
2014-03-06 14:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
elem.c('content', {creator: thecreator, name: mline.media});
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=mid:')) {
|
|
|
|
// prefer identifier from a=mid if present
|
|
|
|
var mid = SDPUtil.parse_mid(SDPUtil.find_line(this.media[i], 'a=mid:'));
|
|
|
|
elem.attrs({ name: mid });
|
|
|
|
}
|
2014-05-08 09:26:15 +00:00
|
|
|
|
2015-09-21 16:28:49 +00:00
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length) {
|
2014-03-06 14:28:36 +00:00
|
|
|
elem.c('description',
|
|
|
|
{xmlns: 'urn:xmpp:jingle:apps:rtp:1',
|
|
|
|
media: mline.media });
|
|
|
|
if (ssrc) {
|
|
|
|
elem.attrs({ssrc: ssrc});
|
|
|
|
}
|
|
|
|
for (j = 0; j < mline.fmt.length; j++) {
|
|
|
|
rtpmap = SDPUtil.find_line(this.media[i], 'a=rtpmap:' + mline.fmt[j]);
|
|
|
|
elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
|
|
|
|
// put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j])) {
|
|
|
|
tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j]));
|
|
|
|
for (k = 0; k < tmp.length; k++) {
|
|
|
|
elem.c('parameter', tmp[k]).up();
|
|
|
|
}
|
|
|
|
}
|
2015-09-21 16:28:49 +00:00
|
|
|
this.rtcpFbToJingle(i, elem, mline.fmt[j]); // XEP-0293 -- map a=rtcp-fb
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=crypto:', this.session)) {
|
|
|
|
elem.c('encryption', {required: 1});
|
|
|
|
var crypto = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
|
|
|
|
crypto.forEach(function(line) {
|
|
|
|
elem.c('crypto', SDPUtil.parse_crypto(line)).up();
|
|
|
|
});
|
|
|
|
elem.up(); // end of encryption
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssrc) {
|
|
|
|
// new style mapping
|
|
|
|
elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
|
|
|
|
// FIXME: group by ssrc and support multiple different ssrcs
|
|
|
|
var ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
|
2014-11-14 10:13:26 +00:00
|
|
|
if(ssrclines.length > 0) {
|
|
|
|
ssrclines.forEach(function (line) {
|
2015-09-21 16:28:49 +00:00
|
|
|
var idx = line.indexOf(' ');
|
2014-11-14 10:13:26 +00:00
|
|
|
var linessrc = line.substr(0, idx).substr(7);
|
|
|
|
if (linessrc != ssrc) {
|
|
|
|
elem.up();
|
|
|
|
ssrc = linessrc;
|
|
|
|
elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
|
|
|
|
}
|
|
|
|
var kv = line.substr(idx + 1);
|
|
|
|
elem.c('parameter');
|
|
|
|
if (kv.indexOf(':') == -1) {
|
|
|
|
elem.attrs({ name: kv });
|
|
|
|
} else {
|
2015-07-10 09:57:20 +00:00
|
|
|
var k = kv.split(':', 2)[0];
|
|
|
|
elem.attrs({ name: k });
|
|
|
|
|
|
|
|
var v = kv.split(':', 2)[1];
|
|
|
|
v = SDPUtil.filter_special_chars(v);
|
|
|
|
elem.attrs({ value: v });
|
2014-11-14 10:13:26 +00:00
|
|
|
}
|
2014-03-06 14:28:36 +00:00
|
|
|
elem.up();
|
2014-11-14 10:13:26 +00:00
|
|
|
});
|
2015-09-21 16:28:49 +00:00
|
|
|
} else {
|
2014-11-14 10:13:26 +00:00
|
|
|
elem.up();
|
|
|
|
elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
|
2014-03-06 14:28:36 +00:00
|
|
|
elem.c('parameter');
|
2014-11-14 10:13:26 +00:00
|
|
|
elem.attrs({name: "cname", value:Math.random().toString(36).substring(7)});
|
2014-03-06 14:28:36 +00:00
|
|
|
elem.up();
|
2014-11-14 10:13:26 +00:00
|
|
|
var msid = null;
|
2015-09-11 03:05:53 +00:00
|
|
|
if(mline.media == "audio") {
|
2015-01-28 14:35:22 +00:00
|
|
|
msid = APP.RTC.localAudio.getId();
|
2015-09-11 03:05:53 +00:00
|
|
|
} else {
|
2015-01-28 14:35:22 +00:00
|
|
|
msid = APP.RTC.localVideo.getId();
|
2014-11-14 10:13:26 +00:00
|
|
|
}
|
2015-09-11 03:05:53 +00:00
|
|
|
if(msid !== null) {
|
2015-07-10 09:57:20 +00:00
|
|
|
msid = SDPUtil.filter_special_chars(msid);
|
2014-11-14 10:13:26 +00:00
|
|
|
elem.c('parameter');
|
|
|
|
elem.attrs({name: "msid", value:msid});
|
|
|
|
elem.up();
|
|
|
|
elem.c('parameter');
|
|
|
|
elem.attrs({name: "mslabel", value:msid});
|
|
|
|
elem.up();
|
|
|
|
elem.c('parameter');
|
|
|
|
elem.attrs({name: "label", value:msid});
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 13:35:13 +00:00
|
|
|
elem.up();
|
2014-03-06 14:28:36 +00:00
|
|
|
|
2014-08-21 09:58:33 +00:00
|
|
|
// XEP-0339 handle ssrc-group attributes
|
|
|
|
var ssrc_group_lines = SDPUtil.find_lines(this.media[i], 'a=ssrc-group:');
|
|
|
|
ssrc_group_lines.forEach(function(line) {
|
2015-09-21 16:28:49 +00:00
|
|
|
var idx = line.indexOf(' ');
|
2014-08-21 09:58:33 +00:00
|
|
|
var semantics = line.substr(0, idx).substr(13);
|
|
|
|
var ssrcs = line.substr(14 + semantics.length).split(' ');
|
2015-09-11 03:26:29 +00:00
|
|
|
if (ssrcs.length) {
|
2014-08-21 09:58:33 +00:00
|
|
|
elem.c('ssrc-group', { semantics: semantics, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
|
|
|
|
ssrcs.forEach(function(ssrc) {
|
|
|
|
elem.c('source', { ssrc: ssrc })
|
|
|
|
.up();
|
|
|
|
});
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
});
|
2014-03-06 14:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
|
|
|
|
elem.c('rtcp-mux').up();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XEP-0293 -- map a=rtcp-fb:*
|
2015-09-21 16:28:49 +00:00
|
|
|
this.rtcpFbToJingle(i, elem, '*');
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
// XEP-0294
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=extmap:')) {
|
|
|
|
lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
|
|
|
|
for (j = 0; j < lines.length; j++) {
|
|
|
|
tmp = SDPUtil.parse_extmap(lines[j]);
|
|
|
|
elem.c('rtp-hdrext', { xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
|
|
|
|
uri: tmp.uri,
|
|
|
|
id: tmp.value });
|
|
|
|
if (tmp.hasOwnProperty('direction')) {
|
|
|
|
switch (tmp.direction) {
|
|
|
|
case 'sendonly':
|
|
|
|
elem.attrs({senders: 'responder'});
|
|
|
|
break;
|
|
|
|
case 'recvonly':
|
|
|
|
elem.attrs({senders: 'initiator'});
|
|
|
|
break;
|
|
|
|
case 'sendrecv':
|
|
|
|
elem.attrs({senders: 'both'});
|
|
|
|
break;
|
|
|
|
case 'inactive':
|
|
|
|
elem.attrs({senders: 'none'});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: handle params
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem.up(); // end of description
|
|
|
|
}
|
|
|
|
|
|
|
|
// map ice-ufrag/pwd, dtls fingerprint, candidates
|
2015-09-21 16:28:49 +00:00
|
|
|
this.transportToJingle(i, elem);
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
if (SDPUtil.find_line(this.media[i], 'a=sendrecv', this.session)) {
|
|
|
|
elem.attrs({senders: 'both'});
|
|
|
|
} else if (SDPUtil.find_line(this.media[i], 'a=sendonly', this.session)) {
|
|
|
|
elem.attrs({senders: 'initiator'});
|
|
|
|
} else if (SDPUtil.find_line(this.media[i], 'a=recvonly', this.session)) {
|
|
|
|
elem.attrs({senders: 'responder'});
|
|
|
|
} else if (SDPUtil.find_line(this.media[i], 'a=inactive', this.session)) {
|
|
|
|
elem.attrs({senders: 'none'});
|
|
|
|
}
|
|
|
|
if (mline.port == '0') {
|
|
|
|
// estos hack to reject an m-line
|
|
|
|
elem.attrs({senders: 'rejected'});
|
|
|
|
}
|
|
|
|
elem.up(); // end of content
|
|
|
|
}
|
|
|
|
elem.up();
|
|
|
|
return elem;
|
|
|
|
};
|
|
|
|
|
2015-09-21 16:28:49 +00:00
|
|
|
SDP.prototype.transportToJingle = function (mediaindex, elem) {
|
|
|
|
var tmp, sctpmap, sctpAttrs, fingerprints;
|
2014-03-06 14:28:36 +00:00
|
|
|
var self = this;
|
|
|
|
elem.c('transport');
|
|
|
|
|
2014-05-26 12:54:17 +00:00
|
|
|
// XEP-0343 DTLS/SCTP
|
|
|
|
if (SDPUtil.find_line(this.media[mediaindex], 'a=sctpmap:').length)
|
|
|
|
{
|
2015-09-21 16:28:49 +00:00
|
|
|
sctpmap = SDPUtil.find_line(
|
|
|
|
this.media[mediaindex], 'a=sctpmap:', self.session);
|
2014-05-26 12:54:17 +00:00
|
|
|
if (sctpmap)
|
|
|
|
{
|
2015-09-21 16:28:49 +00:00
|
|
|
sctpAttrs = SDPUtil.parse_sctpmap(sctpmap);
|
2014-05-26 12:54:17 +00:00
|
|
|
elem.c('sctpmap',
|
|
|
|
{
|
|
|
|
xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
|
|
|
|
number: sctpAttrs[0], /* SCTP port */
|
2015-09-21 16:28:49 +00:00
|
|
|
protocol: sctpAttrs[1] /* protocol */
|
2014-05-26 12:54:17 +00:00
|
|
|
});
|
|
|
|
// Optional stream count attribute
|
|
|
|
if (sctpAttrs.length > 2)
|
|
|
|
elem.attrs({ streams: sctpAttrs[2]});
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
}
|
2014-03-06 14:28:36 +00:00
|
|
|
// XEP-0320
|
2015-09-21 16:28:49 +00:00
|
|
|
fingerprints = SDPUtil.find_lines(this.media[mediaindex], 'a=fingerprint:', this.session);
|
2014-03-06 14:28:36 +00:00
|
|
|
fingerprints.forEach(function(line) {
|
|
|
|
tmp = SDPUtil.parse_fingerprint(line);
|
2014-04-02 13:48:28 +00:00
|
|
|
tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
|
2014-03-06 14:28:36 +00:00
|
|
|
elem.c('fingerprint').t(tmp.fingerprint);
|
|
|
|
delete tmp.fingerprint;
|
|
|
|
line = SDPUtil.find_line(self.media[mediaindex], 'a=setup:', self.session);
|
|
|
|
if (line) {
|
|
|
|
tmp.setup = line.substr(8);
|
|
|
|
}
|
|
|
|
elem.attrs(tmp);
|
|
|
|
elem.up(); // end of fingerprint
|
|
|
|
});
|
|
|
|
tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
|
|
|
|
if (tmp) {
|
|
|
|
tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
|
|
|
|
elem.attrs(tmp);
|
|
|
|
// XEP-0176
|
|
|
|
if (SDPUtil.find_line(this.media[mediaindex], 'a=candidate:', this.session)) { // add any a=candidate lines
|
|
|
|
var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=candidate:', this.session);
|
|
|
|
lines.forEach(function (line) {
|
2015-09-21 16:28:49 +00:00
|
|
|
var candidate = SDPUtil.candidateToJingle(line);
|
|
|
|
var protocol = (candidate &&
|
|
|
|
typeof candidate.protocol === 'string')
|
|
|
|
? candidate.protocol.toLowerCase() : '';
|
|
|
|
if ((self.removeTcpCandidates && protocol === 'tcp') ||
|
|
|
|
(self.removeUdpCandidates && protocol === 'udp')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elem.c('candidate', candidate).up();
|
2014-03-06 14:28:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elem.up(); // end of transport
|
2015-09-11 03:05:53 +00:00
|
|
|
};
|
2014-03-06 14:28:36 +00:00
|
|
|
|
2015-09-21 16:28:49 +00:00
|
|
|
SDP.prototype.rtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
|
2014-03-06 14:28:36 +00:00
|
|
|
var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
|
|
|
|
lines.forEach(function (line) {
|
|
|
|
var tmp = SDPUtil.parse_rtcpfb(line);
|
|
|
|
if (tmp.type == 'trr-int') {
|
|
|
|
elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
|
|
|
|
elem.up();
|
|
|
|
} else {
|
|
|
|
elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
|
|
|
|
if (tmp.params.length > 0) {
|
|
|
|
elem.attrs({'subtype': tmp.params[0]});
|
|
|
|
}
|
|
|
|
elem.up();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-21 16:28:49 +00:00
|
|
|
SDP.prototype.rtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
|
2014-03-06 14:28:36 +00:00
|
|
|
var media = '';
|
|
|
|
var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
|
|
|
|
if (tmp.length) {
|
|
|
|
media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
|
|
|
|
if (tmp.attr('value')) {
|
|
|
|
media += tmp.attr('value');
|
|
|
|
} else {
|
|
|
|
media += '0';
|
|
|
|
}
|
|
|
|
media += '\r\n';
|
|
|
|
}
|
|
|
|
tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
|
|
|
|
tmp.each(function () {
|
|
|
|
media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
|
|
|
|
if ($(this).attr('subtype')) {
|
|
|
|
media += ' ' + $(this).attr('subtype');
|
|
|
|
}
|
|
|
|
media += '\r\n';
|
|
|
|
});
|
|
|
|
return media;
|
|
|
|
};
|
|
|
|
|
|
|
|
// construct an SDP from a jingle stanza
|
|
|
|
SDP.prototype.fromJingle = function (jingle) {
|
|
|
|
var self = this;
|
|
|
|
this.raw = 'v=0\r\n' +
|
2015-09-21 16:28:49 +00:00
|
|
|
'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
|
2014-03-06 14:28:36 +00:00
|
|
|
's=-\r\n' +
|
|
|
|
't=0 0\r\n';
|
|
|
|
// http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
|
|
|
|
if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
|
|
|
|
$(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
|
|
|
|
var contents = $(group).find('>content').map(function (idx, content) {
|
|
|
|
return content.getAttribute('name');
|
|
|
|
}).get();
|
|
|
|
if (contents.length > 0) {
|
|
|
|
self.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.session = this.raw;
|
|
|
|
jingle.find('>content').each(function () {
|
|
|
|
var m = self.jingle2media($(this));
|
|
|
|
self.media.push(m);
|
|
|
|
});
|
|
|
|
|
|
|
|
// reconstruct msid-semantic -- apparently not necessary
|
|
|
|
/*
|
|
|
|
var msid = SDPUtil.parse_ssrc(this.raw);
|
|
|
|
if (msid.hasOwnProperty('mslabel')) {
|
|
|
|
this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.raw = this.session + this.media.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
// translate a jingle content element into an an SDP media part
|
|
|
|
SDP.prototype.jingle2media = function (content) {
|
|
|
|
var media = '',
|
|
|
|
desc = content.find('description'),
|
|
|
|
ssrc = desc.attr('ssrc'),
|
|
|
|
self = this,
|
|
|
|
tmp;
|
2014-05-26 12:54:17 +00:00
|
|
|
var sctp = content.find(
|
|
|
|
'>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
tmp = { media: desc.attr('media') };
|
|
|
|
tmp.port = '1';
|
|
|
|
if (content.attr('senders') == 'rejected') {
|
|
|
|
// estos hack to reject an m-line.
|
|
|
|
tmp.port = '0';
|
|
|
|
}
|
|
|
|
if (content.find('>transport>fingerprint').length || desc.find('encryption').length) {
|
2014-05-26 12:54:17 +00:00
|
|
|
if (sctp.length)
|
2014-05-08 09:26:15 +00:00
|
|
|
tmp.proto = 'DTLS/SCTP';
|
|
|
|
else
|
|
|
|
tmp.proto = 'RTP/SAVPF';
|
2014-03-06 14:28:36 +00:00
|
|
|
} else {
|
|
|
|
tmp.proto = 'RTP/AVPF';
|
|
|
|
}
|
2015-09-21 16:28:49 +00:00
|
|
|
if (!sctp.length) {
|
2014-05-08 09:26:15 +00:00
|
|
|
tmp.fmt = desc.find('payload-type').map(
|
|
|
|
function () { return this.getAttribute('id'); }).get();
|
|
|
|
media += SDPUtil.build_mline(tmp) + '\r\n';
|
2015-09-21 16:28:49 +00:00
|
|
|
} else {
|
2014-05-26 12:54:17 +00:00
|
|
|
media += 'm=application 1 DTLS/SCTP ' + sctp.attr('number') + '\r\n';
|
|
|
|
media += 'a=sctpmap:' + sctp.attr('number') +
|
|
|
|
' ' + sctp.attr('protocol');
|
|
|
|
|
|
|
|
var streamCount = sctp.attr('streams');
|
|
|
|
if (streamCount)
|
|
|
|
media += ' ' + streamCount + '\r\n';
|
|
|
|
else
|
|
|
|
media += '\r\n';
|
2014-05-08 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
media += 'c=IN IP4 0.0.0.0\r\n';
|
2014-05-26 12:54:17 +00:00
|
|
|
if (!sctp.length)
|
2014-05-08 09:26:15 +00:00
|
|
|
media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
|
2014-03-06 14:28:36 +00:00
|
|
|
tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
|
|
|
|
if (tmp.length) {
|
|
|
|
if (tmp.attr('ufrag')) {
|
|
|
|
media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
|
|
|
|
}
|
|
|
|
if (tmp.attr('pwd')) {
|
|
|
|
media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
|
|
|
|
}
|
|
|
|
tmp.find('>fingerprint').each(function () {
|
|
|
|
// FIXME: check namespace at some point
|
|
|
|
media += 'a=fingerprint:' + this.getAttribute('hash');
|
|
|
|
media += ' ' + $(this).text();
|
|
|
|
media += '\r\n';
|
|
|
|
if (this.getAttribute('setup')) {
|
|
|
|
media += 'a=setup:' + this.getAttribute('setup') + '\r\n';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
switch (content.attr('senders')) {
|
|
|
|
case 'initiator':
|
|
|
|
media += 'a=sendonly\r\n';
|
|
|
|
break;
|
|
|
|
case 'responder':
|
|
|
|
media += 'a=recvonly\r\n';
|
|
|
|
break;
|
|
|
|
case 'none':
|
|
|
|
media += 'a=inactive\r\n';
|
|
|
|
break;
|
|
|
|
case 'both':
|
|
|
|
media += 'a=sendrecv\r\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
media += 'a=mid:' + content.attr('name') + '\r\n';
|
|
|
|
|
|
|
|
// <description><rtcp-mux/></description>
|
|
|
|
// see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
|
|
|
|
// and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
|
|
|
|
if (desc.find('rtcp-mux').length) {
|
|
|
|
media += 'a=rtcp-mux\r\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.find('encryption').length) {
|
|
|
|
desc.find('encryption>crypto').each(function () {
|
|
|
|
media += 'a=crypto:' + this.getAttribute('tag');
|
|
|
|
media += ' ' + this.getAttribute('crypto-suite');
|
|
|
|
media += ' ' + this.getAttribute('key-params');
|
|
|
|
if (this.getAttribute('session-params')) {
|
|
|
|
media += ' ' + this.getAttribute('session-params');
|
|
|
|
}
|
|
|
|
media += '\r\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
desc.find('payload-type').each(function () {
|
|
|
|
media += SDPUtil.build_rtpmap(this) + '\r\n';
|
|
|
|
if ($(this).find('>parameter').length) {
|
|
|
|
media += 'a=fmtp:' + this.getAttribute('id') + ' ';
|
2015-09-21 16:28:49 +00:00
|
|
|
media += $(this).find('parameter').map(function () {
|
|
|
|
return (this.getAttribute('name')
|
|
|
|
? (this.getAttribute('name') + '=') : '') +
|
|
|
|
this.getAttribute('value');
|
|
|
|
}).get().join('; ');
|
2014-03-06 14:28:36 +00:00
|
|
|
media += '\r\n';
|
|
|
|
}
|
|
|
|
// xep-0293
|
2015-09-21 16:28:49 +00:00
|
|
|
media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
|
2014-03-06 14:28:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// xep-0293
|
2015-09-21 16:28:49 +00:00
|
|
|
media += self.rtcpFbFromJingle(desc, '*');
|
2014-03-06 14:28:36 +00:00
|
|
|
|
|
|
|
// xep-0294
|
|
|
|
tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
|
|
|
|
tmp.each(function () {
|
|
|
|
media += 'a=extmap:' + this.getAttribute('id') + ' ' + this.getAttribute('uri') + '\r\n';
|
|
|
|
});
|
|
|
|
|
|
|
|
content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
|
2015-09-21 16:28:49 +00:00
|
|
|
var protocol = this.getAttribute('protocol');
|
|
|
|
protocol = (typeof protocol === 'string') ? protocol.toLowerCase(): '';
|
|
|
|
|
|
|
|
if ((self.removeTcpCandidates && protocol === 'tcp') ||
|
|
|
|
(self.removeUdpCandidates && protocol === 'udp')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
media += SDPUtil.candidateFromJingle(this);
|
|
|
|
});
|
|
|
|
|
2014-08-21 09:58:33 +00:00
|
|
|
// XEP-0339 handle ssrc-group attributes
|
2015-09-21 16:28:49 +00:00
|
|
|
content.find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
|
2014-08-21 09:58:33 +00:00
|
|
|
var semantics = this.getAttribute('semantics');
|
|
|
|
var ssrcs = $(this).find('>source').map(function() {
|
|
|
|
return this.getAttribute('ssrc');
|
|
|
|
}).get();
|
|
|
|
|
2015-09-11 03:26:29 +00:00
|
|
|
if (ssrcs.length) {
|
2014-08-21 09:58:33 +00:00
|
|
|
media += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-06 14:28:36 +00:00
|
|
|
tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
|
|
|
|
tmp.each(function () {
|
|
|
|
var ssrc = this.getAttribute('ssrc');
|
|
|
|
$(this).find('>parameter').each(function () {
|
2015-07-10 09:57:20 +00:00
|
|
|
var name = this.getAttribute('name');
|
|
|
|
var value = this.getAttribute('value');
|
|
|
|
value = SDPUtil.filter_special_chars(value);
|
|
|
|
media += 'a=ssrc:' + ssrc + ' ' + name;
|
|
|
|
if (value && value.length)
|
|
|
|
media += ':' + value;
|
2014-03-06 14:28:36 +00:00
|
|
|
media += '\r\n';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return media;
|
2014-04-02 13:48:28 +00:00
|
|
|
};
|
2014-12-26 09:38:10 +00:00
|
|
|
|
|
|
|
|
2015-01-19 09:20:00 +00:00
|
|
|
module.exports = SDP;
|
2014-12-26 09:38:10 +00:00
|
|
|
|