Skip to content

Commit

Permalink
Fix handling of widths/heights when choosing formats.
Browse files Browse the repository at this point in the history
- Make HlsPlaylistParser treat non-positive dimensions as unknown.
- Make HlsPlaylistParser parse floating point resolutions, because
  technically that's how they're spec'd.
- Make VideoFormatSelectorUtil treat non-position dimensions as unknown.

Issue: google#461
  • Loading branch information
ojw28 committed May 19, 2015
1 parent 059b80c commit f474afb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static int[] selectVideoFormats(List<? extends FormatWrapper> formatWrapp
// Keep track of the number of pixels of the selected format whose resolution is the
// smallest to exceed the maximum size at which it can be displayed within the viewport.
// We'll discard formats of higher resolution in a second pass.
if (format.width != -1 && format.height != -1) {
if (format.width > 0 && format.height > 0) {
Point maxVideoSizeInViewport = getMaxVideoSizeInViewport(orientationMayChange,
viewportWidth, viewportHeight, format.width, format.height);
int videoPixels = format.width * format.height;
Expand All @@ -126,7 +126,7 @@ public static int[] selectVideoFormats(List<? extends FormatWrapper> formatWrapp
// viewport.
for (int i = selectedIndexList.size() - 1; i >= 0; i--) {
Format format = formatWrappers.get(i).getFormat();
if (format.width != -1 && format.height != -1
if (format.width > 0 && format.height > 0
&& format.width * format.height > maxVideoPixelsToRetain) {
selectedIndexList.remove(i);
}
Expand All @@ -150,7 +150,7 @@ private static boolean isFormatPlayable(Format format, String[] allowedContainer
// Filtering format because it's HD.
return false;
}
if (format.width != -1 && format.height != -1) {
if (format.width > 0 && format.height > 0) {
// TODO: Use MediaCodecUtil.isSizeAndRateSupportedV21 on API levels >= 21 if we know the
// mimeType of the media samples within the container. Remove the assumption that we're
// dealing with H.264.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public static double parseDoubleAttr(String line, Pattern pattern, String tag)

public static String parseOptionalStringAttr(String line, Pattern pattern) {
Matcher matcher = pattern.matcher(line);
if (matcher.find() && matcher.groupCount() == 1) {
if (matcher.find()) {
return matcher.group(1);
}
return null;
}

public static boolean parseOptionalBooleanAttr(String line, Pattern pattern) {
Matcher matcher = pattern.matcher(line);
if (matcher.find() && matcher.groupCount() == 1) {
if (matcher.find()) {
return BOOLEAN_YES.equals(matcher.group(1));
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
private static final Pattern CODECS_ATTR_REGEX =
Pattern.compile(CODECS_ATTR + "=\"(.+?)\"");
private static final Pattern RESOLUTION_ATTR_REGEX =
Pattern.compile(RESOLUTION_ATTR + "=(\\d+x\\d+)");
Pattern.compile(RESOLUTION_ATTR + "=(\\d+(\\.\\d+)?x\\d+(\\.\\d+)?)");
private static final Pattern MEDIA_DURATION_REGEX =
Pattern.compile(MEDIA_DURATION_TAG + ":([\\d.]+),");
private static final Pattern MEDIA_SEQUENCE_REGEX =
Expand Down Expand Up @@ -168,8 +168,16 @@ private static HlsMasterPlaylist parseMasterPlaylist(LineIterator iterator, Stri
RESOLUTION_ATTR_REGEX);
if (resolutionString != null) {
String[] widthAndHeight = resolutionString.split("x");
width = Integer.parseInt(widthAndHeight[0]);
height = Integer.parseInt(widthAndHeight[1]);
width = Math.round(Float.parseFloat(widthAndHeight[0]));
if (width <= 0) {
// Width was invalid.
width = -1;
}
height = Math.round(Float.parseFloat(widthAndHeight[1]));
if (height <= 0) {
// Height was invalid.
height = -1;
}
} else {
width = -1;
height = -1;
Expand Down

0 comments on commit f474afb

Please sign in to comment.