SDP to JSON conversion

1,551 views
Skip to first unread message

SynosiK

unread,
Mar 14, 2012, 5:14:10 PM3/14/12
to discuss...@googlegroups.com
Hi,

I was wondering if any of you guys tried or managed to convert the SDP to a JSON format. If so, how did you accomplished that (I need to do it with javascript)? Even when I use JSON.stringify, I can't get my desired form. I tried to create a javascript function that splices, splits and replaces part of this text into a JSON format and I got quite close, but can't get the quotes properly...below is the raw SDP and after is the format I want.

My SDP:

SDP { "messageType" : "OFFER", "offererSessionId" : "KwEJD5iVC+e/eCI5wM6aYW9NkOJfqevF", "sdp" : "v=0\r\no=- 0 0 IN IP4 127.0.0.1\r\ns=\r\nc=IN IP4 0.0.0.0\r\nt=0 0\r\nm=audio 1 RTP/AVPF 103 104 0 8 106 105 13 126\r\na=candidate:1 2 udp 1 142.133.114.37 52750 typ host name rtcp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username CDNgSSjbVarXdPcz password IBWwVx1aF23u4W9S generation 0\r\na=candidate:1 1 udp 1 142.133.114.37 52751 typ host name rtp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username 3gHYfBEmkNNW6n/O password kvusRsYG1a2ogCYj generation 0\r\na=mid:audio\r\na=rtcp-mux\r\na=crypto:0 AES_CM_128_HMAC_SHA1_32 inline:aIz76FzkcxEFR1wp+u338L2+quDHyRN7Zf4MRuhl \r\na=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:Cb2lRZHfCaT3IY+t+/XM7b/f0UVDULozbudPjGB1 \r\na=rtpmap:103 ISAC/16000\r\na=rtpmap:104 ISAC/32000\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:106 CN/32000\r\na=rtpmap:105 CN/16000\r\na=rtpmap:13 CN/8000\r\na=rtpmap:126 telephone-event/8000\r\nm=video 1 RTP/AVPF 100 101 102\r\na=candidate:1 2 udp 1 142.133.114.37 52752 typ host name video_rtcp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username CQ2q/EVHZkANkREk password +XQDCEMPe5at3IYa generation 0\r\na=candidate:1 1 udp 1 142.133.114.37 52753 typ host name video_rtp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username tTIVjfE995Hwz5Ca password r1k7MH14b0DRUske generation 0\r\na=mid:video\r\na=rtcp-mux\r\na=crypto:0 AES_CM_128_HMAC_SHA1_80 inline:cwYC2EFHWhoncZLv3yh5NTTROtt71q0Q1Ooq29HZ \r\na=rtpmap:100 VP8/90000\r\na=rtpmap:101 red/90000\r\na=rtpmap:102 ulpfec/90000\r\n", "seq" : 1, "tieBreaker" : 3488104853 } 


JSON format I need:

{
"to": "tel:+1234567890",
"sdp":
[
{
"m": "audio 9 TCP/RTMP 8",
"attributes":
[
{"a": "sendrecv"},
{"a": "rtpmap:8 pcma/8000/1"},
{"a": "setup:active"}
]
},
{
"m": "video 9 TCP/RTMP 98",
"attributes":
[
{"a": "sendrecv"},
{"a": "rtpmap:98 H264/90000"},
{"a": "fmtp:98 profile-levelid=<xxxxx>;packetization-mode=1"},
{"a": "setup:active"}
]
}
]
}

How close I got:

{
"to": "tel:+1234567890",
"sdp":
[
"v"=0 "o":- 0 0 IN IP4 127.0.0.1 "s": "c":IN IP4 0.0.0.0 "t":0 0 { "m":audio 1 RTP/AVPF 103 104 0 8 106 105 13 126 "a":candidate:1 2 udp 1 142.133.114.37 63623 typ host name rtcp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username 6Burg/ktKeNbXAZZ password s/FjHBs6pJz80OYF generation 0 "a":candidate:1 1 udp 1 142.133.114.37 63624 typ host name rtp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username NiJSYfqXHkPWcIzq password z9Isd6MZvk+Xbsso generation 0 "a":mid:audio "a":rtcp-mux "a":crypto:0 AES_CM_128_HMAC_SHA1_32 inline:HDRtzS+GAhHlcC/IV4cygJ86cFyw5fITnVTikDif "a":crypto:1 AES_CM_128_HMAC_SHA1_80 inline:H1YdJRTos3B6qNaQWI1al40qQ2EfmfdP72eYo4ec "a":rtpmap:103 ISAC/16000 "a":rtpmap:104 ISAC/32000 "a":rtpmap:0 PCMU/8000 "a":rtpmap:8 PCMA/8000 "a":rtpmap:106 CN/32000 "a":rtpmap:105 CN/16000 "a":rtpmap:13 CN/8000 "a":rtpmap:126 telephone-event/8000 }
{ "m":video 1 RTP/AVPF 100 101 102 "a":candidate:1 2 udp 1 142.133.114.37 63625 typ host name video_rtcp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username 0o5neglsb2ufwZ5E password 4SmggnpoXUMzDfoY generation 0 "a":candidate:1 1 udp 1 142.133.114.37 63626 typ host name video_rtp network_name {A65B2BBB-6014-43B5-A924-4E2BA43445E5} username 36bOxahLt5+E5wu+ password LVel898PxYNWmKtN generation 0 "a":mid:video "a":rtcp-mux "a":crypto:0 AES_CM_128_HMAC_SHA1_80 inline:ue73K30+GMx8DHQEl5+UU7DjYbQCo0/e6qBc86+l "a":rtpmap:100 VP8/90000 "a":rtpmap:101 red/90000 "a":rtpmap:102 ulpfec/90000 }
]
}

Any tips would be appreciated. I am quite bad in javascript and perhaps someone has figured it out. 

Thanks

Don Park

unread,
Mar 14, 2012, 8:09:21 PM3/14/12
to discuss-webrtc
Try this on for size:

https://gist.github.com/2040569

It's easier to follow in CoffeeScript but here is compiled javascript
version:

var parseSDP, readMedia, readSession, readTime;

parseSDP = function(text) {
var lines;
if (!text) return;
lines = text.split('\r\n');
return readSession(lines);
};

readSession = function(lines) {
var line, name, session, value, _ref, _ref2, _ref3;
session = {};
line = null;
while (line = lines.shift()) {
name = line.charAt(0);
value = line.slice(2);
if (name === 'a' || name === 'b') {
((_ref = session[name]) != null ? _ref : session[name] =
[]).push(value);
} else if (name === 't') {
((_ref2 = session.t) != null ? _ref2 : session.t =
[]).push(readTime(lines, {
t: value
}));
} else if (name === 'm') {
((_ref3 = session.m) != null ? _ref3 : session.m =
[]).push(readMedia(lines, {
m: value
}));
} else if (name) {
session[name] = value;
}
}
return session;
};

readTime = function(lines, entry) {
var line, _ref;
line = null;
while (line = lines.shift()) {
if (line.charAt(0) === 'r') {
((_ref = entry.r) != null ? _ref : entry.r =
[]).push(line.slice(2));
} else {
lines.unshift(line);
break;
}
}
return entry;
};

readMedia = function(lines, entry) {
var line, name, value, _ref;
line = null;
while (line = lines.shift()) {
name = line.charAt(0);
value = line.slice(2);
if (name === 'm') {
lines.unshift(line);
break;
} else if (name === 'a' || name === 'b') {
((_ref = entry[name]) != null ? _ref : entry[name] =
[]).push(value);
} else if (name) {
entry[name] = value;
}
}
return entry;
};

Don Park

unread,
Mar 14, 2012, 8:17:13 PM3/14/12
to discuss-webrtc
FYI, output of my parser is slightly different from what you wanted.

Example (formatted):

{
"v": "0",
"o": "- 0 0 IN IP4 127.0.0.1",
"s": "",
"c": "IN IP4 0.0.0.0",
"t": [{
"t": "0 0"
}],
"m": [{
"m": "audio 1 RTP/AVPF 103 104 0 8 106 105 13 126",
"a": ["candidate:1 2 udp 1 192.168.1.100 61402 typ host name
rtcp network_name en0 username hsWuSQJxx7przmb8 password HM
+ZkeNcG3HezSNI generation 0", "candidate:1 2 udp 1 10.211.55.2 63408
typ host name rtcp network_name vnic0 username D7LmfDa9J4lfdUL8
password W1F7TNJKcbuygaQu generation 0", "candidate:1 2 udp 1
10.37.129.2 50969 typ host name rtcp network_name vnic1 username
A429SJy2e9JctPUy password EAmQrEw3y7hi2iRd generation 0", "candidate:1
1 udp 1 192.168.1.100 63992 typ host name rtp network_name en0
username hLfawKZC5ydJsA+F password ZVpAyqJMT/nW/UW+ generation 0",
"candidate:1 1 udp 1 10.211.55.2 53722 typ host name rtp network_name
vnic0 username tcOmDvXJh/pPhNRe password dcWyymdwEPK3CXo5 generation
0", "candidate:1 1 udp 1 10.37.129.2 58877 typ host name rtp
network_name vnic1 username sTLJSez0eM0VUfUk password Z+1Pyi0MPTQtITP1
generation 0", "candidate:1 2 udp 0.9 67.180.167.129 51373 typ srflx
name rtcp network_name en0 username igWS0/5m/JextOjs password ohegdLCD/
BHEPUFl generation 0", "candidate:1 1 udp 0.9 67.180.167.129 61966 typ
srflx name rtp network_name en0 username 0xOqtPVwKn/YdUN0 password
5nZC8cB4NMHgjk4m generation 0", "mid:audio", "rtcp-mux", "crypto:0
AES_CM_128_HMAC_SHA1_32 inline:FTpXoZMNqmtVOAEZr434tV6gcO6fh882FOBw9/
ux ", "crypto:1 AES_CM_128_HMAC_SHA1_80
inline:kFpSHLGsmijz50QGy50ECtsgeB04DQLi3WSITc13 ", "rtpmap:103 ISAC/
16000", "rtpmap:104 ISAC/32000", "rtpmap:0 PCMU/8000", "rtpmap:8 PCMA/
8000", "rtpmap:106 CN/32000", "rtpmap:105 CN/16000", "rtpmap:13 CN/
8000", "rtpmap:126 telephone-event/8000", "ssrc:802577871
cname:hUpUERBYNu7zcgKg mslabel:bc4863d3-f633-40f6-910e-4ef50a8de58a
label:Default"]
},
{
"m": "video 1 RTP/AVPF 100 101 102",
"a": ["candidate:1 2 udp 1 192.168.1.100 58350 typ host name
video_rtcp network_name en0 username mZdqlA3r4BHEOfQA password UhN
+7FVH7zCfYwSa generation 0", "candidate:1 2 udp 1 10.211.55.2 49657
typ host name video_rtcp network_name vnic0 username kUOxF+pV4GqEWzOZ
password WvnoU3KKQXo26W7R generation 0", "candidate:1 2 udp 1
10.37.129.2 51747 typ host name video_rtcp network_name vnic1 username
tukI5+SblDl80FTY password oOL1FgZ9XnKZ2tQC generation 0", "candidate:1
1 udp 1 192.168.1.100 50226 typ host name video_rtp network_name en0
username JNoUJq5lIhAUx7ql password RoBMWpLHXMvxVdaV generation 0",
"candidate:1 1 udp 1 10.211.55.2 62037 typ host name video_rtp
network_name vnic0 username BbhA+poanGC1U/an password ZjRVQ4oRZshmeOhw
generation 0", "candidate:1 1 udp 1 10.37.129.2 60611 typ host name
video_rtp network_name vnic1 username c+ZxfiliMb0Hl4qG password
JpC5624Cjxmfapt+ generation 0", "candidate:1 2 udp 0.9 67.180.167.129
52928 typ srflx name video_rtcp network_name en0 username
xXI4NSpdGpZyb0c6 password P341wVX1fAstN0ZL generation 0", "candidate:1
1 udp 0.9 67.180.167.129 63478 typ srflx name video_rtp network_name
en0 username 0Og8leCYCyNr2Us7 password 4vziR+9dYETfB+Qr generation 0",
"mid:video", "rtcp-mux", "crypto:0 AES_CM_128_HMAC_SHA1_80 inline:
7yGMmJ+26YtVXSKXC2sIu/wg6cbf6fAd65kbAmhd ", "rtpmap:100 VP8/90000",
"rtpmap:101 red/90000", "rtpmap:102 ulpfec/90000", "ssrc:1913534270
cname:hUpUERBYNu7zcgKg mslabel:bc4863d3-f633-40f6-910e-4ef50a8de58a
label:Built-in iSight"]
}]
}

Michael Weibel

unread,
Mar 15, 2012, 2:06:16 PM3/15/12
to discuss...@googlegroups.com
You could try using my sdpToJingle lib: http://github.com/mweibel/sdpToJingle
It's basicly converting the sdp into an internal representation (which you want to have) and then converts this to jingle (which you don't need).
You should be able to get the internal representation.

Robert Wolff (GoCast)

unread,
Mar 15, 2012, 2:19:36 PM3/15/12
to discuss...@googlegroups.com
I'd be interested to see the "easier to follow CoffeeScript" if you don't mind. :-)  Been looking at Coffeescript a bit lately.

Thanks,
bob

Don Park

unread,
Mar 15, 2012, 8:07:40 PM3/15/12
to discuss-webrtc
I guess you didn't get the 'gist' of it. ;-p

https://gist.github.com/2040569

- Don

Robert Wolff (GoCast)

unread,
Mar 15, 2012, 9:07:02 PM3/15/12
to discuss...@googlegroups.com
Ha! Thanks Don. I thought the gist you sent was another copy of the Javascript version. :-)  That'll teach me to actually LOOK.

Thanks!
bob

Adrian Ber

unread,
Nov 25, 2015, 3:03:10 AM11/25/15
to discuss-webrtc
I created a SDP parser to JSON: https://github.com/beradrian/sdpparser. Even though the reply is quite late, I was thinking that you might want to try it. I will value your feedback.

Iñaki Baz Castillo

unread,
Nov 25, 2015, 12:40:31 PM11/25/15
to discuss...@googlegroups.com
Great. Just wondering if you knew about sdp-transform [*] which is a
SDP parser and writer. It is not based on a PEGjs grammar but on pure
JS regular expressions.


[*] https://github.com/clux/sdp-transform


--
Iñaki Baz Castillo
<i...@aliax.net>
Reply all
Reply to author
Forward
0 new messages