mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Add getLastTimestampUs()
method
PiperOrigin-RevId: 613579674
This commit is contained in:
parent
914874566e
commit
0f72126c20
@ -34,8 +34,9 @@ public final class ConstantRateTimestampIterator implements TimestampIterator {
|
|||||||
private final float frameRate;
|
private final float frameRate;
|
||||||
private final double framesDurationUs;
|
private final double framesDurationUs;
|
||||||
private final long startingTimestampUs;
|
private final long startingTimestampUs;
|
||||||
private double currentTimestampUs;
|
private final int totalNumberOfFramesToAdd;
|
||||||
private int framesToAdd;
|
|
||||||
|
private int framesAdded;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance that outputs timestamps from {@code 0}.
|
* Creates an instance that outputs timestamps from {@code 0}.
|
||||||
@ -66,27 +67,39 @@ public final class ConstantRateTimestampIterator implements TimestampIterator {
|
|||||||
this.durationUs = durationUs;
|
this.durationUs = durationUs;
|
||||||
this.frameRate = frameRate;
|
this.frameRate = frameRate;
|
||||||
this.startingTimestampUs = startingTimestampUs;
|
this.startingTimestampUs = startingTimestampUs;
|
||||||
this.currentTimestampUs = startingTimestampUs;
|
this.totalNumberOfFramesToAdd = round(frameRate * (durationUs / (float) C.MICROS_PER_SECOND));
|
||||||
framesToAdd = round(frameRate * (durationUs / (float) C.MICROS_PER_SECOND));
|
|
||||||
framesDurationUs = C.MICROS_PER_SECOND / frameRate;
|
framesDurationUs = C.MICROS_PER_SECOND / frameRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return framesToAdd != 0;
|
return framesAdded < totalNumberOfFramesToAdd;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long next() {
|
public long next() {
|
||||||
checkState(hasNext());
|
checkState(hasNext());
|
||||||
framesToAdd--;
|
return getTimestampUsAfter(framesAdded++);
|
||||||
long next = round(currentTimestampUs);
|
|
||||||
currentTimestampUs += framesDurationUs;
|
|
||||||
return next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConstantRateTimestampIterator copyOf() {
|
public ConstantRateTimestampIterator copyOf() {
|
||||||
return new ConstantRateTimestampIterator(durationUs, frameRate, startingTimestampUs);
|
return new ConstantRateTimestampIterator(durationUs, frameRate, startingTimestampUs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLastTimestampUs() {
|
||||||
|
if (totalNumberOfFramesToAdd == 0) {
|
||||||
|
return C.TIME_UNSET;
|
||||||
|
}
|
||||||
|
return getTimestampUsAfter(totalNumberOfFramesToAdd - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the timestamp after {@code numberOfFrames}, in microseconds. */
|
||||||
|
private long getTimestampUsAfter(int numberOfFrames) {
|
||||||
|
long timestampUs = round(startingTimestampUs + framesDurationUs * numberOfFrames);
|
||||||
|
// Check for possible overflow.
|
||||||
|
checkState(timestampUs >= 0);
|
||||||
|
return timestampUs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package androidx.media3.common.util;
|
package androidx.media3.common.util;
|
||||||
|
|
||||||
|
import androidx.media3.common.C;
|
||||||
|
|
||||||
/** A primitive long iterator used for generating sequences of timestamps. */
|
/** A primitive long iterator used for generating sequences of timestamps. */
|
||||||
@UnstableApi
|
@UnstableApi
|
||||||
public interface TimestampIterator {
|
public interface TimestampIterator {
|
||||||
@ -27,4 +29,12 @@ public interface TimestampIterator {
|
|||||||
|
|
||||||
/** Returns fresh copy of the iterator. */
|
/** Returns fresh copy of the iterator. */
|
||||||
TimestampIterator copyOf();
|
TimestampIterator copyOf();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last (final) timestamp this iterator generates, in microseconds; returns {@link
|
||||||
|
* C#TIME_UNSET} if the last timestamp is unknown, or when no timestamp will be generated.
|
||||||
|
*/
|
||||||
|
default long getLastTimestampUs() {
|
||||||
|
return C.TIME_UNSET;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package androidx.media3.common.util;
|
package androidx.media3.common.util;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static java.lang.Math.round;
|
||||||
|
|
||||||
import androidx.media3.common.C;
|
import androidx.media3.common.C;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
@ -35,6 +36,8 @@ public class ConstantRateTimestampIteratorTest {
|
|||||||
|
|
||||||
assertThat(generateList(constantRateTimestampIterator))
|
assertThat(generateList(constantRateTimestampIterator))
|
||||||
.containsExactly(0L, C.MICROS_PER_SECOND / 2);
|
.containsExactly(0L, C.MICROS_PER_SECOND / 2);
|
||||||
|
assertThat(constantRateTimestampIterator.getLastTimestampUs())
|
||||||
|
.isEqualTo(C.MICROS_PER_SECOND / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -44,6 +47,8 @@ public class ConstantRateTimestampIteratorTest {
|
|||||||
new ConstantRateTimestampIterator((long) (2.5 * C.MICROS_PER_SECOND), /* frameRate= */ 30);
|
new ConstantRateTimestampIterator((long) (2.5 * C.MICROS_PER_SECOND), /* frameRate= */ 30);
|
||||||
|
|
||||||
assertThat(generateList(constantRateTimestampIterator)).hasSize(75);
|
assertThat(generateList(constantRateTimestampIterator)).hasSize(75);
|
||||||
|
assertThat(constantRateTimestampIterator.getLastTimestampUs())
|
||||||
|
.isEqualTo(round((C.MICROS_PER_SECOND / 30.d) * 74));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -69,6 +74,7 @@ public class ConstantRateTimestampIteratorTest {
|
|||||||
new ConstantRateTimestampIterator(/* durationUs= */ 1, /* frameRate= */ 2);
|
new ConstantRateTimestampIterator(/* durationUs= */ 1, /* frameRate= */ 2);
|
||||||
|
|
||||||
assertThat(generateList(constantRateTimestampIterator)).isEmpty();
|
assertThat(generateList(constantRateTimestampIterator)).isEmpty();
|
||||||
|
assertThat(constantRateTimestampIterator.getLastTimestampUs()).isEqualTo(C.TIME_UNSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -80,6 +86,8 @@ public class ConstantRateTimestampIteratorTest {
|
|||||||
/* startingTimestampUs= */ 1234);
|
/* startingTimestampUs= */ 1234);
|
||||||
|
|
||||||
assertThat(constantRateTimestampIterator.next()).isEqualTo(1234);
|
assertThat(constantRateTimestampIterator.next()).isEqualTo(1234);
|
||||||
|
assertThat(constantRateTimestampIterator.getLastTimestampUs())
|
||||||
|
.isEqualTo(1234 + C.MICROS_PER_SECOND / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user