1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67

content / test / data / media / webrtc_codec_utils.js [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Webrtc codec test utilities.

// Promotes H264 to be the first in the m=video line, if available.
function maybePreferH264SendCodec(sdp) {
  var sdpLines = sdp.split('\r\n');

  var mLineIndex = findLine(sdpLines, 'm=', 'video');
  if (mLineIndex === null) {
    throw new Error('No |m=video| line found in the sdp.');
  }

  var payload = getCodecPayloadType(sdpLines, 'h264');
  if (payload === null) {
    console.log("H264 is not available on this target.");
    return sdp;
  }

  // If h264 is available, set it as the default in m line.
  sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], payload);
  sdp = sdpLines.join('\r\n');
  return sdp;
}

// Find the line in sdpLines that starts with |prefix|, and, if specified,
// contains |substr| (case-insensitive search).
function findLine(sdpLines, prefix, substr) {
  for (var i = 0; i < sdpLines.length; ++i) {
    if (sdpLines[i].indexOf(prefix) === 0) {
      if (!substr ||
          sdpLines[i].toLowerCase().indexOf(substr.toLowerCase()) !== -1) {
        return i;
      }
    }
  }
  return null;
}

// Gets the codec payload type from sdp lines.
function getCodecPayloadType(sdpLines, codec) {
  var index = findLine(sdpLines, 'a=rtpmap', codec);
  if (index === null)
    return null;

  var pattern = new RegExp('a=rtpmap:(\\d+) [a-zA-Z0-9-]+\\/\\d+');
  var result = sdpLines[index].match(pattern);

  return (result && result.length === 2) ? result[1] : null;
}

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