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

// Author: <NAME> <<EMAIL> AT SIP<EMAIL>>

// SYSTEM INCLUDES
#include <assert.h>

// APPLICATION INCLUDES
#include <mp/MpInputDeviceManager.h>
#include <mp/MprFromInputDevice.h>

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS


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

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

// Constructor
MprFromInputDevice::MprFromInputDevice(const UtlString& rName, 
                                      int samplesPerFrame, 
                                      int samplesPerSec,
                                      MpInputDeviceManager* deviceManager,
                                      int deviceId)
: MpAudioResource(rName,
                  1, 1, /* inputs */
                  0, 0, /* outputs */
                  samplesPerFrame, samplesPerSec)
, mpInputDeviceManager(deviceManager)
, mFrameTimeInitialized(FALSE)
, mPreviousFrameTime(0)
, mDeviceId(deviceId)
{
}

// Destructor
MprFromInputDevice::~MprFromInputDevice()
{
}

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

/* ============================ ACCESSORS ================================= */

/* ============================ INQUIRY =================================== */

/* //////////////////////////// PROTECTED ///////////////////////////////// */

/* //////////////////////////// PRIVATE /////////////////////////////////// */

UtlBoolean MprFromInputDevice::doProcessFrame(MpBufPtr inBufs[],
                                             MpBufPtr outBufs[],
                                             int inBufsSize,
                                             int outBufsSize,
                                             UtlBoolean isEnabled,
                                             int samplesPerFrame,
                                             int samplesPerSecond)
{
   // NOTE: Logic to react to frequent starvation is missing.

   UtlBoolean bufferOutput = FALSE;
   assert(mpInputDeviceManager);

   // Milliseconds per frame:
   int frameTimeInterval = samplesPerFrame * 1000 / samplesPerSecond;

   if (!mFrameTimeInitialized)
   {
      // Start with a frame behind.  Possible need smarter
      // decision for starting.
      mPreviousFrameTime = mpInputDeviceManager->getCurrentFrameTime();
      mPreviousFrameTime -= (2 * frameTimeInterval);
   }

   mPreviousFrameTime += frameTimeInterval;

   MpBufPtr buffer;
   unsigned int numFramesNotPlayed;
   unsigned int numFramedBufferedBehind;
   OsStatus getResult = mpInputDeviceManager->getFrame(mDeviceId,
                                                       mPreviousFrameTime,
                                                       buffer,
                                                       numFramesNotPlayed,
                                                       numFramedBufferedBehind);


   if (!mFrameTimeInitialized)
   {
      if (getResult == OS_SUCCESS)
      {
         mFrameTimeInitialized = TRUE;
      }

      if (numFramesNotPlayed > 1)
      {
         // TODO: now is a good time to adjust and get a newer
         // frame
         // could increment mPreviousFrameTime and getFrame again
      }
   }

   if (buffer.isValid())
   {
      outBufs[0] = buffer;
      bufferOutput = TRUE;
   }

   return(bufferOutput);
}

/* ============================ FUNCTIONS ================================= */
<commit_msg>MprFromInputDevice::doProcessFrame() should return result of mpInputDeviceManager->getFrame() call and not buffer.isValid(). Invalid input bufer does not indicate error - it is just empty input, while failed getFrame is a real error.<commit_after>//  
// Copyright (C) 2007 SIPez LLC. 
// Licensed to SIPfoundry under a Contributor Agreement. 
//
// Copyright (C) 2007 SIPfoundry Inc.
// Licensed by SIPfoundry under the LGPL license.
//
// $$
///////////////////////////////////////////////////////////////////////////////

// Author: <NAME> <<EMAIL> AT SIPez DOT com>

// SYSTEM INCLUDES
#include <assert.h>

// APPLICATION INCLUDES
#include <mp/MpInputDeviceManager.h>
#include <mp/MprFromInputDevice.h>

// EXTERNAL FUNCTIONS
// EXTERNAL VARIABLES
// CONSTANTS
// STATIC VARIABLE INITIALIZATIONS


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

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

// Constructor
MprFromInputDevice::MprFromInputDevice(const UtlString& rName, 
                                      int samplesPerFrame, 
                                      int samplesPerSec,
                                      MpInputDeviceManager* deviceManager,
                                      int deviceId)
: MpAudioResource(rName,
                  1, 1, /* inputs */
                  0, 0, /* outputs */
                  samplesPerFrame, samplesPerSec)
, mpInputDeviceManager(deviceManager)
, mFrameTimeInitialized(FALSE)
, mPreviousFrameTime(0)
, mDeviceId(deviceId)
{
}

// Destructor
MprFromInputDevice::~MprFromInputDevice()
{
}

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

/* ============================ ACCESSORS ================================= */

/* ============================ INQUIRY =================================== */

/* //////////////////////////// PROTECTED ///////////////////////////////// */

/* //////////////////////////// PRIVATE /////////////////////////////////// */

UtlBoolean MprFromInputDevice::doProcessFrame(MpBufPtr inBufs[],
                                             MpBufPtr outBufs[],
                                             int inBufsSize,
                                             int outBufsSize,
                                             UtlBoolean isEnabled,
                                             int samplesPerFrame,
                                             int samplesPerSecond)
{
   // NOTE: Logic to react to frequent starvation is missing.

   assert(mpInputDeviceManager);

   // Milliseconds per frame:
   int frameTimeInterval = samplesPerFrame * 1000 / samplesPerSecond;

   if (!mFrameTimeInitialized)
   {
      // Start with a frame behind.  Possible need smarter
      // decision for starting.
      mPreviousFrameTime = mpInputDeviceManager->getCurrentFrameTime();
      mPreviousFrameTime -= (2 * frameTimeInterval);
   }

   mPreviousFrameTime += frameTimeInterval;

   MpBufPtr buffer;
   unsigned int numFramesNotPlayed;
   unsigned int numFramedBufferedBehind;
   OsStatus getResult = mpInputDeviceManager->getFrame(mDeviceId,
                                                       mPreviousFrameTime,
                                                       buffer,
                                                       numFramesNotPlayed,
                                                       numFramedBufferedBehind);


   if (!mFrameTimeInitialized)
   {
      if (getResult == OS_SUCCESS)
      {
         mFrameTimeInitialized = TRUE;
      }

      if (numFramesNotPlayed > 1)
      {
         // TODO: now is a good time to adjust and get a newer
         // frame
         // could increment mPreviousFrameTime and getFrame again
      }
   }

   outBufs[0] = buffer;

   return getResult;
}

/* ============================ FUNCTIONS ================================= */

{'fraction_non_alphanumeric': 0.21775456919060052, 'fraction_numerical': 0.005221932114882507, 'mean_word_length': 1.4913821138211383, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '401690', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1717, 'n_words': 404}