Commit e14180e5 by ibaker Committed by Oliver Woodman

Add SpannedData.isEmpty() method and simplify some logic in SampleQueue

PiperOrigin-RevId: 362527505
parent c8c51452
...@@ -725,15 +725,12 @@ public class SampleQueue implements TrackOutput { ...@@ -725,15 +725,12 @@ public class SampleQueue implements TrackOutput {
return false; return false;
} }
@Nullable SharedSampleMetadata upstreamCommittedMetadata = sharedSampleMetadata.getEndValue(); if (!sharedSampleMetadata.isEmpty()
@Nullable && sharedSampleMetadata.getEndValue().format.equals(format)) {
Format upstreamCommittedFormat =
upstreamCommittedMetadata != null ? upstreamCommittedMetadata.format : null;
if (Util.areEqual(format, upstreamCommittedFormat)) {
// The format has changed back to the format of the last committed sample. If they are // The format has changed back to the format of the last committed sample. If they are
// different objects, we revert back to using upstreamCommittedFormat as the upstreamFormat // different objects, we revert back to using upstreamCommittedFormat as the upstreamFormat
// so we can detect format changes on the read side using cheap referential equality. // so we can detect format changes on the read side using cheap referential equality.
upstreamFormat = upstreamCommittedFormat; upstreamFormat = sharedSampleMetadata.getEndValue().format;
} else { } else {
upstreamFormat = format; upstreamFormat = format;
} }
...@@ -804,9 +801,8 @@ public class SampleQueue implements TrackOutput { ...@@ -804,9 +801,8 @@ public class SampleQueue implements TrackOutput {
cryptoDatas[relativeEndIndex] = cryptoData; cryptoDatas[relativeEndIndex] = cryptoData;
sourceIds[relativeEndIndex] = upstreamSourceId; sourceIds[relativeEndIndex] = upstreamSourceId;
@Nullable SharedSampleMetadata upstreamCommittedMetadata = sharedSampleMetadata.getEndValue(); if (sharedSampleMetadata.isEmpty()
if (upstreamCommittedMetadata == null || !sharedSampleMetadata.getEndValue().format.equals(upstreamFormat)) {
|| !upstreamCommittedMetadata.format.equals(upstreamFormat)) {
DrmSessionReference drmSessionReference = DrmSessionReference drmSessionReference =
drmSessionManager != null drmSessionManager != null
? drmSessionManager.preacquireSession( ? drmSessionManager.preacquireSession(
......
...@@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkState; ...@@ -20,7 +20,6 @@ import static com.google.android.exoplayer2.util.Assertions.checkState;
import static java.lang.Math.min; import static java.lang.Math.min;
import android.util.SparseArray; import android.util.SparseArray;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Consumer; import com.google.android.exoplayer2.util.Consumer;
...@@ -60,8 +59,7 @@ import com.google.android.exoplayer2.util.Consumer; ...@@ -60,8 +59,7 @@ import com.google.android.exoplayer2.util.Consumer;
/** /**
* Returns the value associated with the span covering {@code key}. * Returns the value associated with the span covering {@code key}.
* *
* <p>{@link #appendSpan(int, Object)} must have been called at least once since the last call to * <p>The collection must not be {@link #isEmpty() empty}.
* {@link #clear()}.
* *
* <p>{@code key} must be greater than or equal to the previous value passed to {@link * <p>{@code key} must be greater than or equal to the previous value passed to {@link
* #discardTo(int)} (or zero after {@link #clear()} has been called). * #discardTo(int)} (or zero after {@link #clear()} has been called).
...@@ -103,14 +101,15 @@ import com.google.android.exoplayer2.util.Consumer; ...@@ -103,14 +101,15 @@ import com.google.android.exoplayer2.util.Consumer;
} }
/** /**
* Returns the value associated with the end span, or null if the collection is empty. * Returns the value associated with the end span.
*
* <p>The collection must not be {@link #isEmpty() empty}.
* *
* <p>This is either the last value passed to {@link #appendSpan(int, Object)}, or the value of * <p>This is either the last value passed to {@link #appendSpan(int, Object)}, or the value of
* the span covering the index passed to {@link #discardFrom(int)}. * the span covering the index passed to {@link #discardFrom(int)}.
*/ */
@Nullable
public V getEndValue() { public V getEndValue() {
return spans.size() != 0 ? spans.valueAt(spans.size() - 1) : null; return spans.valueAt(spans.size() - 1);
} }
/** /**
...@@ -150,4 +149,9 @@ import com.google.android.exoplayer2.util.Consumer; ...@@ -150,4 +149,9 @@ import com.google.android.exoplayer2.util.Consumer;
memoizedReadIndex = C.INDEX_UNSET; memoizedReadIndex = C.INDEX_UNSET;
spans.clear(); spans.clear();
} }
/** Returns true if the collection is empty. */
public boolean isEmpty() {
return spans.size() == 0;
}
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -74,6 +75,25 @@ public final class SpannedDataTest { ...@@ -74,6 +75,25 @@ public final class SpannedDataTest {
} }
@Test @Test
public void getEndValue() {
SpannedData<String> spannedData = new SpannedData<>();
assertThrows(Exception.class, spannedData::getEndValue);
spannedData.appendSpan(/* startKey= */ 0, "test 1");
spannedData.appendSpan(/* startKey= */ 2, "test 2");
spannedData.appendSpan(/* startKey= */ 4, "test 3");
assertThat(spannedData.getEndValue()).isEqualTo("test 3");
spannedData.discardFrom(2);
assertThat(spannedData.getEndValue()).isEqualTo("test 2");
spannedData.clear();
assertThrows(Exception.class, spannedData::getEndValue);
}
@Test
public void discardTo() { public void discardTo() {
SpannedData<DrmSessionReference> spannedData = SpannedData<DrmSessionReference> spannedData =
new SpannedData<>(/* removeCallback= */ DrmSessionReference::release); new SpannedData<>(/* removeCallback= */ DrmSessionReference::release);
...@@ -173,4 +193,30 @@ public final class SpannedDataTest { ...@@ -173,4 +193,30 @@ public final class SpannedDataTest {
assertThat(spannedData.get(0)).isEqualTo(value3); assertThat(spannedData.get(0)).isEqualTo(value3);
assertThat(spannedData.get(1)).isEqualTo(value3); assertThat(spannedData.get(1)).isEqualTo(value3);
} }
@Test
public void isEmpty() {
SpannedData<String> spannedData = new SpannedData<>();
assertThat(spannedData.isEmpty()).isTrue();
spannedData.appendSpan(/* startKey= */ 0, "test 1");
spannedData.appendSpan(/* startKey= */ 2, "test 2");
assertThat(spannedData.isEmpty()).isFalse();
// Discarding from 0 still retains the 'first' span, so collection doesn't end up empty.
spannedData.discardFrom(0);
assertThat(spannedData.isEmpty()).isFalse();
spannedData.appendSpan(/* startKey= */ 2, "test 2");
// Discarding to 3 still retains the 'last' span, so collection doesn't end up empty.
spannedData.discardTo(3);
assertThat(spannedData.isEmpty()).isFalse();
spannedData.clear();
assertThat(spannedData.isEmpty()).isTrue();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment