Fix E-AC3 format propagation

Issue: Issue: #7611
PiperOrigin-RevId: 324884412
This commit is contained in:
olly 2020-08-04 21:58:34 +01:00 committed by kim-vde
parent 8da7fc7d2f
commit c0ee267a6c
3 changed files with 64 additions and 22 deletions

View File

@ -631,6 +631,13 @@ public abstract class MediaCodecRenderer extends BaseRenderer {
throws ExoPlaybackException {
boolean outputFormatChanged = false;
@Nullable Format format = formatQueue.pollFloor(presentationTimeUs);
if (format == null && codecOutputMediaFormatChanged) {
// If the codec's output MediaFormat has changed then there should be a corresponding Format
// change, which we've not found. Check the Format queue in case the corresponding
// presentation timestamp is greater than presentationTimeUs, which can happen for some codecs
// [Internal ref: b/162719047].
format = formatQueue.pollFirst();
}
if (format != null) {
outputFormat = format;
outputFormatChanged = true;

View File

@ -62,6 +62,12 @@ public final class TimedValueQueue<V> {
return size;
}
/** Removes and returns the first value in the queue, or null if the queue is empty. */
@Nullable
public synchronized V pollFirst() {
return size == 0 ? null : popFirst();
}
/**
* Returns the value with the greatest timestamp which is less than or equal to the given
* timestamp. Removes all older values and the returned one from the buffer.
@ -71,7 +77,8 @@ public final class TimedValueQueue<V> {
* timestamp or null if there is no such value.
* @see #poll(long)
*/
public synchronized @Nullable V pollFloor(long timestamp) {
@Nullable
public synchronized V pollFloor(long timestamp) {
return poll(timestamp, /* onlyOlder= */ true);
}
@ -83,7 +90,8 @@ public final class TimedValueQueue<V> {
* @return The value with the closest timestamp or null if the buffer is empty.
* @see #pollFloor(long)
*/
public synchronized @Nullable V poll(long timestamp) {
@Nullable
public synchronized V poll(long timestamp) {
return poll(timestamp, /* onlyOlder= */ false);
}
@ -99,7 +107,7 @@ public final class TimedValueQueue<V> {
*/
@Nullable
private V poll(long timestamp, boolean onlyOlder) {
V value = null;
@Nullable V value = null;
long previousTimeDiff = Long.MAX_VALUE;
while (size > 0) {
long timeDiff = timestamp - timestamps[first];
@ -107,14 +115,21 @@ public final class TimedValueQueue<V> {
break;
}
previousTimeDiff = timeDiff;
value = values[first];
values[first] = null;
first = (first + 1) % values.length;
size--;
value = popFirst();
}
return value;
}
@Nullable
private V popFirst() {
Assertions.checkState(size > 0);
@Nullable V value = values[first];
values[first] = null;
first = (first + 1) % values.length;
size--;
return value;
}
private void clearBufferOnTimeDiscontinuity(long timestamp) {
if (size > 0) {
int last = (first + size - 1) % values.length;
@ -131,7 +146,7 @@ public final class TimedValueQueue<V> {
}
int newCapacity = capacity * 2;
long[] newTimestamps = new long[newCapacity];
V[] newValues = newArray(newCapacity);
@NullableType V[] newValues = newArray(newCapacity);
// Reset the loop starting index to 0 while coping to the new buffer.
// First copy the values from 'first' index to the end of original array.
int length = capacity - first;
@ -155,7 +170,7 @@ public final class TimedValueQueue<V> {
}
@SuppressWarnings("unchecked")
private static <V> V[] newArray(int length) {
private static <V> @NullableType V[] newArray(int length) {
return (V[]) new Object[length];
}
}

View File

@ -33,23 +33,13 @@ public class TimedValueQueueTest {
queue = new TimedValueQueue<>();
}
@Test
public void addAndPollValues() {
queue.add(0, "a");
queue.add(1, "b");
queue.add(2, "c");
assertThat(queue.poll(0)).isEqualTo("a");
assertThat(queue.poll(1)).isEqualTo("b");
assertThat(queue.poll(2)).isEqualTo("c");
}
@Test
public void bufferCapacityIncreasesAutomatically() {
queue = new TimedValueQueue<>(1);
for (int i = 0; i < 20; i++) {
queue.add(i, "" + i);
if ((i & 1) == 1) {
assertThat(queue.poll(0)).isEqualTo("" + (i / 2));
assertThat(queue.pollFirst()).isEqualTo("" + (i / 2));
}
}
assertThat(queue.size()).isEqualTo(10);
@ -61,7 +51,7 @@ public class TimedValueQueueTest {
queue.add(2, "c");
queue.add(0, "a");
assertThat(queue.size()).isEqualTo(1);
assertThat(queue.poll(0)).isEqualTo("a");
assertThat(queue.pollFirst()).isEqualTo("a");
}
@Test
@ -71,7 +61,37 @@ public class TimedValueQueueTest {
queue.add(3, "c");
queue.add(2, "a");
assertThat(queue.size()).isEqualTo(1);
assertThat(queue.poll(2)).isEqualTo("a");
assertThat(queue.pollFirst()).isEqualTo("a");
}
@Test
public void pollFirstReturnsValues() {
queue.add(0, "a");
queue.add(1, "b");
queue.add(2, "c");
assertThat(queue.pollFirst()).isEqualTo("a");
assertThat(queue.size()).isEqualTo(2);
assertThat(queue.pollFirst()).isEqualTo("b");
assertThat(queue.size()).isEqualTo(1);
assertThat(queue.pollFirst()).isEqualTo("c");
assertThat(queue.size()).isEqualTo(0);
assertThat(queue.pollFirst()).isEqualTo(null);
assertThat(queue.size()).isEqualTo(0);
}
@Test
public void pollReturnsValues() {
queue.add(0, "a");
queue.add(1, "b");
queue.add(2, "c");
assertThat(queue.poll(0)).isEqualTo("a");
assertThat(queue.size()).isEqualTo(2);
assertThat(queue.poll(1)).isEqualTo("b");
assertThat(queue.size()).isEqualTo(1);
assertThat(queue.poll(2)).isEqualTo("c");
assertThat(queue.size()).isEqualTo(0);
assertThat(queue.pollFirst()).isEqualTo(null);
assertThat(queue.size()).isEqualTo(0);
}
@Test