Advertisement
Guest User

Threaded CinderWMF

a guest
May 25th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include "cinder/app/App.h"
  2. #include "cinder/app/RendererGl.h"
  3. #include "cinder/gl/gl.h"
  4.  
  5. #include "ciWMFVideoPlayer.h"
  6.  
  7. #include "cinder/Filesystem.h"
  8.  
  9. using namespace ci;
  10. using namespace ci::app;
  11. using namespace std;
  12.  
  13. class CinderVideoApp : public App {
  14.   public:
  15.     void setup() override;
  16.     void mouseDown( MouseEvent event ) override;
  17.     void update() override;
  18.     void draw() override;
  19.  
  20. private:
  21.  
  22.     void setupBlockingCreationAndLoadingOfWMFPlayer( ci::fs::path const& pathToVideo );
  23.     void setupAsyncCreationAndLoadingofWMFPlayer( ci::fs::path const& pathToVideo );
  24.     void setupAsyncLoadingofWMFPlayer( ci::fs::path const& pathToVideo );
  25.  
  26.     std::future<std::unique_ptr<ciWMFVideoPlayer>>  asyncCreationAndLoadingOfWMFPlayer();
  27.     std::future<void>                               asyncLoadingOfWMFPlayer();
  28.  
  29.     std::unique_ptr<ciWMFVideoPlayer>   createWMFPlayerAndLoadVideo( gl::ContextRef ctx, ci::fs::path const& pathToVideo );
  30.     void                                loadVideo( ciWMFVideoPlayer &wmfPlayer, ci::fs::path const& pathToVideo );
  31.  
  32.     std::future<std::unique_ptr<ciWMFVideoPlayer>>  mAsyncCreatedWMFVideoPlayer;
  33.     std::future<void>                               mAsyncLoadedWMFPlayer;
  34.     std::unique_ptr<ciWMFVideoPlayer> mWMFPlayer;
  35.  
  36.     bool mWMFPlayerReady = false;
  37. };
  38.  
  39. void CinderVideoApp::setup()
  40. {
  41.     ci::fs::path videoFile = L"D:\\vjMovs\\output\\Original - Bad Audio Sync - Donald Duck's Playground-1.mp4";
  42.  
  43.     // Synchronous (Blocking) creation and loading of WMFPlayer
  44.     //setupBlockingCreationAndLoadingOfWMFPlayer( videoFile );
  45.  
  46.     // Asynchronous (Non-blocking) creation and loading of WMFPlayer
  47.     setupAsyncCreationAndLoadingofWMFPlayer( videoFile );
  48.  
  49.     // Synchronous creation, and asynchronous loading of WMFPlayer
  50.     //setupAsyncLoadingofWMFPlayer( videoFile );
  51. }
  52.  
  53. void CinderVideoApp::setupBlockingCreationAndLoadingOfWMFPlayer( ci::fs::path const& pathToVideo )
  54. {
  55.     mWMFPlayer = std::make_unique<ciWMFVideoPlayer>();
  56.     mWMFPlayer->loadMovie( pathToVideo );
  57.     mWMFPlayer->play();
  58.     mWMFPlayerReady = true;
  59. }
  60.  
  61. void CinderVideoApp::setupAsyncCreationAndLoadingofWMFPlayer( ci::fs::path const& pathToVideo )
  62. {
  63.     // We theorize that the problem is one of context between threads - we must share it... Let's try that...
  64.     auto ctx = gl::env()->createSharedContext( gl::context() );
  65.     if ( !ctx )
  66.         assert(0);
  67.  
  68.     mAsyncCreatedWMFVideoPlayer = std::async( std::launch::async, &CinderVideoApp::createWMFPlayerAndLoadVideo, this, ctx, pathToVideo );
  69. }
  70.  
  71. void CinderVideoApp::setupAsyncLoadingofWMFPlayer( ci::fs::path const& pathToVideo )
  72. {
  73.     mWMFPlayer = std::make_unique<ciWMFVideoPlayer>();
  74.     mAsyncLoadedWMFPlayer = std::async( std::launch::async, &CinderVideoApp::loadVideo, this, *mWMFPlayer, pathToVideo );
  75. }
  76.  
  77. std::unique_ptr<ciWMFVideoPlayer> CinderVideoApp::createWMFPlayerAndLoadVideo( gl::ContextRef ctx, ci::fs::path const& pathToVideo )
  78. {
  79.     // This only works if we can make the render context current.
  80.     ctx->makeCurrent();
  81.  
  82.     auto wmfPlayer = std::make_unique<ciWMFVideoPlayer>();
  83.     wmfPlayer->loadMovie( pathToVideo );
  84.     //wmfPlayer->play();
  85.  
  86.     return std::move( wmfPlayer );
  87. }
  88.  
  89. void CinderVideoApp::loadVideo( ciWMFVideoPlayer &wmfPlayer, ci::fs::path const& pathToVideo )
  90. {
  91.     wmfPlayer.loadMovie( pathToVideo );
  92. }
  93.  
  94. void CinderVideoApp::mouseDown( MouseEvent event )
  95. {
  96. }
  97.  
  98. void CinderVideoApp::update()
  99. {
  100.     if ( mAsyncCreatedWMFVideoPlayer.valid() )
  101.     {
  102.         if ( mAsyncCreatedWMFVideoPlayer.wait_for( std::chrono::seconds( 0 ) ) == std::future_status::ready )
  103.         {
  104.             mWMFPlayer = mAsyncCreatedWMFVideoPlayer.get();
  105.             mWMFPlayer->play();
  106.             mWMFPlayerReady = true;
  107.         }
  108.     }
  109.  
  110.     if ( mAsyncLoadedWMFPlayer.valid() )
  111.     {
  112.         if ( mAsyncLoadedWMFPlayer.wait_for( std::chrono::seconds( 0 ) ) == std::future_status::ready )
  113.         {
  114.             mWMFPlayerReady = true;
  115.         }
  116.     }
  117.  
  118.     if ( mWMFPlayerReady )
  119.         mWMFPlayer->update();
  120. }
  121.  
  122. void CinderVideoApp::draw()
  123. {
  124.     gl::clear( Color( 0, 0, 0 ) );
  125.  
  126.     if (mWMFPlayerReady)
  127.         mWMFPlayer->draw(10, 10);
  128. }
  129.  
  130. CINDER_APP( CinderVideoApp, RendererGl )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement