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


/* There used to be #ifdef's here to do the same thing on WIN32/VXWORKS, but I
* took them out because we do the same thing on every OS. -Mike */
#define PRINTF Zprintf

// SYSTEM INCLUDES
#include <assert.h>

// APPLICATION INCLUDES
#include "os/OsLock.h"
#include "os/OsMsgPool.h"
#include "os/OsCallback.h"
#include "os/OsTimer.h"
#include "utl/UtlHashBagIterator.h"
#include "utl/UtlPtr.h"
#include "mp/MpFlowGraphBase.h"
#include "mp/MpMisc.h"
#include "mp/MpMediaTask.h"
#include "mp/MpMediaTaskMsg.h"
#include "mp/MpBufferMsg.h"
#include "mp/MpCodecFactory.h"
#include "mp/MpCodec.h"
#include "mp/MpAudioDriverManager.h"
#include "mp/MpAudioDriverBase.h"

#ifdef _WIN32
#include "mp/MpMMTimer.h"
#include "mp/MpMMTimerWnt.h"
#endif

#ifdef RTL_ENABLED
#   include <rtl_macro.h>
#endif

// Include sys/time.h if _PROFILE is set.
// This #include has to be after the #include of mp/MpMediaTask.h, because
// that is where we normally set _PROFILE.
#ifdef _PROFILE /* [ */
#include <sys/time.h>
#endif /* _PROFILE ] */

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
#define TEST_TASK_LOAD

#ifdef __pingtel_on_posix__ /* [ */
#define MPMEDIA_DEF_MAX_MSGS 1000
#else
#define MPMEDIA_DEF_MAX_MSGS OsServerTask::DEF_MAX_MSGS
#endif /* __pingtel_on_posix__ ] */

// STATIC VARIABLE INITIALIZATIONS
MpMediaTask* MpMediaTask::spInstance = NULL;
OsBSem       MpMediaTask::sLock(OsBSem::Q_PRIORITY, OsBSem::FULL);
UtlBoolean MpMediaTask::ms_bTestMode = FALSE;

#ifdef _PROFILE /* [ */
   static long long sSignalTicks; // Time (in microseconds) for the current
                                  // frame start signal
   static unsigned long long sMinTicks;
   static unsigned long long sMaxTicks;
#endif /* _PROFILE ] */

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

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

// Return a pointer to the media task, creating it if necessary
MpMediaTask* MpMediaTask::getMediaTask(UtlBoolean bCreate)
{
   UtlBoolean isStarted;

   // If the task object already exists, and the corresponding low-level task
   // has been started, then use it. If we do not request creation, also return NULL
   if ((spInstance != NULL && spInstance->isStarted()) || (!bCreate))
      return spInstance;

   // If the task does not yet exist or hasn't been started, then acquire
   // the lock to ensure that only one instance of the task is started
   OsLock lock(sLock);
   if (spInstance == NULL)
       spInstance = new MpMediaTask();

   isStarted = spInstance->isStarted();
   if (!isStarted)
   {
      isStarted = spInstance->start();
      spInstance->startFrameStartTimer();
      assert(isStarted);
   }

   return spInstance;
}

// Destructor
MpMediaTask::~MpMediaTask()
{
   // $$$ need to figure out how to cleanly shut down this task after
   // $$$ unmanaging and destroying all of its flow graphs<commit_msg>Fixed bug in MpMediaTask - destructor didn't call waitUntilShutDown() - so the thread didn't stop. Nobody else was stopping it either. This bug also exists in SIP foundry version. It manifested itself when FRAME_PROCESSING_THREADS was 1, and some thread was processing flowgraphs while somebody else attempted to delete MpMediaTask - debugger showed pure virtual call error.<commit_after>//  
// Copyright (C) 2006-2007 SIPez LLC. 
// Licensed to SIPfoundry under a Contributor Agreement. 
//
// Copyright (C) 2004-2007 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// Copyright (C) 2004-2006 Pingtel Corp.  All rights reserved.
// Licensed to SIPfoundry under a Contributor Agreement.
//
// $$
///////////////////////////////////////////////////////////////////////////////


/* There used to be #ifdef's here to do the same thing on WIN32/VXWORKS, but I
* took them out because we do the same thing on every OS. -Mike */
#define PRINTF Zprintf

// SYSTEM INCLUDES
#include <assert.h>

// APPLICATION INCLUDES
#include "os/OsLock.h"
#include "os/OsMsgPool.h"
#include "os/OsCallback.h"
#include "os/OsTimer.h"
#include "utl/UtlHashBagIterator.h"
#include "utl/UtlPtr.h"
#include "mp/MpFlowGraphBase.h"
#include "mp/MpMisc.h"
#include "mp/MpMediaTask.h"
#include "mp/MpMediaTaskMsg.h"
#include "mp/MpBufferMsg.h"
#include "mp/MpCodecFactory.h"
#include "mp/MpCodec.h"
#include "mp/MpAudioDriverManager.h"
#include "mp/MpAudioDriverBase.h"

#ifdef _WIN32
#include "mp/MpMMTimer.h"
#include "mp/MpMMTimerWnt.h"
#endif

#ifdef RTL_ENABLED
#   include <rtl_macro.h>
#endif

// Include sys/time.h if _PROFILE is set.
// This #include has to be after the #include of mp/MpMediaTask.h, because
// that is where we normally set _PROFILE.
#ifdef _PROFILE /* [ */
#include <sys/time.h>
#endif /* _PROFILE ] */

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
#define TEST_TASK_LOAD

#ifdef __pingtel_on_posix__ /* [ */
#define MPMEDIA_DEF_MAX_MSGS 1000
#else
#define MPMEDIA_DEF_MAX_MSGS OsServerTask::DEF_MAX_MSGS
#endif /* __pingtel_on_posix__ ] */

// STATIC VARIABLE INITIALIZATIONS
MpMediaTask* MpMediaTask::spInstance = NULL;
OsBSem       MpMediaTask::sLock(OsBSem::Q_PRIORITY, OsBSem::FULL);
UtlBoolean MpMediaTask::ms_bTestMode = FALSE;

#ifdef _PROFILE /* [ */
   static long long sSignalTicks; // Time (in microseconds) for the current
                                  // frame start signal
   static unsigned long long sMinTicks;
   static unsigned long long sMaxTicks;
#endif /* _PROFILE ] */

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

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

// Return a pointer to the media task, creating it if necessary
MpMediaTask* MpMediaTask::getMediaTask(UtlBoolean bCreate)
{
   UtlBoolean isStarted;

   // If the task object already exists, and the corresponding low-level task
   // has been started, then use it. If we do not request creation, also return NULL
   if ((spInstance != NULL && spInstance->isStarted()) || (!bCreate))
      return spInstance;

   // If the task does not yet exist or hasn't been started, then acquire
   // the lock to ensure that only one instance of the task is started
   OsLock lock(sLock);
   if (spInstance == NULL)
       spInstance = new MpMediaTask();

   isStarted = spInstance->isStarted();
   if (!isStarted)
   {
      isStarted = spInstance->start();
      spInstance->startFrameStartTimer();
      assert(isStarted);
   }

   return spInstance;
}

// Destructor
MpMediaTask::~MpMediaTask()
{
   // $$$ need to figure out how to cleanly shut down this task after
   // $$$ unmanaging and destroying all of its flow graphs

   waitUntilShutDown();
{'fraction_non_alphanumeric': 0.16205142695676744, 'fraction_numerical': 0.009183385137044362, 'mean_word_length': 3.0777649769585254, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5432030', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2263, 'n_words': 698}