<commit_before>/*
 * Copyright @ 2018 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.nlj.transform.module.incoming

import org.jitsi.nlj.transform.module.Module
import org.jitsi.nlj.transform.module.forEachAs
import org.jitsi.rtp.Packet
import org.jitsi.rtp.SrtpPacket
import org.jitsi.rtp.rtcp.RtcpHeader
import org.jitsi.rtp.rtcp.RtcpPacket
import org.jitsi.rtp.rtcp.rtcpfb.RtcpFbPacket
import org.jitsi.rtp.rtcp.rtcpfb.Tcc
import unsigned.toUInt

class TccGeneratorModule(
    private val tccExtId: Int,
    private val onTccPacketReady: (RtcpPacket) -> Unit = {}
) : Module("TCC generator") {
    private var currTccSeqNum: Int = 0
    private var currTcc: Tcc = Tcc(feedbackPacketCount = currTccSeqNum++)
    private var tempDetectedSsrc: Long? = null
    override fun doProcessPackets(p: List<Packet>) {
        val now = System.currentTimeMillis()
        p.forEachAs<SrtpPacket> {
            it.header.getExtension(tccExtId).let { tccExt ->
                //TODO: check if it's a one byte or two byte ext?
                val tccSeqNum = tccExt?.data?.getShort(0)?.toUInt() ?: return@let
                addPacket(tccSeqNum, now)
            }
            if (tempDetectedSsrc == null) {
                tempDetectedSsrc = it.header.ssrc
            }
        }
        next(p)
    }

    private fun addPacket(tccSeqNum: Int, timestamp: Long) {
        currTcc.addPacket(tccSeqNum, timestamp)

        if (isTccReadyToSend()) {
            val pkt = RtcpFbPacket(feedbackControlInformation = currTcc)
            pkt.mediaSourceSsrc = tempDetectedSsrc!!
            onTccPacketReady(pkt)
            // Create a new TCC instance for the next set of information
            currTcc = Tcc(feedbackPacketCount = currTccSeqNum++)
        }
    }

    private fun isTccReadyToSend(): Boolean = currTcc.packetInfo.size >= 20
}<commit_msg>add numTccSent stat for TccGenerator<commit_after>/*
 * Copyright @ 2018 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jitsi.nlj.transform.module.incoming

import org.jitsi.nlj.transform.module.Module
import org.jitsi.nlj.transform.module.forEachAs
import org.jitsi.nlj.util.appendLnIndent
import org.jitsi.rtp.Packet
import org.jitsi.rtp.SrtpPacket
import org.jitsi.rtp.rtcp.RtcpPacket
import org.jitsi.rtp.rtcp.rtcpfb.RtcpFbPacket
import org.jitsi.rtp.rtcp.rtcpfb.Tcc
import unsigned.toUInt

class TccGeneratorModule(
    private val tccExtId: Int,
    private val onTccPacketReady: (RtcpPacket) -> Unit = {}
) : Module("TCC generator") {
    private var currTccSeqNum: Int = 0
    private var currTcc: Tcc = Tcc(feedbackPacketCount = currTccSeqNum++)
    private var tempDetectedSsrc: Long? = null
    private var numTccSent: Int = 0
    override fun doProcessPackets(p: List<Packet>) {
        val now = System.currentTimeMillis()
        p.forEachAs<SrtpPacket> {
            it.header.getExtension(tccExtId).let { tccExt ->
                //TODO: check if it's a one byte or two byte ext?
                val tccSeqNum = tccExt?.data?.getShort(0)?.toUInt() ?: return@let
                addPacket(tccSeqNum, now)
            }
            if (tempDetectedSsrc == null) {
                tempDetectedSsrc = it.header.ssrc
            }
        }
        next(p)
    }

    private fun addPacket(tccSeqNum: Int, timestamp: Long) {
        currTcc.addPacket(tccSeqNum, timestamp)

        if (isTccReadyToSend()) {
            val pkt = RtcpFbPacket(feedbackControlInformation = currTcc)
            pkt.mediaSourceSsrc = tempDetectedSsrc!!
            onTccPacketReady(pkt)
            numTccSent++
            // Create a new TCC instance for the next set of information
            currTcc = Tcc(feedbackPacketCount = currTccSeqNum++)
        }
    }

    private fun isTccReadyToSend(): Boolean = currTcc.packetInfo.size >= 20

    override fun getStats(indent: Int): String {
        return with (StringBuffer()) {
            append(super.getStats(indent))
            appendLnIndent(indent + 2, "num tcc packets sent: $numTccSent")
            toString()
        }
    }
}
{'fraction_non_alphanumeric': 0.0907671286355651, 'fraction_numerical': 0.005075151278547726, 'mean_word_length': 2.7955555555555556, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2146595', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1580, 'n_words': 433}