improve audio track selection

This commit is contained in:
Yuriy Liskov
2020-09-28 00:42:52 +03:00
parent 310337a7c4
commit 9cf49ff7fc
3 changed files with 107 additions and 68 deletions
@@ -0,0 +1,101 @@
package com.liskovsoft.smartyoutubetv2.common.exoplayer.selector;
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.smartyoutubetv2.common.exoplayer.selector.TrackSelectorManager.MediaTrack;
class TrackComparator {
private static final int HEIGHT_EQUITY_THRESHOLD_PX = 80;
private int mRendererIndex;
private static boolean codecEquals(String codecs1, String codecs2) {
if (codecs1 == null || codecs2 == null) {
return false;
}
return Helpers.equals(TrackSelectorUtil.codecNameShort(codecs1), TrackSelectorUtil.codecNameShort(codecs2));
}
private static boolean heightEquals(int height1, int height2) {
if (height1 == -1 || height2 == -1) {
return false;
}
return Math.abs(height1 - height2) < HEIGHT_EQUITY_THRESHOLD_PX;
}
private static boolean heightLessOrEquals(int height1, int height2) {
if (height1 == -1 || height2 == -1) {
return false;
}
return height1 <= height2 || heightEquals(height1, height2);
}
private static boolean fpsEquals(float fps1, float fps2) {
if (fps1 == -1 || fps2 == -1) {
return true;
}
return Math.abs(fps1 - fps2) < 10;
}
private static boolean fpsLessOrEquals(float fps1, float fps2) {
if (fps1 == -1 || fps2 == -1) {
return true; // probably live translation
}
return fps1 <= fps2 || fpsEquals(fps1, fps2);
}
public int compare(MediaTrack track1, MediaTrack track2) {
if (mRendererIndex == TrackSelectorManager.RENDERER_INDEX_VIDEO) {
return compareVideo(track1, track2);
} else if (mRendererIndex == TrackSelectorManager.RENDERER_INDEX_AUDIO) {
return compareAudio(track1, track2);
}
return 0;
}
private int compareAudio(MediaTrack track1, MediaTrack track2) {
if (track1 == null || track1.format == null) {
return -1;
}
int result = 1;
if (Helpers.equals(track1.format.id, track2.format.id)) {
result = 0;
} else if (TrackComparator.codecEquals(track1.format.codecs, track2.format.codecs)) {
return 0;
}
return result;
}
private static int compareVideo(MediaTrack track1, MediaTrack track2) {
if (track1 == null || track1.format == null) {
return -1;
}
int result = 1;
if (Helpers.equals(track1.format.id, track2.format.id)) {
result = 0;
} else if (TrackComparator.codecEquals(track1.format.codecs, track2.format.codecs)) {
if (TrackComparator.fpsLessOrEquals(track1.format.frameRate, track2.format.frameRate)) {
if (TrackComparator.heightEquals(track1.format.height, track2.format.height)) {
result = 0;
} else if (TrackComparator.heightLessOrEquals(track1.format.height, track2.format.height)) {
result = -1;
}
}
}
return result;
}
public void setRendererIndex(int rendererIndex) {
mRendererIndex = rendererIndex;
}
}
@@ -33,7 +33,7 @@ public class TrackSelectorManager implements TrackSelectorCallback {
private final Renderer[] mRenderers = new Renderer[3];
private final MediaTrack[] mSelectedTracks = new MediaTrack[3];
//private MediaTrack mPendingSelection;
private final TrackComparator mComparator = new TrackComparator();
public void invalidate() {
Arrays.fill(mRenderers, null);
@@ -400,17 +400,19 @@ public class TrackSelectorManager implements TrackSelectorCallback {
MediaTrack result = createAutoSelection(track.rendererIndex);
mComparator.setRendererIndex(track.rendererIndex);
if (track.format != null) { // not auto selection
for (int groupIndex = 0; groupIndex < renderer.mediaTracks.length; groupIndex++) {
for (int trackIndex = 0; trackIndex < renderer.mediaTracks[groupIndex].length; trackIndex++) {
MediaTrack mediaTrack = renderer.mediaTracks[groupIndex][trackIndex];
int compare = TrackSelectorUtil.compare(mediaTrack, track);
int compare = mComparator.compare(mediaTrack, track);
if (compare == 0) {
result = mediaTrack;
break;
} else if (compare < 0 && TrackSelectorUtil.compare(result, mediaTrack) < 0) {
} else if (compare < 0 && mComparator.compare(result, mediaTrack) < 0) {
result = mediaTrack;
}
}
@@ -4,7 +4,6 @@ import android.text.TextUtils;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.util.MimeTypes;
import com.liskovsoft.sharedutils.helpers.Helpers;
import com.liskovsoft.smartyoutubetv2.common.exoplayer.selector.TrackSelectorManager.MediaTrack;
public class TrackSelectorUtil {
public static final String CODEC_SHORT_AVC = "avc";
@@ -13,7 +12,6 @@ public class TrackSelectorUtil {
public static final String CODEC_SHORT_MP4A = "mp4a";
public static final String CODEC_SHORT_VORBIS = "vorbis";
private static final String SEPARATOR = ", ";
private static final int HEIGHT_EQUITY_THRESHOLD_PX = 80;
/**
* Builds a track name for display.
@@ -81,7 +79,7 @@ public class TrackSelectorUtil {
return false;
}
return codec.equals("vp9.2");
return codec.equals(CODEC_SHORT_VP9_HDR);
}
public static String extractCodec(Format format) {
@@ -113,66 +111,4 @@ public class TrackSelectorUtil {
private static String buildChannels(Format format) {
return format.bitrate > 300000 ? "5.1" : "";
}
private static boolean codecEquals(String codecs1, String codecs2) {
if (codecs1 == null || codecs2 == null) {
return false;
}
return Helpers.equals(codecNameShort(codecs1), codecNameShort(codecs2));
}
private static boolean heightEquals(int height1, int height2) {
if (height1 == -1 || height2 == -1) {
return false;
}
return Math.abs(height1 - height2) < HEIGHT_EQUITY_THRESHOLD_PX;
}
private static boolean heightLessOrEquals(int height1, int height2) {
if (height1 == -1 || height2 == -1) {
return false;
}
return height1 <= height2 || heightEquals(height1, height2);
}
private static boolean fpsEquals(float fps1, float fps2) {
if (fps1 == -1 || fps2 == -1) {
return true;
}
return Math.abs(fps1 - fps2) < 10;
}
private static boolean fpsLessOrEquals(float fps1, float fps2) {
if (fps1 == -1 || fps2 == -1) {
return true;
}
return fps1 <= fps2 || fpsEquals(fps1, fps2);
}
public static int compare(MediaTrack track1, MediaTrack track2) {
if (track1 == null || track1.format == null) {
return -1;
}
int result = 1;
if (Helpers.equals(track1.format.id, track2.format.id)) {
result = 0;
} else if (TrackSelectorUtil.codecEquals(track1.format.codecs, track2.format.codecs)) {
if (TrackSelectorUtil.fpsLessOrEquals(track1.format.frameRate, track2.format.frameRate)) {
if (TrackSelectorUtil.heightEquals(track1.format.height, track2.format.height)) {
result = 0;
} else if (TrackSelectorUtil.heightLessOrEquals(track1.format.height, track2.format.height)) {
result = -1;
}
}
}
return result;
}
}