Move decode-only handling out of MetadataDecoder interface logic

The interface requires the implementation to return null if the
decode-only flag is set. So instead of setting the flag and returning
null, we can simply not call the method and assume it's null.

The only reason where this wouldn't work is if the metadata format
has keyframe-like logic and requires previous metadata to decode
the next one. This is not something we came across before and it seems
ignorable. If that feature is needed in the future, we should instead
add a method to MetadataDecoder to set the first output timestamp.

#minor-release

PiperOrigin-RevId: 570399838
This commit is contained in:
tonihei 2023-10-03 08:46:18 -07:00 committed by Copybara-Service
parent da06bf057a
commit 796781d4c3
5 changed files with 7 additions and 23 deletions

View File

@ -11,6 +11,8 @@
* Video: * Video:
* Text: * Text:
* Metadata: * Metadata:
* `MetadataDecoder.decode` will no longer be called for "decode-only"
samples as the implementation must return null anyway.
* DRM: * DRM:
* Effect: * Effect:
* Muxers: * Muxers:

View File

@ -238,11 +238,9 @@ public final class MetadataRenderer extends BaseRenderer implements Callback {
if (result == C.RESULT_BUFFER_READ) { if (result == C.RESULT_BUFFER_READ) {
if (buffer.isEndOfStream()) { if (buffer.isEndOfStream()) {
inputStreamEnded = true; inputStreamEnded = true;
} else { } else if (buffer.timeUs >= getLastResetPositionUs()) {
// Ignore metadata before start position.
buffer.subsampleOffsetUs = subsampleOffsetUs; buffer.subsampleOffsetUs = subsampleOffsetUs;
if (buffer.timeUs < getLastResetPositionUs()) {
buffer.addFlag(C.BUFFER_FLAG_DECODE_ONLY);
}
buffer.flip(); buffer.flip();
@Nullable Metadata metadata = castNonNull(decoder).decode(buffer); @Nullable Metadata metadata = castNonNull(decoder).decode(buffer);
if (metadata != null) { if (metadata != null) {

View File

@ -32,8 +32,7 @@ public interface MetadataDecoder {
* ByteBuffer#hasArray()} is true. * ByteBuffer#hasArray()} is true.
* *
* @param inputBuffer The input buffer to decode. * @param inputBuffer The input buffer to decode.
* @return The decoded metadata object, or {@code null} if the metadata could not be decoded or if * @return The decoded metadata object, or {@code null} if the metadata could not be decoded.
* {@link MetadataInputBuffer#isDecodeOnly()} was set on the input buffer.
*/ */
@Nullable @Nullable
Metadata decode(MetadataInputBuffer inputBuffer); Metadata decode(MetadataInputBuffer inputBuffer);

View File

@ -21,10 +21,7 @@ import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
/** /** A {@link MetadataDecoder} base class that validates input buffers. */
* A {@link MetadataDecoder} base class that validates input buffers and discards any for which
* {@link MetadataInputBuffer#isDecodeOnly()} is {@code true}.
*/
@UnstableApi @UnstableApi
public abstract class SimpleMetadataDecoder implements MetadataDecoder { public abstract class SimpleMetadataDecoder implements MetadataDecoder {
@ -34,7 +31,7 @@ public abstract class SimpleMetadataDecoder implements MetadataDecoder {
ByteBuffer buffer = Assertions.checkNotNull(inputBuffer.data); ByteBuffer buffer = Assertions.checkNotNull(inputBuffer.data);
Assertions.checkArgument( Assertions.checkArgument(
buffer.position() == 0 && buffer.hasArray() && buffer.arrayOffset() == 0); buffer.position() == 0 && buffer.hasArray() && buffer.arrayOffset() == 0);
return inputBuffer.isDecodeOnly() ? null : decode(inputBuffer, buffer); return decode(inputBuffer, buffer);
} }
/** /**

View File

@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertThrows;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.C;
import androidx.media3.common.Metadata; import androidx.media3.common.Metadata;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -72,17 +71,6 @@ public class SimpleMetadataDecoderTest {
assertThat(decoder.decodeWasCalled).isFalse(); assertThat(decoder.decodeWasCalled).isFalse();
} }
@Test
public void decode_decodeOnlyBuffer_notPassedToDecodeInternal() {
TestSimpleMetadataDecoder decoder = new TestSimpleMetadataDecoder();
MetadataInputBuffer decodeOnlyBuffer = new MetadataInputBuffer();
decodeOnlyBuffer.data = ByteBuffer.wrap(new byte[8]);
decodeOnlyBuffer.setFlags(C.BUFFER_FLAG_DECODE_ONLY);
assertThat(decoder.decode(decodeOnlyBuffer)).isNull();
assertThat(decoder.decodeWasCalled).isFalse();
}
@Test @Test
public void decode_returnsDecodeInternalResult() { public void decode_returnsDecodeInternalResult() {
TestSimpleMetadataDecoder decoder = new TestSimpleMetadataDecoder(); TestSimpleMetadataDecoder decoder = new TestSimpleMetadataDecoder();