Skip to content

Commit

Permalink
Fix sdp parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeny-nadymov committed Apr 2, 2021
1 parent eab6aeb commit 0c15b4e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 70 deletions.
36 changes: 18 additions & 18 deletions src/Calls/P2P/ChromeP2PSdpBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ a=msid-semantic: WMS *`;
const streamName = 'stream' + media.map(x => x.ssrc).join('_');
for (let i = 0; i < media.length; i++) {
const m = media[i];
const { type, ssrc, ssrcGroup, types, dir, mid, extmap } = m;
const { type, ssrc, ssrcGroups, payloadTypes, dir, mid, rtpExtensions } = m;
switch (type) {
case 'application': {
const { port, maxSize } = m;
Expand All @@ -51,12 +51,12 @@ a=max-message-size:${maxSize}`;
}
case 'audio': {
sdp += `
m=audio 56930 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=audio 56930 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-options:trickle
a=mid:${mid}`;
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
if (dir) {
sdp += `
a=${dir}`;
Expand All @@ -67,19 +67,19 @@ a=msid:${streamName} audio${ssrc}`;
}
sdp += `
a=rtcp-mux`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);

break;
}
case 'video': {
sdp += `
m=video 61986 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=video 61986 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-options:trickle
a=mid:${mid}`;
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
if (dir) {
sdp += `
a=${dir}`;
Expand All @@ -91,8 +91,8 @@ a=msid:${streamName} video${ssrc}`;
sdp += `
a=rtcp-mux
a=rtcp-rsize`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);
break;
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ a=msid-semantic: WMS *`;
const streamName = 'stream' + media.map(x => x.ssrc).join('_');
for (let i = 0; i < media.length; i++) {
const m = media[i];
const { type, mid, ssrc, ssrcGroup, types, dir, extmap } = m;
const { type, mid, ssrc, ssrcGroups, payloadTypes, dir, rtpExtensions } = m;
switch (type) {
case 'application': {
const { port, maxSize } = m;
Expand All @@ -146,12 +146,12 @@ a=max-message-size:${maxSize}`;
}
case 'audio': {
sdp += `
m=audio 56930 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=audio 56930 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-options:trickle
a=mid:${mid}`;
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
if (dir) {
sdp += `
a=${dir}`;
Expand All @@ -162,18 +162,18 @@ a=msid:${streamName} audio${ssrc}`;
}
sdp += `
a=rtcp-mux`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);
break;
}
case 'video': {
sdp += `
m=video 61986 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=video 61986 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-options:trickle
a=mid:${mid}`;
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
if (dir) {
sdp += `
a=${dir}`;
Expand All @@ -185,8 +185,8 @@ a=msid:${streamName} video${ssrc}`;
sdp += `
a=rtcp-mux
a=rtcp-rsize`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);
break;
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/Calls/P2P/FirefoxP2PSdpBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ a=msid-semantic:WMS *`;
const streamName = 'stream' + media.map(x => x.ssrc).join('_');
for (let i = 0; i < media.length; i++) {
const m = media[i];
const { type, mid, ssrc, ssrcGroup, types, dir, extmap } = m;
const { type, mid, ssrc, ssrcGroups, payloadTypes, dir, rtpExtensions } = m;
switch (type) {
case 'application': {
const { port, maxSize } = m;
Expand All @@ -50,36 +50,36 @@ a=max-message-size:${maxSize}`;
}
case 'audio': {
sdp += `
m=audio 9 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=audio 9 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=mid:${mid}`;
if (dir) {
sdp += `
a=${dir}`;
}
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
sdp += `
a=rtcp-mux`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);

break;
}
case 'video': {
sdp += `
m=video 9 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=video 9 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=mid:${mid}`;
if (dir) {
sdp += `
a=${dir}`;
}
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
sdp += `
a=rtcp-mux
a=rtcp-rsize`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);

break;
}
Expand Down Expand Up @@ -118,7 +118,7 @@ a=msid-semantic:WMS *`;
const streamName = 'stream' + media.map(x => x.ssrc).join('_');
for (let i = 0; i < media.length; i++) {
const m = media[i];
const { type, mid, ssrc, ssrcGroup, types, dir, extmap } = m;
const { type, mid, ssrc, ssrcGroups, payloadTypes, dir, rtpExtensions } = m;
switch (type) {
case 'application': {
const { port, maxSize } = m;
Expand All @@ -133,36 +133,36 @@ a=max-message-size:${maxSize}`;
}
case 'audio': {
sdp += `
m=audio 9 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=audio 9 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=mid:${mid}`;
if (dir) {
sdp += `
a=${dir}`;
}
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
sdp += `
a=rtcp-mux`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);

break;
}
case 'video': {
sdp += `
m=video 9 UDP/TLS/RTP/SAVPF ${types.map(x => x.id).join(' ')}
m=video 9 UDP/TLS/RTP/SAVPF ${payloadTypes.map(x => x.id).join(' ')}
c=IN IP4 0.0.0.0
a=mid:${mid}`;
if (dir) {
sdp += `
a=${dir}`;
}
sdp += addExtmap(extmap);
sdp += addExtmap(rtpExtensions);
sdp += `
a=rtcp-mux
a=rtcp-rsize`;
sdp += addPayloadTypes(types);
sdp += addSsrc(type, ssrc, ssrcGroup, streamName);
sdp += addPayloadTypes(payloadTypes);
sdp += addSsrc(type, ssrc, ssrcGroups, streamName);
break;
}
}
Expand Down
34 changes: 18 additions & 16 deletions src/Calls/P2P/P2PSdpBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ export function p2pParseSdp(sdp) {
type: lookup('m=', true, mediaIndex, nextMediaIndex).split(' ')[0],
mid: lookup('a=mid:', true, mediaIndex, nextMediaIndex),
dir: findDirection(mediaIndex, nextMediaIndex),
extmap,
types
rtpExtensions: extmap,
payloadTypes: types
}

if (media.type === 'application') {
Expand All @@ -189,8 +189,8 @@ export function p2pParseSdp(sdp) {
for (let i = mediaIndex; i < lineTo; i++) {
const line = lines[i];
if (line.startsWith('a=extmap:')) {
const [ id, value ] = line.substr('a=extmap:'.length).split(' ');
extmap.push({ [id]: value });
const [ id, uri ] = line.substr('a=extmap:'.length).split(' ');
extmap.push({ id: parseInt(id), uri });
} else if (line.startsWith('a=fmtp:')) {
const [ id, str ] = line.substr('a=fmtp:'.length).split(' ');
const arr = str.split(';').map(x => {
Expand Down Expand Up @@ -232,10 +232,10 @@ export function p2pParseSdp(sdp) {
const ssrcGroup = lookup('a=ssrc-group:', false, mediaIndex, nextMediaIndex);
if (ssrcGroup) {
const [ semantics, ...ssrcs ] = ssrcGroup.split(' ');
media.ssrcGroup = {
media.ssrcGroups = [{
semantics,
ssrcs: ssrcs.map(x => parseInt(x))
}
}]
}

info.media.push(media);
Expand All @@ -260,10 +260,9 @@ export function addExtmap(extmap) {

for (let j = 0; j < extmap.length; j++) {
const ext = extmap[j];
Object.getOwnPropertyNames(ext).forEach(x => {
sdp += `
a=extmap:${x} ${ext[x]}`;
});
const { id, uri } = ext;
sdp += `
a=extmap:${id} ${uri}`;
}

return sdp;
Expand Down Expand Up @@ -300,20 +299,23 @@ a=fmtp:${id} ${fmtp.join(';')}`;
return sdp;
}

export function addSsrc(type, ssrc, ssrcGroup, streamName) {
export function addSsrc(type, ssrc, ssrcGroups, streamName) {
let sdp = '';

if (ssrcGroup && ssrcGroup.ssrcs.length > 0) {
sdp += `
if (ssrcGroups && ssrcGroups.length > 0) {
ssrcGroups.forEach(ssrcGroup => {
if (ssrcGroup && ssrcGroup.ssrcs.length > 0) {
sdp += `
a=ssrc-group:${ssrcGroup.semantics} ${ssrcGroup.ssrcs.join(' ')}`;
ssrcGroup.ssrcs.forEach(ssrc => {
sdp += `
ssrcGroup.ssrcs.forEach(ssrc => {
sdp += `
a=ssrc:${ssrc} cname:stream${ssrc}
a=ssrc:${ssrc} msid:${streamName} ${type}${ssrc}
a=ssrc:${ssrc} mslabel:${type}${ssrc}
a=ssrc:${ssrc} label:${type}${ssrc}`;
});
}
});

} else if (ssrc) {
sdp += `
a=ssrc:${ssrc} cname:stream${ssrc}
Expand Down
Loading

0 comments on commit 0c15b4e

Please sign in to comment.