package com.foxx.androidcast.media.codec;

/*********************************************************************
 * PassthroughAudioEncoderSink.java
 * Created at: Sun 17 May 2026 22:49:49 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 48 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	9e17f4283fe1f0aae032d7830fe767d8fc70deb184aa771aeb93d36fe3469a79
**********************************************************************/
import com.foxx.androidcast.CastSettings;
import com.foxx.androidcast.sender.AudioEncoder;

import java.io.IOException;

/** Delegates to AAC MediaCodec while UI shows a future Speex/Opus choice. */
final class PassthroughAudioEncoderSink implements AudioEncoderSink {
    private final AudioEncoderSink delegate;
    private final CastSettings.AudioCodec preference;

    PassthroughAudioEncoderSink(AudioEncoderSink delegate, CastSettings.AudioCodec preference) {
        this.delegate = delegate;
        this.preference = preference;
    }

    @Override
    public void prepare(Callback callback) throws IOException {
        if (preference == CastSettings.AudioCodec.OPUS) {
            CodecSessionRegistry.setAudioBackend(AudioCodecBackend.OPUS_STUB);
        } else if (preference == CastSettings.AudioCodec.SPEEX) {
            CodecSessionRegistry.setAudioBackend(AudioCodecBackend.SPEEX_STUB);
        } else if (preference == CastSettings.AudioCodec.PASSTHROUGH_DEBUG) {
            CodecSessionRegistry.setAudioBackend(AudioCodecBackend.PASSTHROUGH_DEBUG);
        }
        delegate.prepare(callback);
    }

    @Override
    public void queuePcm(byte[] pcm, int length, long ptsUs) {
        delegate.queuePcm(pcm, length, ptsUs);
    }

    @Override
    public long getDroppedPcmFrames() {
        return delegate.getDroppedPcmFrames();
    }

    @Override
    public void stop() {
        delegate.stop();
    }
}
