Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Fixing ordering, reducing redundant iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Gibson committed Jan 31, 2018
1 parent f161c71 commit 616cdac
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/web_app/js/sdputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,21 @@ function maybePreferCodec(sdp, type, dir, codec) {

// If the codec is available, set it as the default in m line.
var payload = null;
for (var i = 0; i < sdpLines.length; i++) {
var index = findLineInRange(sdpLines, i, -1, 'a=rtpmap', codec);
// 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;
}
}

Expand Down Expand Up @@ -456,13 +464,39 @@ 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;
}
}
}
}
Expand Down

0 comments on commit 616cdac

Please sign in to comment.