package com.foxx.androidcast.media.codec;

/*********************************************************************
 * AudioCodecFactory.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
 * class AudioCodecFactory
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 25 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	e8b2c92d5f3a97150adcf9af3f44791d2ee6a0db31dd7b7d96cb970bedc5e9fd
**********************************************************************/
import com.foxx.androidcast.CastSettings;
import com.foxx.androidcast.media.AudioNegotiator;
import com.foxx.androidcast.sender.AudioEncoder;

/** Creates audio encoder sinks. */
public final class AudioCodecFactory {
    private AudioCodecFactory() {}

    public static AudioEncoderSink createEncoder(CastSettings settings) {
        CastSettings.AudioCodec preference = settings != null
                ? AudioNegotiator.effective(settings) : CastSettings.AudioCodec.AAC;
        return createEncoder(preference);
    }

    public static AudioEncoderSink createEncoder(CastSettings.AudioCodec preference) {
        CodecSessionRegistry.setAudioPreference(preference);
        AudioCodecBackend backend = PassthroughCodecPolicy.audioBackendFor(preference, true);
        if (backend == AudioCodecBackend.SPEEX_STUB
                || backend == AudioCodecBackend.OPUS_STUB
                || backend == AudioCodecBackend.PASSTHROUGH_DEBUG) {
            return new PassthroughAudioEncoderSink(new AudioEncoder(), preference);
        }
        return new AudioEncoder();
    }
}
