Last active
March 21, 2019 14:20
-
-
Save Mazuh/700f3fa048838e16a4b7d19359b7c65e to your computer and use it in GitHub Desktop.
Functional and pure helper to assure video comes first on SDP media lines (tho not so pragmatic). Forked from: https://gist.github.com/edolix/73b2e085104250691a421b8f3678c86c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function reorderSdpMediaLines(sdp) { | |
const lineBreaker = '\r\n'; | |
const parsed = sdp.split(lineBreaker).reduce((parser, line) => { | |
const reading = (line.startsWith('m=video') && 'video') | |
|| (line.startsWith('m=audio') && 'audio') | |
|| parser.reading; | |
if (line === '') { | |
return parser; | |
} | |
if (reading === 'video') { | |
return { ...parser, reading, videoLines: [...parser.videoLines, line] }; | |
} | |
if (reading === 'audio') { | |
return { ...parser, reading, audioLines: [...parser.audioLines, line] }; | |
} | |
return { ...parser, reading, otherLines: [...parser.otherLines, line] }; | |
}, { | |
reading: '', | |
videoLines: [], | |
audioLines: [], | |
otherLines: [], | |
}); | |
const resultingLines = [ | |
...parsed.otherLines, | |
...parsed.videoLines, | |
...parsed.audioLines, | |
'', | |
]; | |
return resultingLines.join(lineBreaker); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { reorderSdpMediaLines } from './sdp-handler'; | |
const SDP_VIDEO_FIRST = 'v=0\r\no=mozilla...THIS_IS_SDPARTA-64.0 6615912393115535091 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 39:FD:F3:EC:58:BB:E0:FF:C4:A9:F0:D8:B6:89:B6:2D:16:F4:FB:45:2E:36:2F:C4:A5:EA:8C:6C:20:E7:98:7D\r\na=group:BUNDLE 0 1\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=video 33831 UDP/TLS/RTP/SAVPF 120 121 126 97\r\nc=IN IP4 172.16.19.165\r\na=candidate:0 1 UDP 2122252543 172.16.19.165 33831 typ host\r\na=candidate:1 1 UDP 2122187007 172.17.0.1 51764 typ host\r\na=candidate:2 1 TCP 2105524479 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 1 TCP 2105458943 172.17.0.1 9 typ host tcptype active\r\na=candidate:0 2 UDP 2122252542 172.16.19.165 36827 typ host\r\na=candidate:1 2 UDP 2122187006 172.17.0.1 41928 typ host\r\na=candidate:2 2 TCP 2105524478 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 2 TCP 2105458942 172.17.0.1 9 typ host tcptype active\r\na=sendrecv\r\na=end-of-candidates\r\na=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:4 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:5 urn:ietf:params:rtp-hdrext:toffset\r\na=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1\r\na=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1\r\na=fmtp:120 max-fs=12288;max-fr=60\r\na=fmtp:121 max-fs=12288;max-fr=60\r\na=ice-pwd:c3068c1ae5cf4748605b9795f133a14f\r\na=ice-ufrag:ff542923\r\na=mid:0\r\na=msid:{812eca7b-e12e-45b0-840f-b2d6a2e83e47} {dcfb360f-0b24-4005-b9a9-bcaf06ac7bda}\r\na=rtcp:36827 IN IP4 172.16.19.165\r\na=rtcp-fb:120 nack\r\na=rtcp-fb:120 nack pli\r\na=rtcp-fb:120 ccm fir\r\na=rtcp-fb:120 goog-remb\r\na=rtcp-fb:121 nack\r\na=rtcp-fb:121 nack pli\r\na=rtcp-fb:121 ccm fir\r\na=rtcp-fb:121 goog-remb\r\na=rtcp-fb:126 nack\r\na=rtcp-fb:126 nack pli\r\na=rtcp-fb:126 ccm fir\r\na=rtcp-fb:126 goog-remb\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=rtcp-fb:97 ccm fir\r\na=rtcp-fb:97 goog-remb\r\na=rtcp-mux\r\na=rtpmap:120 VP8/90000\r\na=rtpmap:121 VP9/90000\r\na=rtpmap:126 H264/90000\r\na=rtpmap:97 H264/90000\r\na=setup:actpass\r\na=ssrc:3497616421 cname:{5daee4d9-07ea-4b59-a411-6c68c04d6e71}\r\nm=audio 58701 UDP/TLS/RTP/SAVPF 109 9 0 8 101\r\nc=IN IP4 172.16.19.165\r\na=candidate:0 1 UDP 2122252543 172.16.19.165 58701 typ host\r\na=candidate:1 1 UDP 2122187007 172.17.0.1 51013 typ host\r\na=candidate:2 1 TCP 2105524479 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 1 TCP 2105458943 172.17.0.1 9 typ host tcptype active\r\na=candidate:0 2 UDP 2122252542 172.16.19.165 48918 typ host\r\na=candidate:1 2 UDP 2122187006 172.17.0.1 50769 typ host\r\na=candidate:2 2 TCP 2105524478 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 2 TCP 2105458942 172.17.0.1 9 typ host tcptype active\r\na=sendrecv\r\na=end-of-candidates\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2/recvonly urn:ietf:params:rtp-hdrext:csrc-audio-level\r\na=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=fmtp:109 maxplaybackrate=48000;stereo=1;useinbandfec=1\r\na=fmtp:101 0-15\r\na=ice-pwd:c3068c1ae5cf4748605b9795f133a14f\r\na=ice-ufrag:ff542923\r\na=mid:1\r\na=msid:{812eca7b-e12e-45b0-840f-b2d6a2e83e47} {3083ce40-71a5-4d08-9928-2ca7cbd2ffe4}\r\na=rtcp:48918 IN IP4 172.16.19.165\r\na=rtcp-mux\r\na=rtpmap:109 opus/48000/2\r\na=rtpmap:9 G722/8000/1\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:101 telephone-event/8000\r\na=setup:actpass\r\na=ssrc:4225865926 cname:{5daee4d9-07ea-4b59-a411-6c68c04d6e71}\r\n'; | |
const SDP_AUDIO_FIRST = 'v=0\r\no=mozilla...THIS_IS_SDPARTA-64.0 6615912393115535091 0 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 39:FD:F3:EC:58:BB:E0:FF:C4:A9:F0:D8:B6:89:B6:2D:16:F4:FB:45:2E:36:2F:C4:A5:EA:8C:6C:20:E7:98:7D\r\na=group:BUNDLE 0 1\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=audio 58701 UDP/TLS/RTP/SAVPF 109 9 0 8 101\r\nc=IN IP4 172.16.19.165\r\na=candidate:0 1 UDP 2122252543 172.16.19.165 58701 typ host\r\na=candidate:1 1 UDP 2122187007 172.17.0.1 51013 typ host\r\na=candidate:2 1 TCP 2105524479 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 1 TCP 2105458943 172.17.0.1 9 typ host tcptype active\r\na=candidate:0 2 UDP 2122252542 172.16.19.165 48918 typ host\r\na=candidate:1 2 UDP 2122187006 172.17.0.1 50769 typ host\r\na=candidate:2 2 TCP 2105524478 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 2 TCP 2105458942 172.17.0.1 9 typ host tcptype active\r\na=sendrecv\r\na=end-of-candidates\r\na=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\na=extmap:2/recvonly urn:ietf:params:rtp-hdrext:csrc-audio-level\r\na=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=fmtp:109 maxplaybackrate=48000;stereo=1;useinbandfec=1\r\na=fmtp:101 0-15\r\na=ice-pwd:c3068c1ae5cf4748605b9795f133a14f\r\na=ice-ufrag:ff542923\r\na=mid:1\r\na=msid:{812eca7b-e12e-45b0-840f-b2d6a2e83e47} {3083ce40-71a5-4d08-9928-2ca7cbd2ffe4}\r\na=rtcp:48918 IN IP4 172.16.19.165\r\na=rtcp-mux\r\na=rtpmap:109 opus/48000/2\r\na=rtpmap:9 G722/8000/1\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:101 telephone-event/8000\r\na=setup:actpass\r\na=ssrc:4225865926 cname:{5daee4d9-07ea-4b59-a411-6c68c04d6e71}\r\nm=video 33831 UDP/TLS/RTP/SAVPF 120 121 126 97\r\nc=IN IP4 172.16.19.165\r\na=candidate:0 1 UDP 2122252543 172.16.19.165 33831 typ host\r\na=candidate:1 1 UDP 2122187007 172.17.0.1 51764 typ host\r\na=candidate:2 1 TCP 2105524479 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 1 TCP 2105458943 172.17.0.1 9 typ host tcptype active\r\na=candidate:0 2 UDP 2122252542 172.16.19.165 36827 typ host\r\na=candidate:1 2 UDP 2122187006 172.17.0.1 41928 typ host\r\na=candidate:2 2 TCP 2105524478 172.16.19.165 9 typ host tcptype active\r\na=candidate:3 2 TCP 2105458942 172.17.0.1 9 typ host tcptype active\r\na=sendrecv\r\na=end-of-candidates\r\na=extmap:3 urn:ietf:params:rtp-hdrext:sdes:mid\r\na=extmap:4 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\na=extmap:5 urn:ietf:params:rtp-hdrext:toffset\r\na=fmtp:126 profile-level-id=42e01f;level-asymmetry-allowed=1;packetization-mode=1\r\na=fmtp:97 profile-level-id=42e01f;level-asymmetry-allowed=1\r\na=fmtp:120 max-fs=12288;max-fr=60\r\na=fmtp:121 max-fs=12288;max-fr=60\r\na=ice-pwd:c3068c1ae5cf4748605b9795f133a14f\r\na=ice-ufrag:ff542923\r\na=mid:0\r\na=msid:{812eca7b-e12e-45b0-840f-b2d6a2e83e47} {dcfb360f-0b24-4005-b9a9-bcaf06ac7bda}\r\na=rtcp:36827 IN IP4 172.16.19.165\r\na=rtcp-fb:120 nack\r\na=rtcp-fb:120 nack pli\r\na=rtcp-fb:120 ccm fir\r\na=rtcp-fb:120 goog-remb\r\na=rtcp-fb:121 nack\r\na=rtcp-fb:121 nack pli\r\na=rtcp-fb:121 ccm fir\r\na=rtcp-fb:121 goog-remb\r\na=rtcp-fb:126 nack\r\na=rtcp-fb:126 nack pli\r\na=rtcp-fb:126 ccm fir\r\na=rtcp-fb:126 goog-remb\r\na=rtcp-fb:97 nack\r\na=rtcp-fb:97 nack pli\r\na=rtcp-fb:97 ccm fir\r\na=rtcp-fb:97 goog-remb\r\na=rtcp-mux\r\na=rtpmap:120 VP8/90000\r\na=rtpmap:121 VP9/90000\r\na=rtpmap:126 H264/90000\r\na=rtpmap:97 H264/90000\r\na=setup:actpass\r\na=ssrc:3497616421 cname:{5daee4d9-07ea-4b59-a411-6c68c04d6e71}\r\n'; | |
describe('reorderSdpMediaLines: helper to assure video comes first on sdp media lines', function () { | |
it('reorder sdp media lines for video to come first', function() { | |
expect(reorderSdpMediaLines(SDP_AUDIO_FIRST)).toEqual(SDP_VIDEO_FIRST); | |
}); | |
it('if video already comes first, do nothing', function() { | |
expect(reorderSdpMediaLines(SDP_VIDEO_FIRST)).toEqual(SDP_VIDEO_FIRST); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment