mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +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 androidx.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters that apply to seeking.
|
* Parameters that apply to seeking.
|
||||||
|
|
@ -71,6 +72,41 @@ public final class SeekParameters {
|
||||||
this.toleranceAfterUs = toleranceAfterUs;
|
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
|
@Override
|
||||||
public boolean equals(@Nullable Object obj) {
|
public boolean equals(@Nullable Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
|
|
|
||||||
|
|
@ -446,8 +446,8 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
SeekPoints seekPoints = seekMap.getSeekPoints(positionUs);
|
SeekPoints seekPoints = seekMap.getSeekPoints(positionUs);
|
||||||
return Util.resolveSeekPositionUs(
|
return seekParameters.resolveSeekPositionUs(
|
||||||
positionUs, seekParameters, seekPoints.first.timeUs, seekPoints.second.timeUs);
|
positionUs, seekPoints.first.timeUs, seekPoints.second.timeUs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SampleStream methods.
|
// SampleStream methods.
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,6 @@ import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
||||||
import com.google.android.exoplayer2.Format;
|
import com.google.android.exoplayer2.Format;
|
||||||
import com.google.android.exoplayer2.ParserException;
|
import com.google.android.exoplayer2.ParserException;
|
||||||
import com.google.android.exoplayer2.SeekParameters;
|
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
|
|
@ -1140,44 +1139,6 @@ public final class Util {
|
||||||
return Math.round((double) mediaDuration / speed);
|
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.
|
* Converts a list of integers to a primitive array.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ public class DefaultDashChunkSource implements DashChunkSource {
|
||||||
firstSyncUs < positionUs && segmentNum < representationHolder.getSegmentCount() - 1
|
firstSyncUs < positionUs && segmentNum < representationHolder.getSegmentCount() - 1
|
||||||
? representationHolder.getSegmentStartTimeUs(segmentNum + 1)
|
? representationHolder.getSegmentStartTimeUs(segmentNum + 1)
|
||||||
: firstSyncUs;
|
: 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.
|
// 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.DataSpec;
|
||||||
import com.google.android.exoplayer2.upstream.LoaderErrorThrower;
|
import com.google.android.exoplayer2.upstream.LoaderErrorThrower;
|
||||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -129,7 +128,7 @@ public class DefaultSsChunkSource implements SsChunkSource {
|
||||||
firstSyncUs < positionUs && chunkIndex < streamElement.chunkCount - 1
|
firstSyncUs < positionUs && chunkIndex < streamElement.chunkCount - 1
|
||||||
? streamElement.getStartTimeUs(chunkIndex + 1)
|
? streamElement.getStartTimeUs(chunkIndex + 1)
|
||||||
: firstSyncUs;
|
: firstSyncUs;
|
||||||
return Util.resolveSeekPositionUs(positionUs, seekParameters, firstSyncUs, secondSyncUs);
|
return seekParameters.resolveSeekPositionUs(positionUs, firstSyncUs, secondSyncUs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.DataSpec;
|
||||||
import com.google.android.exoplayer2.upstream.TransferListener;
|
import com.google.android.exoplayer2.upstream.TransferListener;
|
||||||
import com.google.android.exoplayer2.util.MimeTypes;
|
import com.google.android.exoplayer2.util.MimeTypes;
|
||||||
import com.google.android.exoplayer2.util.Util;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -90,7 +89,7 @@ public final class FakeChunkSource implements ChunkSource {
|
||||||
firstSyncUs < positionUs && chunkIndex < dataSet.getChunkCount() - 1
|
firstSyncUs < positionUs && chunkIndex < dataSet.getChunkCount() - 1
|
||||||
? dataSet.getStartTime(chunkIndex + 1)
|
? dataSet.getStartTime(chunkIndex + 1)
|
||||||
: firstSyncUs;
|
: firstSyncUs;
|
||||||
return Util.resolveSeekPositionUs(positionUs, seekParameters, firstSyncUs, secondSyncUs);
|
return seekParameters.resolveSeekPositionUs(positionUs, firstSyncUs, secondSyncUs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue