mirror of
https://github.com/samsonjs/media.git
synced 2026-04-03 10:55:48 +00:00
Make resolveSeekPositionUs an instance method
PiperOrigin-RevId: 291125686
This commit is contained in:
parent
a0f6bc877b
commit
6b03d4bc40
6 changed files with 41 additions and 46 deletions
|
|
@ -17,6 +17,7 @@ package com.google.android.exoplayer2;
|
|||
|
||||
import androidx.annotation.Nullable;
|
||||
import com.google.android.exoplayer2.util.Assertions;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
|
||||
/**
|
||||
* Parameters that apply to seeking.
|
||||
|
|
@ -71,6 +72,41 @@ public final class SeekParameters {
|
|||
this.toleranceAfterUs = toleranceAfterUs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a seek based on the parameters, given the requested seek position and two candidate
|
||||
* sync points.
|
||||
*
|
||||
* @param positionUs The requested seek position, in microseocnds.
|
||||
* @param firstSyncUs The first candidate seek point, in micrseconds.
|
||||
* @param secondSyncUs The second candidate seek point, in microseconds. May equal {@code
|
||||
* firstSyncUs} if there's only one candidate.
|
||||
* @return The resolved seek position, in microseconds.
|
||||
*/
|
||||
public long resolveSeekPositionUs(long positionUs, long firstSyncUs, long secondSyncUs) {
|
||||
if (toleranceBeforeUs == 0 && toleranceAfterUs == 0) {
|
||||
return positionUs;
|
||||
}
|
||||
long minPositionUs =
|
||||
Util.subtractWithOverflowDefault(positionUs, toleranceBeforeUs, Long.MIN_VALUE);
|
||||
long maxPositionUs = Util.addWithOverflowDefault(positionUs, toleranceAfterUs, Long.MAX_VALUE);
|
||||
boolean firstSyncPositionValid = minPositionUs <= firstSyncUs && firstSyncUs <= maxPositionUs;
|
||||
boolean secondSyncPositionValid =
|
||||
minPositionUs <= secondSyncUs && secondSyncUs <= maxPositionUs;
|
||||
if (firstSyncPositionValid && secondSyncPositionValid) {
|
||||
if (Math.abs(firstSyncUs - positionUs) <= Math.abs(secondSyncUs - positionUs)) {
|
||||
return firstSyncUs;
|
||||
} else {
|
||||
return secondSyncUs;
|
||||
}
|
||||
} else if (firstSyncPositionValid) {
|
||||
return firstSyncUs;
|
||||
} else if (secondSyncPositionValid) {
|
||||
return secondSyncUs;
|
||||
} else {
|
||||
return minPositionUs;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable Object obj) {
|
||||
if (this == obj) {
|
||||
|
|
|
|||
|
|
@ -446,8 +446,8 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
|
|||
return 0;
|
||||
}
|
||||
SeekPoints seekPoints = seekMap.getSeekPoints(positionUs);
|
||||
return Util.resolveSeekPositionUs(
|
||||
positionUs, seekParameters, seekPoints.first.timeUs, seekPoints.second.timeUs);
|
||||
return seekParameters.resolveSeekPositionUs(
|
||||
positionUs, seekPoints.first.timeUs, seekPoints.second.timeUs);
|
||||
}
|
||||
|
||||
// SampleStream methods.
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ import com.google.android.exoplayer2.C;
|
|||
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
||||
import com.google.android.exoplayer2.Format;
|
||||
import com.google.android.exoplayer2.ParserException;
|
||||
import com.google.android.exoplayer2.SeekParameters;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.Closeable;
|
||||
|
|
@ -1140,44 +1139,6 @@ public final class Util {
|
|||
return Math.round((double) mediaDuration / speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a seek given the requested seek position, a {@link SeekParameters} and two candidate
|
||||
* sync points.
|
||||
*
|
||||
* @param positionUs The requested seek position, in microseocnds.
|
||||
* @param seekParameters The {@link SeekParameters}.
|
||||
* @param firstSyncUs The first candidate seek point, in micrseconds.
|
||||
* @param secondSyncUs The second candidate seek point, in microseconds. May equal {@code
|
||||
* firstSyncUs} if there's only one candidate.
|
||||
* @return The resolved seek position, in microseconds.
|
||||
*/
|
||||
public static long resolveSeekPositionUs(
|
||||
long positionUs, SeekParameters seekParameters, long firstSyncUs, long secondSyncUs) {
|
||||
if (SeekParameters.EXACT.equals(seekParameters)) {
|
||||
return positionUs;
|
||||
}
|
||||
long minPositionUs =
|
||||
subtractWithOverflowDefault(positionUs, seekParameters.toleranceBeforeUs, Long.MIN_VALUE);
|
||||
long maxPositionUs =
|
||||
addWithOverflowDefault(positionUs, seekParameters.toleranceAfterUs, Long.MAX_VALUE);
|
||||
boolean firstSyncPositionValid = minPositionUs <= firstSyncUs && firstSyncUs <= maxPositionUs;
|
||||
boolean secondSyncPositionValid =
|
||||
minPositionUs <= secondSyncUs && secondSyncUs <= maxPositionUs;
|
||||
if (firstSyncPositionValid && secondSyncPositionValid) {
|
||||
if (Math.abs(firstSyncUs - positionUs) <= Math.abs(secondSyncUs - positionUs)) {
|
||||
return firstSyncUs;
|
||||
} else {
|
||||
return secondSyncUs;
|
||||
}
|
||||
} else if (firstSyncPositionValid) {
|
||||
return firstSyncUs;
|
||||
} else if (secondSyncPositionValid) {
|
||||
return secondSyncUs;
|
||||
} else {
|
||||
return minPositionUs;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a list of integers to a primitive array.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -198,7 +198,7 @@ public class DefaultDashChunkSource implements DashChunkSource {
|
|||
firstSyncUs < positionUs && segmentNum < representationHolder.getSegmentCount() - 1
|
||||
? representationHolder.getSegmentStartTimeUs(segmentNum + 1)
|
||||
: firstSyncUs;
|
||||
return Util.resolveSeekPositionUs(positionUs, seekParameters, firstSyncUs, secondSyncUs);
|
||||
return seekParameters.resolveSeekPositionUs(positionUs, firstSyncUs, secondSyncUs);
|
||||
}
|
||||
}
|
||||
// We don't have a segment index to adjust the seek position with yet.
|
||||
|
|
|
|||
|
|
@ -38,7 +38,6 @@ import com.google.android.exoplayer2.upstream.DataSource;
|
|||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.LoaderErrorThrower;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -129,7 +128,7 @@ public class DefaultSsChunkSource implements SsChunkSource {
|
|||
firstSyncUs < positionUs && chunkIndex < streamElement.chunkCount - 1
|
||||
? streamElement.getStartTimeUs(chunkIndex + 1)
|
||||
: firstSyncUs;
|
||||
return Util.resolveSeekPositionUs(positionUs, seekParameters, firstSyncUs, secondSyncUs);
|
||||
return seekParameters.resolveSeekPositionUs(positionUs, firstSyncUs, secondSyncUs);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -32,7 +32,6 @@ import com.google.android.exoplayer2.upstream.DataSource;
|
|||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||
import com.google.android.exoplayer2.util.MimeTypes;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -90,7 +89,7 @@ public final class FakeChunkSource implements ChunkSource {
|
|||
firstSyncUs < positionUs && chunkIndex < dataSet.getChunkCount() - 1
|
||||
? dataSet.getStartTime(chunkIndex + 1)
|
||||
: firstSyncUs;
|
||||
return Util.resolveSeekPositionUs(positionUs, seekParameters, firstSyncUs, secondSyncUs);
|
||||
return seekParameters.resolveSeekPositionUs(positionUs, firstSyncUs, secondSyncUs);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in a new issue