【问题标题】:Split SDP (string) for m line to change video codecs为 m 行拆分 SDP(字符串)以更改视频编解码器
【发布时间】:2017-08-09 00:31:28
【问题描述】:

我希望最后有一个方法可以将 VP9 或 H.264 设置为 SDP 中的首选视频编解码器。

所以我在我的 SDP 中寻找 m 行:

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

我的 SDP 的控制台日志:

在这种情况下,我将获取并使用 VP8 (96) 作为视频编解码器,而不是 VP9 (98)。所以我想检查 98/VP9 是否可能/可用,并希望将其设置在开始/第一个位置以实际使用它。

到目前为止我得到了什么:

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?

        }
    })
}

我认为这种方法很糟糕,因为我得到了我想要的 m 行,但我在位置 '28' 处做了一个修复子字符串,所以如果之前的某些内容发生变化,它会中断。

最后,我的 SDP 中应该有以下“m 行”:

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

有人可以帮我吗?

【问题讨论】:

  • 你能告诉我规则是什么吗?如果一行以m=video 开头并包含用空格括起来的98
  • 该行必须以“m=video”开头 - sdp 中只有这样的行。所以我想检查我的字符串中是否有“98”。如果它可用,我想首先检查它是否在第一位,如果不是,我想这样做(交换位置)。

标签: javascript string replace pattern-matching sdp


【解决方案1】:

这个方法可以用来自己修改SDP。您可以修改 SDP 以强制使用 h264、vp9 或 vp8 编解码器。

<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');

【讨论】:

    【解决方案2】:

    是这样的吗:

    // 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(' ');
    }
    

    【讨论】:

      【解决方案3】:

      您应该先用空格分隔行,然后根据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(' ');
      }
      

      【讨论】:

      • 这就是我的 sdp 变量的样子:abload.de/img/screenshot2017-03-17akhs0y.png 所以当我使用你的代码时,我 formats.includes 找不到“98”所以没有日志消息。
      • 啊,你已经完全改变了问题:(你的问题不清楚如果 98 根本没有出现在列表中,你想要发生什么。
      • 其实并非如此。我现在的问题是,当我在 m 行中找到“98”时如何更改字符串的顺序(交换位置)。
      • 更新了我的问题,因为你的回答对我不起作用@Alnitak
      • @Oskar nope - 在上面的代码中,formats 是一个数组,而不是字符串 - 请注意 ... 前缀,它是一个 ES6 解构赋值,将 type 分配给第零个元素 @ 987654327@ 到 #1,proto 到 #2,formats 到数组的其余部分。
      猜你喜欢
      • 2019-03-15
      • 1970-01-01
      • 2016-06-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2016-03-15
      相关资源
      最近更新 更多