<commit_before>//  
// Copyright (C) 2007-2011 SIPez LLC.  All rights reserved.
// Licensed to SIPfoundry under a Contributor Agreement. 
//
// Copyright (C) 2007-2008 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// $$
///////////////////////////////////////////////////////////////////////////////

// Author: <NAME> <Alexander DOT Chemeris AT SIPez DOT com>

// SYSTEM INCLUDES
// APPLICATION INCLUDES
#include <os/OsIntTypes.h>
#include <mp/MpRtpBuf.h>
#include <mp/MprRtpDispatcherIpAffinity.h>
#include <mp/MprDejitter.h>
#include <os/OsSocket.h>
#include <os/OsSysLog.h>

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS
const int MprRtpDispatcherIpAffinity::SSRC_SWITCH_MISMATCH_COUNT = 8;

/* //////////////////////////// PUBLIC //////////////////////////////////// */

/* ============================ CREATORS ================================== */

// Constructor
MprRtpDispatcherIpAffinity::MprRtpDispatcherIpAffinity(const UtlString& rName,
                                                       int connectionId)
: MprRtpDispatcher(rName, connectionId)
, mpDejitter(new MprDejitter())
, mPrefSsrcValid(FALSE)
, mRtpDestIp(0)
, mRtpDestPort(0)
, mNumPushed(0)
, mNumDropped(0)
, mTotalWarnings(0)
, mNumWarnings(0)
, mNumNonPrefPackets(0)
, mRtpDestMatchIpOnlySsrc(0)
, mRtpDestMatchIpOnlySsrcValid(FALSE)
, mRtpOtherSsrc(0)
, mRtpOtherSsrcValid(FALSE)
{
   mRtpStream.mStreamId = 0;
}

// Destructor
MprRtpDispatcherIpAffinity::~MprRtpDispatcherIpAffinity()
{
   // We own Dejitter and should delete it.
   delete mpDejitter;
}

/* ============================ MANIPULATORS ============================== */

OsStatus MprRtpDispatcherIpAffinity::pushPacket(MpRtpBufPtr &pRtp)
{
   OsLock lock(mMutex);
   OsStatus    ret = OS_SUCCESS;
   RtpSRC      thisSsrc;
   const in_addr* fromIP = &pRtp->getIP();
   int         fromPort = pRtp->getUdpPort();

   mNumPushed++;
   if (0 == (mNumPushed & ((1<<11)-1)))
      mNumWarnings = 0; // every 2048

   // What SSRC is this from?
   thisSsrc = pRtp->getRtpSSRC();
   if (!mPrefSsrcValid || !mRtpStream.isActive())
   {
      setPrefSsrc(thisSsrc);
      mRtpStream.activate(fromIP->s_addr, fromPort);
      // Notify about stream activation
      if (areNotificationsEnabled())
      {
         mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                    MprnRtpStreamActivityMsg::STREAM_START);
      }
   }

   // If this is from our preferred SSRC,
   if (thisSsrc == getPrefSsrc())
   {
      mNumNonPrefPackets = 0;

      // Track when the source IP changes
      if ((mRtpStream.mAddress != fromIP->s_addr) || (mRtpStream.mPort != fromPort))
      {
         mRtpStream.mAddress = fromIP->s_addr;
         mRtpStream.mPort = fromPort;
         // Notify about stream change
         if (areNotificationsEnabled())
         {
            mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                       MprnRtpStreamActivityMsg::STREAM_CHANGE);
         }
      }

      mRtpStream.pushPacket(pRtp);
      // Warning! pRtp may contain some random packet or even NULL from now on.
   }
   // Not from our preferred SSRC
   else
   {
      mTotalWarnings++;

      if (mNumWarnings++ < 20)
      {
         UtlString Old(""), New("");
         struct in_addr t;
         t.s_addr = mRtpDestIp;
         OsSocket::inet_ntoa_pt(t, Old);
         OsSocket::inet_ntoa_pt(*fromIP, New);
         if (OsSysLog::willLog(FAC_MP, PRI_DEBUG))
            osPrintf("**MprFromNet recvd RTP pkt from SSRC %d (%s:%d), "
                     "expected SSRC %d (%s:%d)\n",
                     thisSsrc, New.data(), fromPort,
                     getPrefSsrc(), Old.data(), mRtpDestPort);
      }
      // If it's from the same IP:port, SSRC must have changed
      if ((fromIP->s_addr == mRtpDestIp) && (fromPort == mRtpDestPort))
      {
         setPrefSsrc(thisSsrc);
         // Notify about stream change
         if (areNotificationsEnabled())
         {
            mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                       MprnRtpStreamActivityMsg::STREAM_CHANGE);
         }
      }
      //
      //else if (mRtpRtcpMatchSsrcValid && thisSsrc == mRtpRtcpMatchSsrc)
      //{
      //    setPrefSsrc(thisSsrc);
      //}
      // OK, definitely from someone else
      else
      {
         mNumNonPrefPackets++;

         // If this is from the same IP as pref'd but different port,
         if (fromIP->s_addr == mRtpDestIp)
         {
            mRtpDestMatchIpOnlySsrc = thisSsrc;
            mRtpDestMatchIpOnlySsrcValid = TRUE;
         }
         // Hey, this isn't even from the same IP as pref'd!
         else
         {
            mRtpOtherSsrc = thisSsrc;<commit_msg>Fixed compiler warnings in MprRtpDispatcherIpAffinity.cpp<commit_after>//  
// Copyright (C) 2007-2017 SIPez LLC.  All rights reserved.
//
// Copyright (C) 2007-2008 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// $$
///////////////////////////////////////////////////////////////////////////////

// Author: <NAME> <Alexander DOT Chemeris AT SIPez DOT com>

// SYSTEM INCLUDES
// APPLICATION INCLUDES
#include <os/OsIntTypes.h>
#include <mp/MpRtpBuf.h>
#include <mp/MprRtpDispatcherIpAffinity.h>
#include <mp/MprDejitter.h>
#include <os/OsSocket.h>
#include <os/OsSysLog.h>

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS
const int MprRtpDispatcherIpAffinity::SSRC_SWITCH_MISMATCH_COUNT = 8;

/* //////////////////////////// PUBLIC //////////////////////////////////// */

/* ============================ CREATORS ================================== */

// Constructor
MprRtpDispatcherIpAffinity::MprRtpDispatcherIpAffinity(const UtlString& rName,
                                                       int connectionId)
: MprRtpDispatcher(rName, connectionId)
, mpDejitter(new MprDejitter())
, mPrefSsrcValid(FALSE)
, mRtpDestIp(0)
, mRtpDestPort(0)
, mNumPushed(0)
, mNumDropped(0)
, mTotalWarnings(0)
, mNumWarnings(0)
, mNumNonPrefPackets(0)
, mRtpDestMatchIpOnlySsrc(0)
, mRtpDestMatchIpOnlySsrcValid(FALSE)
, mRtpOtherSsrc(0)
, mRtpOtherSsrcValid(FALSE)
{
   mRtpStream.mStreamId = 0;
}

// Destructor
MprRtpDispatcherIpAffinity::~MprRtpDispatcherIpAffinity()
{
   // We own Dejitter and should delete it.
   delete mpDejitter;
}

/* ============================ MANIPULATORS ============================== */

OsStatus MprRtpDispatcherIpAffinity::pushPacket(MpRtpBufPtr &pRtp)
{
   OsLock lock(mMutex);
   OsStatus    ret = OS_SUCCESS;
   RtpSRC      thisSsrc;
   const in_addr* fromIP = &pRtp->getIP();
   int fromPort = pRtp->getUdpPort();

   mNumPushed++;
   if (0 == (mNumPushed & ((1<<11)-1)))
      mNumWarnings = 0; // every 2048

   // What SSRC is this from?
   thisSsrc = pRtp->getRtpSSRC();
   if (!mPrefSsrcValid || !mRtpStream.isActive())
   {
      setPrefSsrc(thisSsrc);
      mRtpStream.activate(fromIP->s_addr, fromPort);
      // Notify about stream activation
      if (areNotificationsEnabled())
      {
         mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                    MprnRtpStreamActivityMsg::STREAM_START);
      }
   }

   // If this is from our preferred SSRC,
   if (thisSsrc == getPrefSsrc())
   {
      mNumNonPrefPackets = 0;

      // Track when the source IP changes
      if ((mRtpStream.mAddress != (int)(fromIP->s_addr)) ||
          (mRtpStream.mPort != fromPort))
      {
         mRtpStream.mAddress = fromIP->s_addr;
         mRtpStream.mPort = fromPort;
         // Notify about stream change
         if (areNotificationsEnabled())
         {
            mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                       MprnRtpStreamActivityMsg::STREAM_CHANGE);
         }
      }

      mRtpStream.pushPacket(pRtp);
      // Warning! pRtp may contain some random packet or even NULL from now on.
   }
   // Not from our preferred SSRC
   else
   {
      mTotalWarnings++;

      if (mNumWarnings++ < 20)
      {
         UtlString Old(""), New("");
         struct in_addr t;
         t.s_addr = mRtpDestIp;
         OsSocket::inet_ntoa_pt(t, Old);
         OsSocket::inet_ntoa_pt(*fromIP, New);
         if (OsSysLog::willLog(FAC_MP, PRI_DEBUG))
            osPrintf("**MprFromNet recvd RTP pkt from SSRC %d (%s:%d), "
                     "expected SSRC %d (%s:%u)\n",
                     thisSsrc, New.data(), fromPort,
                     getPrefSsrc(), Old.data(), mRtpDestPort);
      }
      // If it's from the same IP:port, SSRC must have changed
      if ((fromIP->s_addr == mRtpDestIp) && 
          (fromPort == mRtpDestPort))
      {
         setPrefSsrc(thisSsrc);
         // Notify about stream change
         if (areNotificationsEnabled())
         {
            mRtpStream.postStreamNotif(mpEventMsgQ, mResourceName, mConnectionId,
                                       MprnRtpStreamActivityMsg::STREAM_CHANGE);
         }
      }
      //
      //else if (mRtpRtcpMatchSsrcValid && thisSsrc == mRtpRtcpMatchSsrc)
      //{
      //    setPrefSsrc(thisSsrc);
      //}
      // OK, definitely from someone else
      else
      {
         mNumNonPrefPackets++;

         // If this is from the same IP as pref'd but different port,
         if (fromIP->s_addr == mRtpDestIp)
         {
            mRtpDestMatchIpOnlySsrc = thisSsrc;
            mRtpDestMatchIpOnlySsrcValid = TRUE;
         }
         // Hey, this isn't even from the same IP as pref'd!
         else
         {
            mRtpOtherSsrc = thisSsrc;
{'fraction_non_alphanumeric': 0.15882838283828382, 'fraction_numerical': 0.00825082508250825, 'mean_word_length': 2.178302196001311, 'pattern_counts': {'":': 0, '<': 25, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6607979', 'n_tokens_mistral': 3249, 'n_tokens_neox': 3135, 'n_words': 630}