AtmoLiveView.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:8k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*
  2.  * AtmoLiveView.cpp:  this effect outputs colors as result of a picture
  3.  * content (most complex effect) see thread.c of the linux VDR version -
  4.  * to fully understand what happens here..
  5.  *
  6.  * See the README.txt file for copyright information and how to reach the author(s).
  7.  *
  8.  * $Id: 4ce5ba02e59bad3e257bd7dc04e29b994d196905 $
  9.  */
  10. #include "AtmoDefs.h"
  11. #include "AtmoLiveView.h"
  12. #include "AtmoOutputFilter.h"
  13. #include "AtmoTools.h"
  14. #if defined(_ATMO_VLC_PLUGIN_)
  15. # include <vlc_common.h>
  16. #else
  17. #  include "AtmoGdiDisplayCaptureInput.h"
  18. #endif
  19. #include "AtmoExternalCaptureInput.h"
  20. #if defined(_ATMO_VLC_PLUGIN_)
  21. CAtmoLiveView::CAtmoLiveView(CAtmoDynData *pAtmoDynData) :
  22.                CThread(pAtmoDynData->getAtmoFilter())
  23. {
  24.     this->m_pAtmoDynData    = pAtmoDynData;
  25.     m_pAtmoInput = NULL;
  26. }
  27. #else
  28. CAtmoLiveView::CAtmoLiveView(CAtmoDynData *pAtmoDynData)
  29. {
  30.     this->m_pAtmoDynData  = pAtmoDynData;
  31.     m_LiveViewSource = lvsGDI;
  32.     m_CurrentLiveViewSource = lvsGDI;
  33.     m_InputChangedEvent = CreateEvent(NULL,ATMO_FALSE,ATMO_FALSE,NULL);
  34.     m_pAtmoInput = NULL;
  35.     InitializeCriticalSection(&m_InputChangeCriticalSection);
  36. }
  37. #endif
  38. CAtmoLiveView::~CAtmoLiveView(void)
  39. {
  40. #if !defined(_ATMO_VLC_PLUGIN_)
  41.    DeleteCriticalSection(&m_InputChangeCriticalSection);
  42.    CloseHandle(m_InputChangedEvent);
  43. #endif
  44. }
  45. #if !defined(_ATMO_VLC_PLUGIN_)
  46. STDMETHODIMP CAtmoLiveView::setLiveViewSource(enum ComLiveViewSource dwModus)
  47. {
  48.     if(dwModus != m_LiveViewSource) {
  49.        m_LiveViewSource = dwModus;
  50.        /*
  51.          you may ask why I don't use a critical section here and directly acces the
  52.          the variable of the Thread?
  53.          Just because you would need very much / often entering / leaving the critical
  54.          section ... and in this case It could be avoid ...
  55.          assigning the value to the "mirror" variable m_LiveViewSource which is compare
  56.          in every run of the thread with its current value ... if there is a change
  57.          the thread can proceed switching the live source ... until this is done
  58.          the thread calling this method is waiting...
  59.        */
  60.        // I don't expect that it will take longer than 500ms to switch...
  61.        if(WaitForSingleObject(m_InputChangedEvent,500) == WAIT_TIMEOUT)
  62.           return S_FALSE; // if not so the switch seems be have failed (badly)
  63.     }
  64.     return S_OK;
  65. }
  66. STDMETHODIMP CAtmoLiveView::getCurrentLiveViewSource(enum ComLiveViewSource *modus) {
  67.      *modus = m_LiveViewSource;
  68.      return S_OK;
  69. }
  70. #endif
  71. DWORD CAtmoLiveView::Execute(void)
  72. {
  73. #if defined(_ATMO_VLC_PLUGIN_)
  74.       mtime_t ticks;
  75. #else
  76.       DWORD ticks;
  77. #endif
  78.       int i_frame_counter = 0;
  79.       CAtmoInput *newInput,*oldInput;
  80.       tColorPacket ColorPacket;
  81.       CAtmoConnection *pAtmoConnection = this->m_pAtmoDynData->getAtmoConnection();
  82.       if((pAtmoConnection == NULL) || (pAtmoConnection->isOpen() == ATMO_FALSE)) return 0;
  83.       CAtmoConfig *pAtmoConfig = this->m_pAtmoDynData->getAtmoConfig();
  84.       /*
  85.          this object does post processing of the pixel data
  86.          like jump /scenechange detection fading over the colors
  87.       */
  88.       CAtmoOutputFilter *filter = new CAtmoOutputFilter(this->m_pAtmoDynData->getAtmoConfig());
  89. #if defined(_ATMO_VLC_PLUGIN_)
  90.       /* this thread is the data preprocess which gets the real 64x48 pixel
  91.          and converts them into the RGB channel values - this is done in
  92.          another thread to keep this thread at a constant timing - to that
  93.          color output is updated 25 times a second
  94.       */
  95.       m_pAtmoInput = new CAtmoExternalCaptureInput(m_pAtmoDynData);
  96. #else
  97.       if(m_LiveViewSource == lvsGDI)
  98.          m_pAtmoInput = new CAtmoGdiDisplayCaptureInput(m_pAtmoDynData);
  99.       else
  100.          m_pAtmoInput = new CAtmoExternalCaptureInput(m_pAtmoDynData);
  101. #endif
  102.       if(m_pAtmoInput->Open() == ATMO_TRUE)
  103.       {
  104.           /*
  105.             wait for the first frame to go in sync with the other thread
  106.           */
  107. #if defined(_ATMO_VLC_PLUGIN_)
  108.           msg_Dbg( m_pAtmoThread, "CAtmoLiveView::Execute(void)");
  109. #endif
  110.           m_pAtmoInput->WaitForNextFrame(500);
  111.           while(this->m_bTerminated == ATMO_FALSE)
  112.           {
  113.               /*  atmoInput - capture Thread Running... */
  114. #if defined(_ATMO_VLC_PLUGIN_)
  115.                 ticks = mdate();
  116. #else
  117.                 ticks = GetTickCount();
  118. #endif
  119.                 /* grab current Packet from Input! */
  120.                 ColorPacket = m_pAtmoInput->GetColorPacket();
  121.                 /* pass it through the outputfilters! */
  122.                 ColorPacket = filter->Filtering(ColorPacket);
  123.                 /* apply gamma later ;-) not implemented yet */
  124.                 ColorPacket = CAtmoTools::ApplyGamma(pAtmoConfig, ColorPacket);
  125.                 /*
  126.                    apply white calibration - only if it is not
  127.                    done by the hardware
  128.                  */
  129.                 if(pAtmoConfig->isUseSoftwareWhiteAdj())
  130.                    ColorPacket = CAtmoTools::WhiteCalibration(pAtmoConfig,
  131.                                                               ColorPacket);
  132.                 /* send color data to the the hardware... */
  133.                 pAtmoConnection->SendData(ColorPacket);
  134.                 /*
  135.                    experimental do sync every 100 Frames to the image producer
  136.                    thread because GetTickCount precision is really poor ;-)
  137.                 */
  138.                 i_frame_counter++;
  139.                 if(i_frame_counter == 100) {
  140.                    m_pAtmoInput->WaitForNextFrame(50);
  141.                    i_frame_counter = 0;
  142. #if !defined(WIN32)
  143. /* kludge for pthreads? when running GDB debugger using the same condition variable
  144.    to often results in haging wait timedout...
  145. */
  146. #ifdef _ATMO_KLUDGE_
  147.                    vlc_mutex_lock( &m_TerminateLock );
  148.                    vlc_cond_destroy( &m_TerminateCond );
  149.                    vlc_cond_init( &m_TerminateCond );
  150.                    vlc_mutex_unlock( &m_TerminateLock );
  151. #endif
  152. #endif
  153.                    continue;
  154.                 }
  155. #if !defined(_ATMO_VLC_PLUGIN_)
  156.                 /*
  157.                   Check if Input Source has changed - through an async
  158.                   call from the com interface?
  159.                 */
  160.                 if(m_CurrentLiveViewSource != m_LiveViewSource) {
  161.                    oldInput = m_pAtmoInput;
  162.                    m_pAtmoInput = NULL;
  163.                    if(m_LiveViewSource == lvsGDI) {
  164.                       // create new GDI Input Source...
  165.                       newInput = new CAtmoGdiDisplayCaptureInput(m_pAtmoDynData);
  166.                       newInput->Open(); // should not fail now... hope is the best!
  167.                    } else if(m_LiveViewSource == lvsExternal) {
  168.                       newInput = new CAtmoExternalCaptureInput(m_pAtmoDynData);
  169.                       newInput->Open();
  170.                    }
  171.                    m_CurrentLiveViewSource = m_LiveViewSource;
  172.                    m_pAtmoInput = newInput;
  173.                    oldInput->Close();
  174.                    delete oldInput;
  175.                    /*
  176.                      signal the call to the method "setLiveViewSource" the source
  177.                      was switched...
  178.                    */
  179.                    SetEvent(m_InputChangedEvent);
  180.                    // do sync with input thread
  181.                    m_pAtmoInput->WaitForNextFrame(100);
  182.                    continue;
  183.                 }
  184. #endif
  185.                 /*
  186.                    calculate RunTime of thread abbove (doesn't work well - so
  187.                    this threads comes out of sync with Image producer and the
  188.                    framerate (25fps) drifts away
  189.                */
  190. #if defined(_ATMO_VLC_PLUGIN_)
  191.                 ticks = ((mdate() - ticks) + 999)/1000;
  192. #else
  193.                 ticks = GetTickCount() - ticks;
  194. #endif
  195.                 if(ticks < 40)
  196.                 {
  197.                     // ThreadSleep -> AtmoThread.cpp
  198.                     if(this->ThreadSleep(40 - ticks)==ATMO_FALSE)
  199.                       break;
  200.                 }
  201.           }
  202.           /* shutdown the input processor thread */
  203.           m_pAtmoInput->Close();
  204.       }
  205.       delete m_pAtmoInput;
  206.       m_pAtmoInput = NULL;
  207. #if !defined(_ATMO_VLC_PLUGIN_)
  208.       /*
  209.         if there is a pending call to setLiveViewSource let him surely return before
  210.         destroying the thread and this class instance...
  211.       */
  212.       SetEvent(m_InputChangedEvent);
  213. #endif
  214.       delete filter;
  215.       return 0;
  216. }