4

I want to have a method at the end that can set VP9 or H.264 as preferred video codec in a SDP.

So I am looking for the m line in my SDP:

m=video 9 UDP/TLS/RTP/SAVPF 96 98 100 102 127 97 99 101 125

Console log of my SDP:

Screenshot of console log

In this case I would get and use VP8 (96) as video codec instead of VP9 (98). So I want to check if 98/VP9 is possible/available and want to set it at the beginning/first position to actually use it.

What I got so far:

if(sdpOrigin == 'local') {
    let lines = sdp.split('\n').map(l => l.trim());
    lines.forEach(function(line) {
        if (line.indexOf('m=video') === 0) {
            let parts = line.substr(28); // Should be avoided!
            let vp9_order = parts.indexOf("98");
            let array = parts.split(/\s+/);
            console.log("array", array); // 96 98 100 102 127 97 99 101 125
            if (vp9_order > 0) {
                array.splice(vp9_order, 1);
                array.unshift("98");
            }
            console.log("array-new", array); // 98 96 100 102 127 97 99 101 125

            // How do I update my SDP now with the new codec order?

        }
    })
}

This approach is bad in my opinion, because I get my desired m line but I do a fix substring at the position '28', so it will break if something before changes.

At the end I should have the following "m line" in my SDP:

m=video 9 UDP/TLS/RTP/SAVPF 98 96 100 102 127 97 99 101 125

Can somebody help me with that?

Eric
  • 6,563
  • 5
  • 42
  • 66
mrks
  • 5,439
  • 11
  • 52
  • 74
  • Could you please let know what the rule is? If a line starts with `m=video` and contains `98` enclosed with spaces? – Wiktor Stribiżew Mar 17 '17 at 12:37
  • The line has to start with "m=video" - there is only line like this in the sdp. So I want to check if "98" is available in my string. If its available, I want to check if it is on the first place, if it is not, I want to do it (swap places). – mrks Mar 17 '17 at 13:14

3 Answers3

3

You should split the line by white space first, to break it into the appropriate fields per the SDP specification:

let fields = line.split(/\s+/);
if (fields[0] === 'm=video') {
    let [ type, port, proto, ...formats] = fields;

    let vp9_order = formats.indexOf("98");
    if (vp9_order > 0) {
        formats.splice(vp9_order, 1);  // remove from existing position
        formats.unshift("98");         // and prepend
    }
    line = [ type, port, proto, ...formats].join(' ');
}
Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • This is how my sdp variable already looks like: https://abload.de/img/screenshot2017-03-17akhs0y.png So when I use your code, I formats.includes doesnt find "98" so no log message. – mrks Mar 17 '17 at 13:12
  • Ah, you've completely changed the question :( Your question is unclear about what you want to happen if 98 doesn't appear in the list at all. – Alnitak Mar 17 '17 at 13:38
  • Well actually not really. My problem is now, how I change the order of the string (swap places) when I find "98" in the m line. – mrks Mar 19 '17 at 12:36
  • Updated my question because your answer did not worked for me @Alnitak – mrks Mar 20 '17 at 10:46
  • You keep moving the goal posts... What I'd already given you should have been more than sufficient to allow you to finish the job. Please bear in mind that your first version of the question _only asked how to avoid the magic 28 constant in your code_, with _nothing about reconstructing the SDP_. – Alnitak Mar 20 '17 at 13:50
  • Hi guys, you're splicing only 1 character from the string. '98' are 2 characters – Oskar Mar 23 '17 at 16:27
  • 2
    @Oskar nope - in the code above `formats` is an array, not a string - note the `...` prefix which is an ES6 destructuring assignment that assigns `type` to the zeroth element, `port` to #1, `proto` to #2, and `formats` to the remainder of the array. – Alnitak Mar 23 '17 at 17:03
  • @Alnitak, yes you're right, sorry for confusion. I know the spread operator, just missed the first line with 'split' – Oskar Mar 23 '17 at 17:27
2

This method can be used to modify SDP yourself. You can modify SDP to force h264, vp9 or vp8 codecs.

<script src="https://cdn.webrtc-experiment.com/CodecsHandler.js"></script>
sdp = CodecsHandler.preferCodec(sdp, 'h264');
sdp = CodecsHandler.preferCodec(sdp, 'vp8');
sdp = CodecsHandler.preferCodec(sdp, 'vp9');
user233428
  • 177
  • 9
1

Did it like this:

// Returns a new m= line with the specified codec as the first one.
function setDefaultCodec(mLine, payload) {
  var elements = mLine.split(' ');

  // Just copy the first three parameters; codec order starts on fourth.
  var newLine = elements.slice(0, 3);

  // Put target payload first and copy in the rest.
  newLine.push(payload);
  for (var i = 3; i < elements.length; i++) {
    if (elements[i] !== payload) {
      newLine.push(elements[i]);
    }
  }
  return newLine.join(' ');
}
mrks
  • 5,439
  • 11
  • 52
  • 74