diff --git a/src/web_app/js/sdputils.js b/src/web_app/js/sdputils.js index 1f03559b5..12c10962c 100644 --- a/src/web_app/js/sdputils.js +++ b/src/web_app/js/sdputils.js @@ -330,9 +330,23 @@ function maybePreferCodec(sdp, type, dir, codec) { } // If the codec is available, set it as the default in m line. - var payload = getCodecPayloadType(sdpLines, codec); - if (payload) { - sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], payload); + var payload = null; + // Iterate through rtpmap enumerations to find all matching codec entries + for (var i = sdpLines.length-1; i >= 0 ; --i) { + // Finds first match in rtpmap + var index = findLineInRange(sdpLines, i, 0, 'a=rtpmap', codec, 'desc'); + if (index !== null) { + // Skip all of the entries between i and index match + i = index; + payload = getCodecPayloadTypeFromLine(sdpLines[index]); + if (payload) { + // Move codec to top + sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], payload); + } + } else { + // No match means we can break the loop + break; + } } sdp = sdpLines.join('\r\n'); @@ -450,13 +464,40 @@ function findLine(sdpLines, prefix, substr) { // Find the line in sdpLines[startLine...endLine - 1] that starts with |prefix| // and, if specified, contains |substr| (case-insensitive search). -function findLineInRange(sdpLines, startLine, endLine, prefix, substr) { - var realEndLine = endLine !== -1 ? endLine : sdpLines.length; - for (var i = startLine; i < realEndLine; ++i) { - if (sdpLines[i].indexOf(prefix) === 0) { - if (!substr || - sdpLines[i].toLowerCase().indexOf(substr.toLowerCase()) !== -1) { - return i; +function findLineInRange( + sdpLines, + startLine, + endLine, + prefix, + substr, + direction +) { + if (direction === undefined) { + direction = 'asc'; + } + + direction = direction || 'asc'; + + if (direction === 'asc') { + // Search beginning to end + var realEndLine = endLine !== -1 ? endLine : sdpLines.length; + for (var i = startLine; i < realEndLine; ++i) { + if (sdpLines[i].indexOf(prefix) === 0) { + if (!substr || + sdpLines[i].toLowerCase().indexOf(substr.toLowerCase()) !== -1) { + return i; + } + } + } + } else { + // Search end to beginning + var realStartLine = startLine !== -1 ? startLine : sdpLines.length-1; + for (var j = realStartLine; j >= 0; --j) { + if (sdpLines[j].indexOf(prefix) === 0) { + if (!substr || + sdpLines[j].toLowerCase().indexOf(substr.toLowerCase()) !== -1) { + return j; + } } } }