Rabbit-R1/switch port/java/sources/androidx/media3/exoplayer/audio/DecoderAudioRenderer.java

599 lines
24 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package androidx.media3.exoplayer.audio;
import android.media.AudioDeviceInfo;
import android.os.Handler;
import android.os.SystemClock;
import androidx.media3.common.AudioAttributes;
import androidx.media3.common.AuxEffectInfo;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.audio.AudioProcessor;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.TraceUtil;
import androidx.media3.common.util.Util;
import androidx.media3.decoder.CryptoConfig;
import androidx.media3.decoder.Decoder;
import androidx.media3.decoder.DecoderException;
import androidx.media3.decoder.DecoderInputBuffer;
import androidx.media3.decoder.SimpleDecoderOutputBuffer;
import androidx.media3.exoplayer.BaseRenderer;
import androidx.media3.exoplayer.DecoderCounters;
import androidx.media3.exoplayer.DecoderReuseEvaluation;
import androidx.media3.exoplayer.ExoPlaybackException;
import androidx.media3.exoplayer.FormatHolder;
import androidx.media3.exoplayer.MediaClock;
import androidx.media3.exoplayer.RendererCapabilities;
import androidx.media3.exoplayer.audio.AudioRendererEventListener;
import androidx.media3.exoplayer.audio.AudioSink;
import androidx.media3.exoplayer.audio.DefaultAudioSink;
import androidx.media3.exoplayer.drm.DrmSession;
import com.google.common.base.MoreObjects;
/* loaded from: classes2.dex */
public abstract class DecoderAudioRenderer<T extends Decoder<DecoderInputBuffer, ? extends SimpleDecoderOutputBuffer, ? extends DecoderException>> extends BaseRenderer implements MediaClock {
private static final int MAX_PENDING_OUTPUT_STREAM_OFFSET_COUNT = 10;
private static final int REINITIALIZATION_STATE_NONE = 0;
private static final int REINITIALIZATION_STATE_SIGNAL_END_OF_STREAM = 1;
private static final int REINITIALIZATION_STATE_WAIT_END_OF_STREAM = 2;
private static final String TAG = "DecoderAudioRenderer";
private boolean allowFirstBufferPositionDiscontinuity;
private boolean allowPositionDiscontinuity;
private final AudioSink audioSink;
private boolean audioTrackNeedsConfigure;
private long currentPositionUs;
private T decoder;
private DecoderCounters decoderCounters;
private DrmSession decoderDrmSession;
private boolean decoderReceivedBuffers;
private int decoderReinitializationState;
private int encoderDelay;
private int encoderPadding;
private final AudioRendererEventListener.EventDispatcher eventDispatcher;
private boolean experimentalKeepAudioTrackOnSeek;
private boolean firstStreamSampleRead;
private final DecoderInputBuffer flagsOnlyBuffer;
private DecoderInputBuffer inputBuffer;
private Format inputFormat;
private boolean inputStreamEnded;
private SimpleDecoderOutputBuffer outputBuffer;
private boolean outputStreamEnded;
private long outputStreamOffsetUs;
private int pendingOutputStreamOffsetCount;
private final long[] pendingOutputStreamOffsetsUs;
private DrmSession sourceDrmSession;
protected abstract T createDecoder(Format format, CryptoConfig cryptoConfig) throws DecoderException;
public void experimentalSetEnableKeepAudioTrackOnSeek(boolean z) {
this.experimentalKeepAudioTrackOnSeek = z;
}
@Override // androidx.media3.exoplayer.BaseRenderer, androidx.media3.exoplayer.Renderer
public MediaClock getMediaClock() {
return this;
}
protected abstract Format getOutputFormat(T t);
protected void onPositionDiscontinuity() {
this.allowPositionDiscontinuity = true;
}
protected abstract int supportsFormatInternal(Format format);
public DecoderAudioRenderer() {
this((Handler) null, (AudioRendererEventListener) null, new AudioProcessor[0]);
}
public DecoderAudioRenderer(Handler handler, AudioRendererEventListener audioRendererEventListener, AudioProcessor... audioProcessorArr) {
this(handler, audioRendererEventListener, null, audioProcessorArr);
}
public DecoderAudioRenderer(Handler handler, AudioRendererEventListener audioRendererEventListener, AudioCapabilities audioCapabilities, AudioProcessor... audioProcessorArr) {
this(handler, audioRendererEventListener, new DefaultAudioSink.Builder().setAudioCapabilities((AudioCapabilities) MoreObjects.firstNonNull(audioCapabilities, AudioCapabilities.DEFAULT_AUDIO_CAPABILITIES)).setAudioProcessors(audioProcessorArr).build());
}
public DecoderAudioRenderer(Handler handler, AudioRendererEventListener audioRendererEventListener, AudioSink audioSink) {
super(1);
this.eventDispatcher = new AudioRendererEventListener.EventDispatcher(handler, audioRendererEventListener);
this.audioSink = audioSink;
audioSink.setListener(new AudioSinkListener());
this.flagsOnlyBuffer = DecoderInputBuffer.newNoDataInstance();
this.decoderReinitializationState = 0;
this.audioTrackNeedsConfigure = true;
setOutputStreamOffsetUs(-9223372036854775807L);
this.pendingOutputStreamOffsetsUs = new long[10];
}
@Override // androidx.media3.exoplayer.RendererCapabilities
public final int supportsFormat(Format format) {
if (!MimeTypes.isAudio(format.sampleMimeType)) {
return RendererCapabilities.create(0);
}
int supportsFormatInternal = supportsFormatInternal(format);
if (supportsFormatInternal <= 2) {
return RendererCapabilities.create(supportsFormatInternal);
}
return RendererCapabilities.create(supportsFormatInternal, 8, Util.SDK_INT >= 21 ? 32 : 0);
}
protected final boolean sinkSupportsFormat(Format format) {
return this.audioSink.supportsFormat(format);
}
protected final int getSinkFormatSupport(Format format) {
return this.audioSink.getFormatSupport(format);
}
@Override // androidx.media3.exoplayer.Renderer
public void render(long j, long j2) throws ExoPlaybackException {
if (this.outputStreamEnded) {
try {
this.audioSink.playToEndOfStream();
return;
} catch (AudioSink.WriteException e) {
throw createRendererException(e, e.format, e.isRecoverable, 5002);
}
}
if (this.inputFormat == null) {
FormatHolder formatHolder = getFormatHolder();
this.flagsOnlyBuffer.clear();
int readSource = readSource(formatHolder, this.flagsOnlyBuffer, 2);
if (readSource != -5) {
if (readSource == -4) {
Assertions.checkState(this.flagsOnlyBuffer.isEndOfStream());
this.inputStreamEnded = true;
try {
processEndOfStream();
return;
} catch (AudioSink.WriteException e2) {
throw createRendererException(e2, null, 5002);
}
}
return;
}
onInputFormatChanged(formatHolder);
}
maybeInitDecoder();
if (this.decoder != null) {
try {
TraceUtil.beginSection("drainAndFeed");
do {
} while (drainOutputBuffer());
do {
} while (feedInputBuffer());
TraceUtil.endSection();
this.decoderCounters.ensureUpdated();
} catch (DecoderException e3) {
Log.e(TAG, "Audio codec error", e3);
this.eventDispatcher.audioCodecError(e3);
throw createRendererException(e3, this.inputFormat, 4003);
} catch (AudioSink.ConfigurationException e4) {
throw createRendererException(e4, e4.format, 5001);
} catch (AudioSink.InitializationException e5) {
throw createRendererException(e5, e5.format, e5.isRecoverable, 5001);
} catch (AudioSink.WriteException e6) {
throw createRendererException(e6, e6.format, e6.isRecoverable, 5002);
}
}
}
protected DecoderReuseEvaluation canReuseDecoder(String str, Format format, Format format2) {
return new DecoderReuseEvaluation(str, format, format2, 0, 1);
}
private boolean drainOutputBuffer() throws ExoPlaybackException, DecoderException, AudioSink.ConfigurationException, AudioSink.InitializationException, AudioSink.WriteException {
if (this.outputBuffer == null) {
SimpleDecoderOutputBuffer simpleDecoderOutputBuffer = (SimpleDecoderOutputBuffer) this.decoder.dequeueOutputBuffer();
this.outputBuffer = simpleDecoderOutputBuffer;
if (simpleDecoderOutputBuffer == null) {
return false;
}
if (simpleDecoderOutputBuffer.skippedOutputBufferCount > 0) {
this.decoderCounters.skippedOutputBufferCount += this.outputBuffer.skippedOutputBufferCount;
this.audioSink.handleDiscontinuity();
}
if (this.outputBuffer.isFirstSample()) {
processFirstSampleOfStream();
}
}
if (this.outputBuffer.isEndOfStream()) {
if (this.decoderReinitializationState == 2) {
releaseDecoder();
maybeInitDecoder();
this.audioTrackNeedsConfigure = true;
} else {
this.outputBuffer.release();
this.outputBuffer = null;
try {
processEndOfStream();
} catch (AudioSink.WriteException e) {
throw createRendererException(e, e.format, e.isRecoverable, 5002);
}
}
return false;
}
if (this.audioTrackNeedsConfigure) {
this.audioSink.configure(getOutputFormat(this.decoder).buildUpon().setEncoderDelay(this.encoderDelay).setEncoderPadding(this.encoderPadding).build(), 0, null);
this.audioTrackNeedsConfigure = false;
}
if (!this.audioSink.handleBuffer(this.outputBuffer.data, this.outputBuffer.timeUs, 1)) {
return false;
}
this.decoderCounters.renderedOutputBufferCount++;
this.outputBuffer.release();
this.outputBuffer = null;
return true;
}
private void processFirstSampleOfStream() {
this.audioSink.handleDiscontinuity();
if (this.pendingOutputStreamOffsetCount != 0) {
setOutputStreamOffsetUs(this.pendingOutputStreamOffsetsUs[0]);
int i = this.pendingOutputStreamOffsetCount - 1;
this.pendingOutputStreamOffsetCount = i;
long[] jArr = this.pendingOutputStreamOffsetsUs;
System.arraycopy(jArr, 1, jArr, 0, i);
}
}
private void setOutputStreamOffsetUs(long j) {
this.outputStreamOffsetUs = j;
if (j != -9223372036854775807L) {
this.audioSink.setOutputStreamOffsetUs(j);
}
}
private boolean feedInputBuffer() throws DecoderException, ExoPlaybackException {
T t = this.decoder;
if (t == null || this.decoderReinitializationState == 2 || this.inputStreamEnded) {
return false;
}
if (this.inputBuffer == null) {
DecoderInputBuffer decoderInputBuffer = (DecoderInputBuffer) t.dequeueInputBuffer();
this.inputBuffer = decoderInputBuffer;
if (decoderInputBuffer == null) {
return false;
}
}
if (this.decoderReinitializationState == 1) {
this.inputBuffer.setFlags(4);
this.decoder.queueInputBuffer(this.inputBuffer);
this.inputBuffer = null;
this.decoderReinitializationState = 2;
return false;
}
FormatHolder formatHolder = getFormatHolder();
int readSource = readSource(formatHolder, this.inputBuffer, 0);
if (readSource == -5) {
onInputFormatChanged(formatHolder);
return true;
}
if (readSource != -4) {
if (readSource == -3) {
return false;
}
throw new IllegalStateException();
}
if (this.inputBuffer.isEndOfStream()) {
this.inputStreamEnded = true;
this.decoder.queueInputBuffer(this.inputBuffer);
this.inputBuffer = null;
return false;
}
if (!this.firstStreamSampleRead) {
this.firstStreamSampleRead = true;
this.inputBuffer.addFlag(134217728);
}
this.inputBuffer.flip();
this.inputBuffer.format = this.inputFormat;
onQueueInputBuffer(this.inputBuffer);
this.decoder.queueInputBuffer(this.inputBuffer);
this.decoderReceivedBuffers = true;
this.decoderCounters.queuedInputBufferCount++;
this.inputBuffer = null;
return true;
}
private void processEndOfStream() throws AudioSink.WriteException {
this.outputStreamEnded = true;
this.audioSink.playToEndOfStream();
}
private void flushDecoder() throws ExoPlaybackException {
if (this.decoderReinitializationState != 0) {
releaseDecoder();
maybeInitDecoder();
return;
}
this.inputBuffer = null;
SimpleDecoderOutputBuffer simpleDecoderOutputBuffer = this.outputBuffer;
if (simpleDecoderOutputBuffer != null) {
simpleDecoderOutputBuffer.release();
this.outputBuffer = null;
}
this.decoder.flush();
this.decoderReceivedBuffers = false;
}
@Override // androidx.media3.exoplayer.Renderer
public boolean isEnded() {
return this.outputStreamEnded && this.audioSink.isEnded();
}
@Override // androidx.media3.exoplayer.Renderer
public boolean isReady() {
return this.audioSink.hasPendingData() || (this.inputFormat != null && (isSourceReady() || this.outputBuffer != null));
}
@Override // androidx.media3.exoplayer.MediaClock
public long getPositionUs() {
if (getState() == 2) {
updateCurrentPosition();
}
return this.currentPositionUs;
}
@Override // androidx.media3.exoplayer.MediaClock
public void setPlaybackParameters(PlaybackParameters playbackParameters) {
this.audioSink.setPlaybackParameters(playbackParameters);
}
@Override // androidx.media3.exoplayer.MediaClock
public PlaybackParameters getPlaybackParameters() {
return this.audioSink.getPlaybackParameters();
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onEnabled(boolean z, boolean z2) throws ExoPlaybackException {
DecoderCounters decoderCounters = new DecoderCounters();
this.decoderCounters = decoderCounters;
this.eventDispatcher.enabled(decoderCounters);
if (getConfiguration().tunneling) {
this.audioSink.enableTunnelingV21();
} else {
this.audioSink.disableTunneling();
}
this.audioSink.setPlayerId(getPlayerId());
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onPositionReset(long j, boolean z) throws ExoPlaybackException {
if (this.experimentalKeepAudioTrackOnSeek) {
this.audioSink.experimentalFlushWithoutAudioTrackRelease();
} else {
this.audioSink.flush();
}
this.currentPositionUs = j;
this.allowFirstBufferPositionDiscontinuity = true;
this.allowPositionDiscontinuity = true;
this.inputStreamEnded = false;
this.outputStreamEnded = false;
if (this.decoder != null) {
flushDecoder();
}
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onStarted() {
this.audioSink.play();
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onStopped() {
updateCurrentPosition();
this.audioSink.pause();
}
@Override // androidx.media3.exoplayer.BaseRenderer
protected void onDisabled() {
this.inputFormat = null;
this.audioTrackNeedsConfigure = true;
setOutputStreamOffsetUs(-9223372036854775807L);
try {
setSourceDrmSession(null);
releaseDecoder();
this.audioSink.reset();
} finally {
this.eventDispatcher.disabled(this.decoderCounters);
}
}
/* JADX INFO: Access modifiers changed from: protected */
@Override // androidx.media3.exoplayer.BaseRenderer
public void onStreamChanged(Format[] formatArr, long j, long j2) throws ExoPlaybackException {
super.onStreamChanged(formatArr, j, j2);
this.firstStreamSampleRead = false;
if (this.outputStreamOffsetUs == -9223372036854775807L) {
setOutputStreamOffsetUs(j2);
return;
}
int i = this.pendingOutputStreamOffsetCount;
if (i == this.pendingOutputStreamOffsetsUs.length) {
Log.w(TAG, "Too many stream changes, so dropping offset: " + this.pendingOutputStreamOffsetsUs[this.pendingOutputStreamOffsetCount - 1]);
} else {
this.pendingOutputStreamOffsetCount = i + 1;
}
this.pendingOutputStreamOffsetsUs[this.pendingOutputStreamOffsetCount - 1] = j2;
}
@Override // androidx.media3.exoplayer.BaseRenderer, androidx.media3.exoplayer.PlayerMessage.Target
public void handleMessage(int i, Object obj) throws ExoPlaybackException {
if (i == 2) {
this.audioSink.setVolume(((Float) obj).floatValue());
return;
}
if (i == 3) {
this.audioSink.setAudioAttributes((AudioAttributes) obj);
return;
}
if (i == 6) {
this.audioSink.setAuxEffectInfo((AuxEffectInfo) obj);
return;
}
if (i == 12) {
if (Util.SDK_INT >= 23) {
Api23.setAudioSinkPreferredDevice(this.audioSink, obj);
}
} else if (i == 9) {
this.audioSink.setSkipSilenceEnabled(((Boolean) obj).booleanValue());
} else if (i == 10) {
this.audioSink.setAudioSessionId(((Integer) obj).intValue());
} else {
super.handleMessage(i, obj);
}
}
private void maybeInitDecoder() throws ExoPlaybackException {
CryptoConfig cryptoConfig;
if (this.decoder != null) {
return;
}
setDecoderDrmSession(this.sourceDrmSession);
DrmSession drmSession = this.decoderDrmSession;
if (drmSession != null) {
cryptoConfig = drmSession.getCryptoConfig();
if (cryptoConfig == null && this.decoderDrmSession.getError() == null) {
return;
}
} else {
cryptoConfig = null;
}
try {
long elapsedRealtime = SystemClock.elapsedRealtime();
TraceUtil.beginSection("createAudioDecoder");
this.decoder = createDecoder(this.inputFormat, cryptoConfig);
TraceUtil.endSection();
long elapsedRealtime2 = SystemClock.elapsedRealtime();
this.eventDispatcher.decoderInitialized(this.decoder.getName(), elapsedRealtime2, elapsedRealtime2 - elapsedRealtime);
this.decoderCounters.decoderInitCount++;
} catch (DecoderException e) {
Log.e(TAG, "Audio codec error", e);
this.eventDispatcher.audioCodecError(e);
throw createRendererException(e, this.inputFormat, 4001);
} catch (OutOfMemoryError e2) {
throw createRendererException(e2, this.inputFormat, 4001);
}
}
private void releaseDecoder() {
this.inputBuffer = null;
this.outputBuffer = null;
this.decoderReinitializationState = 0;
this.decoderReceivedBuffers = false;
if (this.decoder != null) {
this.decoderCounters.decoderReleaseCount++;
this.decoder.release();
this.eventDispatcher.decoderReleased(this.decoder.getName());
this.decoder = null;
}
setDecoderDrmSession(null);
}
private void setSourceDrmSession(DrmSession drmSession) {
DrmSession.replaceSession(this.sourceDrmSession, drmSession);
this.sourceDrmSession = drmSession;
}
private void setDecoderDrmSession(DrmSession drmSession) {
DrmSession.replaceSession(this.decoderDrmSession, drmSession);
this.decoderDrmSession = drmSession;
}
private void onInputFormatChanged(FormatHolder formatHolder) throws ExoPlaybackException {
DecoderReuseEvaluation canReuseDecoder;
Format format = (Format) Assertions.checkNotNull(formatHolder.format);
setSourceDrmSession(formatHolder.drmSession);
Format format2 = this.inputFormat;
this.inputFormat = format;
this.encoderDelay = format.encoderDelay;
this.encoderPadding = format.encoderPadding;
T t = this.decoder;
if (t == null) {
maybeInitDecoder();
this.eventDispatcher.inputFormatChanged(this.inputFormat, null);
return;
}
if (this.sourceDrmSession != this.decoderDrmSession) {
canReuseDecoder = new DecoderReuseEvaluation(this.decoder.getName(), format2, format, 0, 128);
} else {
canReuseDecoder = canReuseDecoder(t.getName(), format2, format);
}
if (canReuseDecoder.result == 0) {
if (this.decoderReceivedBuffers) {
this.decoderReinitializationState = 1;
} else {
releaseDecoder();
maybeInitDecoder();
this.audioTrackNeedsConfigure = true;
}
}
this.eventDispatcher.inputFormatChanged(this.inputFormat, canReuseDecoder);
}
protected void onQueueInputBuffer(DecoderInputBuffer decoderInputBuffer) {
if (!this.allowFirstBufferPositionDiscontinuity || decoderInputBuffer.isDecodeOnly()) {
return;
}
if (Math.abs(decoderInputBuffer.timeUs - this.currentPositionUs) > 500000) {
this.currentPositionUs = decoderInputBuffer.timeUs;
}
this.allowFirstBufferPositionDiscontinuity = false;
}
private void updateCurrentPosition() {
long currentPositionUs = this.audioSink.getCurrentPositionUs(isEnded());
if (currentPositionUs != Long.MIN_VALUE) {
if (!this.allowPositionDiscontinuity) {
currentPositionUs = Math.max(this.currentPositionUs, currentPositionUs);
}
this.currentPositionUs = currentPositionUs;
this.allowPositionDiscontinuity = false;
}
}
/* loaded from: classes2.dex */
private final class AudioSinkListener implements AudioSink.Listener {
private AudioSinkListener() {
}
@Override // androidx.media3.exoplayer.audio.AudioSink.Listener
public void onPositionDiscontinuity() {
DecoderAudioRenderer.this.onPositionDiscontinuity();
}
@Override // androidx.media3.exoplayer.audio.AudioSink.Listener
public void onPositionAdvancing(long j) {
DecoderAudioRenderer.this.eventDispatcher.positionAdvancing(j);
}
@Override // androidx.media3.exoplayer.audio.AudioSink.Listener
public void onUnderrun(int i, long j, long j2) {
DecoderAudioRenderer.this.eventDispatcher.underrun(i, j, j2);
}
@Override // androidx.media3.exoplayer.audio.AudioSink.Listener
public void onSkipSilenceEnabledChanged(boolean z) {
DecoderAudioRenderer.this.eventDispatcher.skipSilenceEnabledChanged(z);
}
@Override // androidx.media3.exoplayer.audio.AudioSink.Listener
public void onAudioSinkError(Exception exc) {
Log.e(DecoderAudioRenderer.TAG, "Audio sink error", exc);
DecoderAudioRenderer.this.eventDispatcher.audioSinkError(exc);
}
}
/* loaded from: classes2.dex */
private static final class Api23 {
private Api23() {
}
public static void setAudioSinkPreferredDevice(AudioSink audioSink, Object obj) {
audioSink.setPreferredDevice((AudioDeviceInfo) obj);
}
}
}