<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.jmfext.media.protocol.rtpdumpfile;

import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;

import org.jitsi.impl.neomedia.*;
import org.jitsi.impl.neomedia.jmfext.media.protocol.*;
import org.jitsi.util.*;

import java.io.*;

/**
 * Implements a <tt>PullBufferStream</tt> which read an rtpdump file to generate
 * a RTP stream from the payloads recorded in a rtpdump file.
 * 
 * @author <NAME>
 */
public class RtpdumpStream
    extends AbstractVideoPullBufferStream<DataSource>
{
    /**
     * The <tt>Logger</tt> used by <tt>RtpdumpStream</tt> and its instances
     * for logging output.
     */
    private static final Logger logger
            = Logger.getLogger(RtpdumpStream.class);

    /**
     * The RTP clock rate, used to interpret the RTP timestamps read from the
     * file.
     */
    private final long CLOCK_RATE;

    /**
     * The timestamp of the last rtp packet (the timestamp change only when
     * a marked packet has been sent).
     * 
     * It is initialize with Long.MAX_VALUE because the timestamp of the first
     * packet read isn't generally 0, and the difference between it and the
     * first value of lastRtpTimestamp would in this case be huge, making the
     * stream sleep forever. 
     */
    private long lastRtpTimestamp = Long.MAX_VALUE;

    /**
     * Boolean indicating if the last call to <tt>doRead</tt> return a marked
     * rtp packet (to know if <tt>timestamp</tt> needs to be updated).
     */
    private boolean lastReadWasMarked = true;

    /**
     * The <tt>RtpdumpFileReader</tt> used by this stream to get the rtp payload.
     */
    private RtpdumpFileReader rtpFileReader;

    /**
     * The timestamp to use for the timestamp of the next <tt>Buffer</tt> filled
     * in {@link #doRead(javax.media.Buffer)}
     */
    private long timestamp;

    /**
     * Initializes a new <tt>RtpdumpStream</tt> instance
     *
     * @param dataSource the <tt>DataSource</tt> which is creating the new
     * instance so that it becomes one of its <tt>streams</tt>
     * @param formatControl the <tt>FormatControl</tt> of the new instance which
     * is to specify the format in which it is to provide its media data
     */
    RtpdumpStream(DataSource dataSource, FormatControl formatControl)
    {
        super(dataSource, formatControl);

        /*
         * NOTE: We use the sampleRate or frameRate field of the format to
         * piggyback the RTP clock rate. See
         * RtpdumpMediaDevice#createRtpdumpMediaDevice.
         */
        Format format = getFormat();
        if (format instanceof AudioFormat)
        {
            CLOCK_RATE = (long) ((AudioFormat) format).getSampleRate();
        }
        else if (format instanceof VideoFormat)
        {
            CLOCK_RATE = (long) ((VideoFormat) format).getFrameRate();
        }
        else
        {
            logger.warn("Unknown format. Creating RtpdumpStream with clock" +
                                "rate 1 000 000 000.");
            CLOCK_RATE = 1000 * 1000 * 1000;
        }

        String rtpdumpFilePath = dataSource.getLocator().getRemainder();
        this.rtpFileReader = new RtpdumpFileReader(rtpdumpFilePath);
    }

    /**
     * Reads available media data from this instance into a specific
     * <tt>Buffer</tt>.
     *
     * @param buffer the <tt>Buffer</tt> to write the available media data
     * into
     * @throws IOException if an I/O error has prevented the reading of
     * available media data from this instance into the specified
     * <tt>Buffer</tt>
     */
    @Override
    protected void doRead(Buffer buffer)
        throws IOException
    {
        Format format;

        format = buffer.getFormat();
        if (format == null)
        {
            format = getFormat();
            if (format != null)
                buffer.setFormat(format);
        }

        RawPacket rtpPacket = rtpFileReader.getNextPacket(true);
        byte[] data = rtpPacket.getPayload(); 

        buffer.setData(data);
        buffer.setOffset(rtpPacket.getOffset());
        buffer.setLength(rtpPacket.getPayloadLength());

        buffer.setFlags(Buffer.FLAG_SYSTEM_TIME | Buffer.FLAG_LIVE_DATA);
        if(lastReadWasMarked)
        {
            timestamp = System.nanoTime();
        }
        lastReadWasMarked = rtpPacket.isPacketMarked();
        if(lastReadWasMarked)
        {
            buffer.setFlags(buffer.getFlags() | Buffer.FLAG_RTP_MARKER);
        }
        buffer.setTimeStamp(timestamp);

        long rtpDiff = rtpPacket.getTimestamp() - this.lastRtpTimestamp;
        if (rtpDiff < 0)
            rtpDiff += 1L << 32; //rtp timestamps wrap at 2^32

        long nanos = (rtpDiff * 1000 * 1000 * 1000) / CLOCK_RATE;
        if (nanos > 0)
        {
            try
            {
                Thread.sleep(
                               nanos / 1000000,
                        (int) (nanos % 1000000));
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        this.lastRtpTimestamp = rtpPacket.getTimestamp();
    }
}<commit_msg>Fix problem with sleep time calculation introduced in my last commit. Reported by <NAME>.<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.jmfext.media.protocol.rtpdumpfile;

import javax.media.*;
import javax.media.control.*;
import javax.media.format.*;

import org.jitsi.impl.neomedia.*;
import org.jitsi.impl.neomedia.jmfext.media.protocol.*;
import org.jitsi.util.*;

import java.io.*;

/**
 * Implements a <tt>PullBufferStream</tt> which read an rtpdump file to generate
 * a RTP stream from the payloads recorded in a rtpdump file.
 * 
 * @author <NAME>
 */
public class RtpdumpStream
    extends AbstractVideoPullBufferStream<DataSource>
{
    /**
     * The <tt>Logger</tt> used by <tt>RtpdumpStream</tt> and its instances
     * for logging output.
     */
    private static final Logger logger
            = Logger.getLogger(RtpdumpStream.class);

    /**
     * The RTP clock rate, used to interpret the RTP timestamps read from the
     * file.
     */
    private final long CLOCK_RATE;

    /**
     * The timestamp of the last rtp packet (the timestamp change only when
     * a marked packet has been sent).
     */
    private long lastRtpTimestamp = -1;

    /**
     * Boolean indicating if the last call to <tt>doRead</tt> return a marked
     * rtp packet (to know if <tt>timestamp</tt> needs to be updated).
     */
    private boolean lastReadWasMarked = true;

    /**
     * The <tt>RtpdumpFileReader</tt> used by this stream to get the rtp payload.
     */
    private RtpdumpFileReader rtpFileReader;

    /**
     * The timestamp to use for the timestamp of the next <tt>Buffer</tt> filled
     * in {@link #doRead(javax.media.Buffer)}
     */
    private long timestamp;

    /**
     * Initializes a new <tt>RtpdumpStream</tt> instance
     *
     * @param dataSource the <tt>DataSource</tt> which is creating the new
     * instance so that it becomes one of its <tt>streams</tt>
     * @param formatControl the <tt>FormatControl</tt> of the new instance which
     * is to specify the format in which it is to provide its media data
     */
    RtpdumpStream(DataSource dataSource, FormatControl formatControl)
    {
        super(dataSource, formatControl);

        /*
         * NOTE: We use the sampleRate or frameRate field of the format to
         * piggyback the RTP clock rate. See
         * RtpdumpMediaDevice#createRtpdumpMediaDevice.
         */
        Format format = getFormat();
        if (format instanceof AudioFormat)
        {
            CLOCK_RATE = (long) ((AudioFormat) format).getSampleRate();
        }
        else if (format instanceof VideoFormat)
        {
            CLOCK_RATE = (long) ((VideoFormat) format).getFrameRate();
        }
        else
        {
            logger.warn("Unknown format. Creating RtpdumpStream with clock" +
                                "rate 1 000 000 000.");
            CLOCK_RATE = 1000 * 1000 * 1000;
        }

        String rtpdumpFilePath = dataSource.getLocator().getRemainder();
        this.rtpFileReader = new RtpdumpFileReader(rtpdumpFilePath);
    }

    /**
     * Reads available media data from this instance into a specific
     * <tt>Buffer</tt>.
     *
     * @param buffer the <tt>Buffer</tt> to write the available media data
     * into
     * @throws IOException if an I/O error has prevented the reading of
     * available media data from this instance into the specified
     * <tt>Buffer</tt>
     */
    @Override
    protected void doRead(Buffer buffer)
        throws IOException
    {
        Format format;

        format = buffer.getFormat();
        if (format == null)
        {
            format = getFormat();
            if (format != null)
                buffer.setFormat(format);
        }

        RawPacket rtpPacket = rtpFileReader.getNextPacket(true);
        byte[] data = rtpPacket.getPayload(); 

        buffer.setData(data);
        buffer.setOffset(rtpPacket.getOffset());
        buffer.setLength(rtpPacket.getPayloadLength());

        buffer.setFlags(Buffer.FLAG_SYSTEM_TIME | Buffer.FLAG_LIVE_DATA);
        if(lastReadWasMarked)
        {
            timestamp = System.nanoTime();
        }
        lastReadWasMarked = rtpPacket.isPacketMarked();
        if(lastReadWasMarked)
        {
            buffer.setFlags(buffer.getFlags() | Buffer.FLAG_RTP_MARKER);
        }
        buffer.setTimeStamp(timestamp);

        if (lastRtpTimestamp == -1)
        {
            lastRtpTimestamp = 0xffffffffL & rtpPacket.getTimestamp();
            return;
        }

        long previous= lastRtpTimestamp;
        lastRtpTimestamp = 0xffffffffL & rtpPacket.getTimestamp();

        long rtpDiff = lastRtpTimestamp - previous;
        if (rtpDiff < 0)
            rtpDiff += 1L << 32; //rtp timestamps wrap at 2^32

        long nanos = (rtpDiff * 1000 * 1000 * 1000) / CLOCK_RATE;
        if (nanos > 0)
        {
            try
            {
                Thread.sleep(
                               nanos / 1000000,
                        (int) (nanos % 1000000));
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.09463900103276687, 'fraction_numerical': 0.010984884048446155, 'mean_word_length': 2.1182669789227164, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7153979', 'n_tokens_mistral': 3141, 'n_tokens_neox': 2965, 'n_words': 886}