<commit_before>/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jitsi.impl.neomedia.rtcp;

import java.io.*;
import java.util.*;

import net.sf.fmj.media.rtp.*;
import org.jitsi.impl.neomedia.*;
import org.jitsi.util.*;

/**
 * A class which represents an RTCP Generic NACK feedback message, as defined
 * in RFC4585 Section 6.2.1.
 *
 * The RTCP packet structure is:
 *
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |V=2|P| FMT=1   |   PT=205      |             length            |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                  SSRC of packet sender                        |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                  SSRC of media source                         |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                           FCI                                 |
 * |                          [...]                                |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * The Feedback Control Information (FCI) field consists of one or more
 * 32-bit words, each with the following structure:
 *
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |             PID               |             BLP               |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * @author <NAME>
 */
public class NACKPacket
    extends RTCPFBPacket
{
    /**
     * The <tt>Logger</tt> used by the <tt>NACKPacket</tt> class and its
     * instances for logging output.
     */
    private static final Logger logger
            = Logger.getLogger(NACKPacket.class);

    /**
     * The value of the "fmt" field for a NACK packet.
     */
    public static final int FMT = 1;

    /**
     * The set of sequence numbers described by this packet.
     */
    private Collection<Integer> lostPackets = null;

    /**
     * Initializes a new <tt>NACKPacket</tt> instance.
     * @param base
     */
    public NACKPacket(RTCPCompoundPacket base)
    {
        super(base);
    }

    /**
     * Initializes a new <tt>NACKPacket</tt> instance with specific "packet
     * sender SSRC" and "media source SSRC" values and which describes a
     * specific set of sequence numbers.
     * @param senderSSRC the value to use for the "packet sender SSRC" field.
     * @param sourceSSRC the value to use for the "media source SSRC" field.
     * @param lostPackets the set of RTP sequence numbers which this NACK
     * packet is to describe.
     *
     * Note that this implementation is not optimized and might not always use
     * the minimal possible number of bytes to describe a given set of packets.
     * Specifically, it does take into account that sequence numbers wrap
     * at 2^16 and fails to pack numbers close to 2^16 with those close to 0.
     */
    public NACKPacket(long senderSSRC, long sourceSSRC,
                      Collection<Integer> lostPackets)
    {
        super(FMT, RTPFB, senderSSRC, sourceSSRC);

        List<Integer> sorted = new LinkedList<Integer>(lostPackets);
        Collections.sort(sorted);
        List<byte[]> nackList = new LinkedList<byte[]>();

        int currentPid = -1;
        byte[] currentNack = null;
        for (int seq : sorted)
        {
            if (currentPid == -1 || currentPid + 16 <= seq)
            {
                currentPid = seq;
                currentNack = new byte[4];
                currentNack[0] = (byte) (seq & 0xff00);
                currentNack[1] = (byte) (seq & 0x00ff);
                currentNack[2] = 0;
                currentNack[3] = 0;

                nackList.add(currentNack);
<commit_msg>Fix the PID field in generated NACK packets.<commit_after>/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jitsi.impl.neomedia.rtcp;

import java.io.*;
import java.util.*;

import net.sf.fmj.media.rtp.*;
import org.jitsi.impl.neomedia.*;
import org.jitsi.util.*;

/**
 * A class which represents an RTCP Generic NACK feedback message, as defined
 * in RFC4585 Section 6.2.1.
 *
 * The RTCP packet structure is:
 *
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |V=2|P| FMT=1   |   PT=205      |             length            |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                  SSRC of packet sender                        |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                  SSRC of media source                         |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                           FCI                                 |
 * |                          [...]                                |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * The Feedback Control Information (FCI) field consists of one or more
 * 32-bit words, each with the following structure:
 *
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |             PID               |             BLP               |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *
 * @author <NAME>
 */
public class NACKPacket
    extends RTCPFBPacket
{
    /**
     * The <tt>Logger</tt> used by the <tt>NACKPacket</tt> class and its
     * instances for logging output.
     */
    private static final Logger logger
            = Logger.getLogger(NACKPacket.class);

    /**
     * The value of the "fmt" field for a NACK packet.
     */
    public static final int FMT = 1;

    /**
     * The set of sequence numbers described by this packet.
     */
    private Collection<Integer> lostPackets = null;

    /**
     * Initializes a new <tt>NACKPacket</tt> instance.
     * @param base
     */
    public NACKPacket(RTCPCompoundPacket base)
    {
        super(base);
    }

    /**
     * Initializes a new <tt>NACKPacket</tt> instance with specific "packet
     * sender SSRC" and "media source SSRC" values and which describes a
     * specific set of sequence numbers.
     * @param senderSSRC the value to use for the "packet sender SSRC" field.
     * @param sourceSSRC the value to use for the "media source SSRC" field.
     * @param lostPackets the set of RTP sequence numbers which this NACK
     * packet is to describe.
     *
     * Note that this implementation is not optimized and might not always use
     * the minimal possible number of bytes to describe a given set of packets.
     * Specifically, it does take into account that sequence numbers wrap
     * at 2^16 and fails to pack numbers close to 2^16 with those close to 0.
     */
    public NACKPacket(long senderSSRC, long sourceSSRC,
                      Collection<Integer> lostPackets)
    {
        super(FMT, RTPFB, senderSSRC, sourceSSRC);

        List<Integer> sorted = new LinkedList<Integer>(lostPackets);
        Collections.sort(sorted);
        List<byte[]> nackList = new LinkedList<byte[]>();

        int currentPid = -1;
        byte[] currentNack = null;
        for (int seq : sorted)
        {
            if (currentPid == -1 || currentPid + 16 <= seq)
            {
                currentPid = seq;
                currentNack = new byte[4];
                currentNack[0] = (byte) ((seq & 0xff00) >> 8);
                currentNack[1] = (byte) (seq & 0x00ff);
                currentNack[2] = 0;
                currentNack[3] = 0;

                nackList.add(currentNack);

{'fraction_non_alphanumeric': 0.1901803118908382, 'fraction_numerical': 0.027168615984405457, 'mean_word_length': 1.501981103322158, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '971800', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2340, 'n_words': 790}