diff --git a/src/ofxWMFVideoPlayer.cpp b/src/ofxWMFVideoPlayer.cpp index 09242c6..eec22e3 100644 --- a/src/ofxWMFVideoPlayer.cpp +++ b/src/ofxWMFVideoPlayer.cpp @@ -20,7 +20,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); ofxWMFVideoPlayer* findPlayers(HWND hwnd) { - for each (PlayerItem e in g_WMFVideoPlayers) + for (PlayerItem e : g_WMFVideoPlayers) { if (e.first == hwnd) return e.second; } @@ -63,6 +63,8 @@ ofxWMFVideoPlayer::ofxWMFVideoPlayer() : _player(NULL) _wantToSetVolume = false; _currentVolume = 1.0; _frameRate = 0.0f; + _duration = 0.f; + _totalNumFrames = 0.f; } @@ -71,8 +73,8 @@ ofxWMFVideoPlayer::ofxWMFVideoPlayer() : _player(NULL) ofxWMFVideoPlayer::~ofxWMFVideoPlayer() { if (_player) { + if (_sharedTextureCreated) _player->m_pEVRPresenter->releaseSharedTexture(); _player->Shutdown(); - //if (_sharedTextureCreated) _player->m_pEVRPresenter->releaseSharedTexture(); SafeRelease(&_player); } @@ -101,6 +103,9 @@ void ofxWMFVideoPlayer::forceExit() bool ofxWMFVideoPlayer:: loadMovie(string name) { + loadEventSent = false; + bLoaded = false; + if (!_player) { ofLogError("ofxWMFVideoPlayer") << "Player not created. Can't open the movie."; return false; @@ -134,7 +139,7 @@ void ofxWMFVideoPlayer::forceExit() _width = _player->getWidth(); _height = _player->getHeight(); - _tex.allocate(_width,_height,GL_RGBA,true); + _tex.allocate(_width,_height,GL_RGB,true); _player->m_pEVRPresenter->createSharedTexture(_width, _height,_tex.texData.textureID); _sharedTextureCreated = true; @@ -149,7 +154,7 @@ void ofxWMFVideoPlayer::forceExit() _width = _player->getWidth(); _height = _player->getHeight(); - _tex.allocate(_width,_height,GL_RGBA,true); + _tex.allocate(_width,_height,GL_RGB,true); _player->m_pEVRPresenter->createSharedTexture(_width, _height,_tex.texData.textureID); } @@ -157,6 +162,11 @@ void ofxWMFVideoPlayer::forceExit() } _waitForLoadedToPlay = false; + _frameRate = _player->getFrameRate(); + _duration = _player->getDuration(); + + _totalNumFrames = _duration * _frameRate; + return false; @@ -173,18 +183,37 @@ void ofxWMFVideoPlayer::forceExit() + } + + void ofxWMFVideoPlayer::bind() { + + + _player->m_pEVRPresenter->lockSharedTexture(); + _tex.setTextureWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_BORDER); + _tex.bind(); + + + + } + void ofxWMFVideoPlayer::unbind() { + + + _tex.unbind(); + _player->m_pEVRPresenter->unlockSharedTexture(); + + + } -bool ofxWMFVideoPlayer:: isPlaying() { +bool ofxWMFVideoPlayer:: isPlaying() const { return _player->GetState() == Started; } -bool ofxWMFVideoPlayer:: isStopped() { +bool ofxWMFVideoPlayer:: isStopped() const { return (_player->GetState() == Stopped || _player->GetState() == Paused); } -bool ofxWMFVideoPlayer:: isPaused() -{ +bool ofxWMFVideoPlayer:: isPaused() const { return _player->GetState() == Paused; } @@ -208,14 +237,45 @@ void ofxWMFVideoPlayer:: update() { if ((_wantToSetVolume)) { - _player->setVolume(_currentVolume); + setVolume(_currentVolume); } return; } +bool ofxWMFVideoPlayer::getIsMovieDone() const +{ + int currentFrame = getCurrentFrame(); + int finalFrame = _totalNumFrames - (_frameRate / 5.f); + return (currentFrame >= finalFrame); +} + +bool ofxWMFVideoPlayer::isLoaded() const { + if(_player == NULL){ return false; } + PlayerState ps = _player->GetState(); + return ps == PlayerState::Paused || ps == PlayerState::Stopped || ps == PlayerState::Started; +} +bool ofxWMFVideoPlayer::setPixelFormat(ofPixelFormat pixelFormat){ + return (pixelFormat == OF_PIXELS_RGB); +} + +ofPixelFormat ofxWMFVideoPlayer::getPixelFormat() const { + return OF_PIXELS_RGB; +} + +bool ofxWMFVideoPlayer::isFrameNew() const { + return true;//TODO fix this +} + +void ofxWMFVideoPlayer::setPaused( bool bPause ) +{ + if ( bPause == true ) + pause() ; + else + play() ; +} void ofxWMFVideoPlayer:: play() { @@ -235,18 +295,40 @@ void ofxWMFVideoPlayer:: pause() _player->Pause(); } +void ofxWMFVideoPlayer::setLoopState( ofLoopType loopType ) +{ + switch ( loopType ) + { + case OF_LOOP_NONE : + setLoop( false ) ; + break ; + case OF_LOOP_NORMAL : + setLoop( true ) ; + break; + default : + ofLogError ( "ofxWMFVideoPlayer::setLoopState LOOP TYPE NOT SUPPORTED" ) << loopType << endl ; + break ; + } +} + +float ofxWMFVideoPlayer::getPosition() const +{ + return ( _player->getPosition() / getDuration() ); + //this returns it in seconds + // return _player->getPosition(); +} -float ofxWMFVideoPlayer:: getPosition() { - return _player->getPosition(); +int ofxWMFVideoPlayer::getCurrentFrame() const { + return getPosition() * _totalNumFrames; } -float ofxWMFVideoPlayer:: getDuration() { - return _player->getDuration(); +float ofxWMFVideoPlayer::getDuration() const { + return _duration; } void ofxWMFVideoPlayer::setPosition(float pos) { - _player->setPosition(pos); + _player->setPosition(pos * getDuration()); } void ofxWMFVideoPlayer::setVolume(float vol) @@ -262,20 +344,24 @@ void ofxWMFVideoPlayer::setVolume(float vol) } -float ofxWMFVideoPlayer::getVolume() +float ofxWMFVideoPlayer::getVolume() const { return _player->getVolume(); } -float ofxWMFVideoPlayer::getFrameRate() +float ofxWMFVideoPlayer::getFrameRate() const { - if (!_player) return 0.0f; - if (_frameRate == 0.0f) _frameRate = _player->getFrameRate(); return _frameRate; } -float ofxWMFVideoPlayer::getHeight() { return _player->getHeight(); } -float ofxWMFVideoPlayer::getWidth() { return _player->getWidth(); } + +int ofxWMFVideoPlayer::getTotalNumFrames() const +{ + return _totalNumFrames; +} + +float ofxWMFVideoPlayer::getHeight() const { return _player->getHeight(); } +float ofxWMFVideoPlayer::getWidth() const { return _player->getWidth(); } void ofxWMFVideoPlayer::setLoop(bool isLooping) { _isLooping = isLooping; _player->setLooping(isLooping); } @@ -286,13 +372,30 @@ void ofxWMFVideoPlayer::setLoop(bool isLooping) { _isLooping = isLooping; _play //----------------------------------- - // Handler for Media Session events. void ofxWMFVideoPlayer::OnPlayerEvent(HWND hwnd, WPARAM pUnkPtr) { HRESULT hr = _player->HandleEvent(pUnkPtr); + PlayerState state; + + if (_player->GetState() == 3) + { + + if (!loadEventSent){ + bLoaded = true; + ofNotifyEvent(videoLoadEvent,bLoaded,this); + loadEventSent = true; + } + } + if (FAILED(hr)) { + if (!loadEventSent){ + bLoaded = false; + ofNotifyEvent(videoLoadEvent,bLoaded,this); + loadEventSent = true; + } + ofLogError("ofxWMFVideoPlayer", "An error occurred."); } } diff --git a/src/ofxWMFVideoPlayer.h b/src/ofxWMFVideoPlayer.h index 5bc33a6..c375f7b 100644 --- a/src/ofxWMFVideoPlayer.h +++ b/src/ofxWMFVideoPlayer.h @@ -7,27 +7,28 @@ #include "ofMain.h" + #include "ofxWMFVideoPlayerUtils.h" #include "EVRPresenter.h" -class ofxWMFVideoPlayer; +//class ofxWMFVideoPlayer; class CPlayer; -class ofxWMFVideoPlayer { +class ofxWMFVideoPlayer : public ofBaseVideoPlayer { private: + static int _instanceCount; - - + HWND _hwndPlayer; - + BOOL bRepaintClient; - - + + int _width; int _height; @@ -38,70 +39,94 @@ class ofxWMFVideoPlayer { float _currentVolume; bool _sharedTextureCreated; - + ofTexture _tex; - + ofPixels _pixels; + BOOL InitInstance(); - - void OnPlayerEvent(HWND hwnd, WPARAM pUnkPtr); - float _frameRate; + void OnPlayerEvent(HWND hwnd, WPARAM pUnkPtr); + + bool loadEventSent; + bool bLoaded; + + + float _frameRate; + float _duration; + int _totalNumFrames; +public: - public: CPlayer* _player; int _id; - ofxWMFVideoPlayer(); ~ofxWMFVideoPlayer(); - bool loadMovie(string name); - //bool loadMovie(string name_left, string name_right) ; - void close(); - void update(); - - void play(); - void stop(); - void pause(); + bool load(string name) { return loadMovie(name); } + bool loadMovie(string name); + + void close(); + void update(); + + void play(); + void stop(); + void pause(); + void setPaused( bool bPause ) ; + + float getPosition() const; + float getDuration() const; + float getFrameRate() const; + int getCurrentFrame() const; + int getTotalNumFrames() const; - float getPosition(); - float getDuration(); - float getFrameRate(); + void setPosition(float pos); - void setPosition(float pos); + void setVolume(float vol); + float getVolume() const; - void setVolume(float vol); - float getVolume(); + float getHeight() const; + float getWidth() const; - float getHeight(); - float getWidth(); + bool isPlaying() const; + bool isStopped() const; + bool isPaused() const; - bool isPlaying(); - bool isStopped(); - bool isPaused(); + bool isLoaded() const; - void setLoop(bool isLooping); - bool isLooping() { return _isLooping; } + void setLoop(bool isLooping); + bool isLooping() { return _isLooping; } + void bind(); + void unbind(); + ofEvent videoLoadEvent; - + void setLoopState( ofLoopType loopType ) ; + bool getIsMovieDone() const; + ofPixels& getPixelsRef() { return _pixels; } + ofPixels& getPixels() { return _pixels; } + ofTexture& getTexture(){ return _tex; }; + bool setPixelFormat(ofPixelFormat pixelFormat); + ofPixelFormat getPixelFormat() const; + const ofTexture & getTexture() const { return _tex; }; + const ofPixels & getPixels() const { return _pixels; }; - void draw(int x, int y , int w, int h); - void draw(int x, int y) { draw(x,y,getWidth(),getHeight()); } + bool isFrameNew() const; + void draw(int x, int y , int w, int h); + void draw(int x, int y) { draw(x,y,getWidth(),getHeight()); } - HWND getHandle() { return _hwndPlayer;} - LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); + HWND getHandle() { return _hwndPlayer;} + LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); - static void forceExit(); + static void forceExit(); -}; \ No newline at end of file +}; diff --git a/src/ofxWMFVideoPlayerUtils.cpp b/src/ofxWMFVideoPlayerUtils.cpp index d7b63ec..652d71f 100644 --- a/src/ofxWMFVideoPlayerUtils.cpp +++ b/src/ofxWMFVideoPlayerUtils.cpp @@ -638,7 +638,7 @@ HRESULT CPlayer::HandleEvent(UINT_PTR pEventPtr) GetEventObject (pEvent,&topology); WORD nodeCount; topology->GetNodeCount(&nodeCount); - cout << "Topo set and we have " << nodeCount << " nodes" << endl; + ofLog(ofLogLevel::OF_LOG_VERBOSE, "Video topography set with %d nodes", nodeCount); SafeRelease(&topology); break; @@ -1412,7 +1412,7 @@ float CPlayer::getPosition() { MFTIME longPosition = 0; hr = pClock->GetTime(&longPosition); if (SUCCEEDED(hr)) - position = (float)longPosition / 10000000.0; + position = float(longPosition / 10000000.0); } SafeRelease(&pClock); return position; diff --git a/src/presenter/Presenter.cpp b/src/presenter/Presenter.cpp index a060e3d..3c916ae 100644 --- a/src/presenter/Presenter.cpp +++ b/src/presenter/Presenter.cpp @@ -128,12 +128,12 @@ HRESULT EVRCustomPresenter::QueryInterface(REFIID riid, void ** ppv) ULONG EVRCustomPresenter::AddRef() { - return RefCountedObject::AddRef(); + return RefCountedObject_::AddRef(); } ULONG EVRCustomPresenter::Release() { - return RefCountedObject::Release(); + return RefCountedObject_::Release(); } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/presenter/Presenter.h b/src/presenter/Presenter.h index 09bcab2..ec471ad 100644 --- a/src/presenter/Presenter.h +++ b/src/presenter/Presenter.h @@ -70,7 +70,7 @@ enum FRAMESTEP_STATE class EVRCustomPresenter : BaseObject, - RefCountedObject, + RefCountedObject_, // COM interfaces: public IMFVideoDeviceID, public IMFVideoPresenter, // Inherits IMFClockStateSink diff --git a/src/presenter/common/ClassFactory.h b/src/presenter/common/ClassFactory.h index ef9f728..61095c4 100644 --- a/src/presenter/common/ClassFactory.h +++ b/src/presenter/common/ClassFactory.h @@ -206,14 +206,14 @@ namespace MediaFoundationSamples // RefCountedObject // You can use this when implementing IUnknown or any object that uses reference counting. - class RefCountedObject + class RefCountedObject_ { protected: volatile long m_refCount; public: - RefCountedObject() : m_refCount(1) {} - virtual ~RefCountedObject() + RefCountedObject_() : m_refCount(1) {} + virtual ~RefCountedObject_() { assert(m_refCount == 0); }