package com.foxx.androidcast.network.transport;

/*********************************************************************
 * StreamProtectionCompat.java
 * Created at: Mon 18 May 2026 14:25:02 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class StreamProtectionCompat
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 33 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	49de97502e18546009f074f42933ba0dadb2b8a6d574e5cd73902e69085a68bd
**********************************************************************/
import com.foxx.androidcast.network.transport.fec.FecShardWire;
import com.foxx.androidcast.network.transport.fec.ProtectionEnvelope;

/**
 * Backward-compatible handling of optional UDP stream protection (FEC/NACK).
 * Peers that did not negotiate protection must ignore protected payloads without
 * disconnecting or feeding decoders.
 */
public final class StreamProtectionCompat {
    private StreamProtectionCompat() {}

    /** True when the payload uses FEC wrapping this peer is not applying. */
    public static boolean isForeignProtectionPayload(byte[] body) {
        if (body == null || body.length < 4) {
            return false;
        }
        return FecShardWire.isShardPacket(body) || ProtectionEnvelope.isProtected(body);
    }

    /** Drop foreign protection silently (no NACK) when {@code activeMode} is {@link StreamProtectionMode#NONE}. */
    public static boolean shouldIgnoreInbound(byte[] body, StreamProtectionMode activeMode) {
        return activeMode == StreamProtectionMode.NONE && isForeignProtectionPayload(body);
    }
}
