Rabbit-R1/switch port/java/sources/androidx/media3/exoplayer/metadata/MetadataRenderer.java

206 lines
7.8 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package androidx.media3.exoplayer.metadata;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.media3.common.Format;
import androidx.media3.common.Metadata;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.BaseRenderer;
import androidx.media3.exoplayer.FormatHolder;
import androidx.media3.exoplayer.RendererCapabilities;
import androidx.media3.extractor.metadata.MetadataDecoder;
import androidx.media3.extractor.metadata.MetadataInputBuffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.checkerframework.dataflow.qual.SideEffectFree;
/* loaded from: classes2.dex */
public final class MetadataRenderer extends BaseRenderer implements Handler.Callback {
private static final int MSG_INVOKE_RENDERER = 0;
private static final String TAG = "MetadataRenderer";
private final MetadataInputBuffer buffer;
private MetadataDecoder decoder;
private final MetadataDecoderFactory decoderFactory;
private boolean inputStreamEnded;
private final MetadataOutput output;
private final Handler outputHandler;
private final boolean outputMetadataEarly;
private boolean outputStreamEnded;
private long outputStreamOffsetUs;
private Metadata pendingMetadata;
private long subsampleOffsetUs;
@Override // androidx.media3.exoplayer.Renderer, androidx.media3.exoplayer.RendererCapabilities
public String getName() {
return TAG;
}
@Override // androidx.media3.exoplayer.Renderer
public boolean isEnded() {
return this.outputStreamEnded;
}
@Override // androidx.media3.exoplayer.Renderer
public boolean isReady() {
return true;
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onDisabled() {
this.pendingMetadata = null;
this.decoder = null;
this.outputStreamOffsetUs = -9223372036854775807L;
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onPositionReset(long j, boolean z) {
this.pendingMetadata = null;
this.inputStreamEnded = false;
this.outputStreamEnded = false;
}
public MetadataRenderer(MetadataOutput metadataOutput, Looper looper) {
this(metadataOutput, looper, MetadataDecoderFactory.DEFAULT);
}
public MetadataRenderer(MetadataOutput metadataOutput, Looper looper, MetadataDecoderFactory metadataDecoderFactory) {
this(metadataOutput, looper, metadataDecoderFactory, false);
}
public MetadataRenderer(MetadataOutput metadataOutput, Looper looper, MetadataDecoderFactory metadataDecoderFactory, boolean z) {
super(5);
this.output = (MetadataOutput) Assertions.checkNotNull(metadataOutput);
this.outputHandler = looper == null ? null : Util.createHandler(looper, this);
this.decoderFactory = (MetadataDecoderFactory) Assertions.checkNotNull(metadataDecoderFactory);
this.outputMetadataEarly = z;
this.buffer = new MetadataInputBuffer();
this.outputStreamOffsetUs = -9223372036854775807L;
}
@Override // androidx.media3.exoplayer.RendererCapabilities
public int supportsFormat(Format format) {
if (this.decoderFactory.supportsFormat(format)) {
return RendererCapabilities.create(format.cryptoType == 0 ? 4 : 2);
}
return RendererCapabilities.create(0);
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // androidx.media3.exoplayer.BaseRenderer
public void onStreamChanged(Format[] formatArr, long j, long j2) {
this.decoder = this.decoderFactory.createDecoder(formatArr[0]);
Metadata metadata = this.pendingMetadata;
if (metadata != null) {
this.pendingMetadata = metadata.copyWithPresentationTimeUs((metadata.presentationTimeUs + this.outputStreamOffsetUs) - j2);
}
this.outputStreamOffsetUs = j2;
}
@Override // androidx.media3.exoplayer.Renderer
public void render(long j, long j2) {
boolean z = true;
while (z) {
readMetadata();
z = outputMetadata(j);
}
}
private void decodeWrappedMetadata(Metadata metadata, List<Metadata.Entry> list) {
for (int i = 0; i < metadata.length(); i++) {
Format wrappedMetadataFormat = metadata.get(i).getWrappedMetadataFormat();
if (wrappedMetadataFormat != null && this.decoderFactory.supportsFormat(wrappedMetadataFormat)) {
MetadataDecoder createDecoder = this.decoderFactory.createDecoder(wrappedMetadataFormat);
byte[] bArr = (byte[]) Assertions.checkNotNull(metadata.get(i).getWrappedMetadataBytes());
this.buffer.clear();
this.buffer.ensureSpaceForWrite(bArr.length);
((ByteBuffer) Util.castNonNull(this.buffer.data)).put(bArr);
this.buffer.flip();
Metadata decode = createDecoder.decode(this.buffer);
if (decode != null) {
decodeWrappedMetadata(decode, list);
}
} else {
list.add(metadata.get(i));
}
}
}
@Override // android.os.Handler.Callback
public boolean handleMessage(Message message) {
if (message.what == 0) {
invokeRendererInternal((Metadata) message.obj);
return true;
}
throw new IllegalStateException();
}
private void readMetadata() {
if (this.inputStreamEnded || this.pendingMetadata != null) {
return;
}
this.buffer.clear();
FormatHolder formatHolder = getFormatHolder();
int readSource = readSource(formatHolder, this.buffer, 0);
if (readSource != -4) {
if (readSource == -5) {
this.subsampleOffsetUs = ((Format) Assertions.checkNotNull(formatHolder.format)).subsampleOffsetUs;
}
} else {
if (this.buffer.isEndOfStream()) {
this.inputStreamEnded = true;
return;
}
this.buffer.subsampleOffsetUs = this.subsampleOffsetUs;
this.buffer.flip();
Metadata decode = ((MetadataDecoder) Util.castNonNull(this.decoder)).decode(this.buffer);
if (decode != null) {
ArrayList arrayList = new ArrayList(decode.length());
decodeWrappedMetadata(decode, arrayList);
if (arrayList.isEmpty()) {
return;
}
this.pendingMetadata = new Metadata(getPresentationTimeUs(this.buffer.timeUs), arrayList);
}
}
}
private boolean outputMetadata(long j) {
boolean z;
Metadata metadata = this.pendingMetadata;
if (metadata == null || (!this.outputMetadataEarly && metadata.presentationTimeUs > getPresentationTimeUs(j))) {
z = false;
} else {
invokeRenderer(this.pendingMetadata);
this.pendingMetadata = null;
z = true;
}
if (this.inputStreamEnded && this.pendingMetadata == null) {
this.outputStreamEnded = true;
}
return z;
}
private void invokeRenderer(Metadata metadata) {
Handler handler = this.outputHandler;
if (handler != null) {
handler.obtainMessage(0, metadata).sendToTarget();
} else {
invokeRendererInternal(metadata);
}
}
private void invokeRendererInternal(Metadata metadata) {
this.output.onMetadata(metadata);
}
@SideEffectFree
private long getPresentationTimeUs(long j) {
Assertions.checkState(j != -9223372036854775807L);
Assertions.checkState(this.outputStreamOffsetUs != -9223372036854775807L);
return j - this.outputStreamOffsetUs;
}
}