From f9d51e6880b14cae6e7f519104a6c2123ab30da4 Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 20 Oct 2014 19:10:08 +0000 Subject: [PATCH] More cleanups and documentation in media related stuff. --- MediaImpl.cpp | 94 +++++++++++++++++++++------------------------------ MediaImpl.h | 30 ++++++++-------- Paint.cpp | 11 ++++-- Paint.h | 28 +++++++-------- 4 files changed, 76 insertions(+), 87 deletions(-) diff --git a/MediaImpl.cpp b/MediaImpl.cpp index 5cadd3c..a989ddf 100644 --- a/MediaImpl.cpp +++ b/MediaImpl.cpp @@ -52,13 +52,16 @@ int MediaImpl::getHeight() const return _padHandlerData.height; } -const uchar* MediaImpl::getBits() const +const uchar* MediaImpl::getBits() { + // Reset bits changed. + _bitsChanged = false; + + // Return data. if (_currentFrameSample == NULL) return NULL; - - - return _data; + else + return _data; } QString MediaImpl::getUri() const @@ -94,18 +97,6 @@ MediaImpl::~MediaImpl() delete _mutexLocker; } -bool MediaImpl::_videoPull() -{ - GstSample *sample = _currentFrameSample; - - if (sample == NULL) - { - // Either means we are not playing or we have reached EOS. - return false; - } - else return true; -} - bool MediaImpl::_eos() const { if (_movieReady) @@ -157,6 +148,9 @@ GstFlowReturn MediaImpl::gstNewSampleCallback(GstElement*, MediaImpl *p) // Retrieve data from map info. p->_data = map.data; + + // Bits have changed. + p->_bitsChanged = true; } p->unlockMutex(); @@ -175,6 +169,7 @@ _videoconvert0(NULL), _appsink0(NULL), _currentFrameSample(NULL), _currentFrameBuffer(NULL), +_bitsChanged(false), _width(640), // unused _height(480), // unused _data(NULL), @@ -221,24 +216,29 @@ void MediaImpl::freeResources() _pipeline = NULL; } - qDebug() << "Freeing remaining samples/buffers" << endl; - - _freeCurrentSample(); - + // Reset pipeline elements. _uridecodebin0 = NULL; _queue0 = NULL; _videoconvert0 = NULL; - //_audioSink = NULL; _appsink0 = NULL; - _currentFrameSample = NULL; + + // Reset pad handler. _padHandlerData = GstPadHandlerData(); - - // unref the shmsrc poller + + // Unref the shmsrc poller. if (_pollSource) { g_source_unref(_pollSource); _pollSource = NULL; } + + qDebug() << "Freeing remaining samples/buffers" << endl; + + // Frees current sample and buffer. + _freeCurrentSample(); + + // Resets bits changed. + _bitsChanged = false; } void MediaImpl::resetMovie() @@ -476,43 +476,27 @@ bool MediaImpl::loadMovie(QString filename) return true; } -bool MediaImpl::runVideo() +void MediaImpl::update() { - if (! _preRun()) + // Check for end-of-stream or terminate. + if (_eos() || _terminate) { - return false; + _setFinished(true); + resetMovie(); } - - bool bitsChanged = false; - - // Check if we have some frames in the input buffer. - if (_currentFrameSample != NULL) - { - // Pull video. - if (_videoPull()) - { - bitsChanged = true; - } - - //std::cout << "VideoImpl::runVideo: read frame #" << _videoNewBufferCounter << std::endl; - } - - /* TODO: This causes the texture to be loaded always in Mapper.cpp . The - * problem if this is not set is: When we have more than one shape, a - * shape that has a new buffer coming in will overdraw the old buffer of the - * shape on top. This implementation seems to be fast enough that - * _videoNewBufferCounter is often 1 or 0. If bitsChanged is often switching - * between true and false (as in the case described above), than the shape - * textures will appear to be flickering/alternating. Maybe a better solution is - * needed (in the GL layer or here?)*/ else { - bitsChanged = true; + _setFinished(false); } - _postRun(); - - return bitsChanged; +// // Check if movie is ready and connected. +// if (!isReady()) +// { +// _bitsChanged = false; +// } +// + // Check gstreamer messages on bus. + _checkMessages(); } bool MediaImpl::setPlayState(bool play) @@ -557,7 +541,7 @@ bool MediaImpl::_preRun() return true; } -void MediaImpl::_postRun() +void MediaImpl::_checkMessages() { // Parse message. if (_bus != NULL) diff --git a/MediaImpl.h b/MediaImpl.h index 93cb336..4f6f098 100644 --- a/MediaImpl.h +++ b/MediaImpl.h @@ -60,33 +60,42 @@ public: * Returns whether or not GStreamer video support is ok. */ static bool hasVideoSupport(); + /** * Sets up the player. * Basically calls loadMovie(). */ void build(); + /** * Returns the width of the video image. */ int getWidth() const; + /** * Returns the height of the video image. */ int getHeight() const; + /** * Returns the path to the media file being played. */ QString getUri() const; + /** * When using the shared memory source, returns whether or not we * are attached to a shared memory socket. */ bool getAttached(); + /** * Returns the raw image of the last video frame. * It is currently unused! */ - const uchar* getBits() const; + const uchar* getBits(); + + /// Returns true iff bits have changed since last call to getBits(). + bool bitsHaveChanged() const { return _bitsChanged; } /** * Checks if the pipeline is ready. @@ -94,14 +103,12 @@ public: * Returns whether or not the elements in the pipeline are connected, * and if we are using shmsrc, if the shared memory socket is being read. */ - bool isReady() const { return _padHandlerData.videoIsConnected; } + bool isReady() const { return _movieReady && _padHandlerData.videoIsConnected; } /** - * Tries to pull a video frame from the queue. - * - * Returns true if the image has changed. + * Performs regular updates (checks if movie is ready and checks messages). */ - bool runVideo(); + void update(); // void runAudio(); @@ -125,12 +132,6 @@ protected: void freeResources(); private: - /** - * Tries to pull a video frame from the asynchronous input buffer. - * - * Pushes the frame into the asynchronous output buffer. - */ - bool _videoPull(); /** * Checks if we reached the end of the video file. * @@ -142,7 +143,7 @@ private: // void _init(); bool _preRun(); - void _postRun(); + void _checkMessages(); void _setReady(bool ready); void _setFinished(bool finished); @@ -205,7 +206,8 @@ private: */ GstSample *_currentFrameSample; GstBuffer *_currentFrameBuffer; - GstMapInfo _mapInfo; + GstMapInfo _mapInfo; + bool _bitsChanged; /** * shmsrc socket poller. diff --git a/Paint.cpp b/Paint.cpp index bfa6807..74b6af2 100644 --- a/Paint.cpp +++ b/Paint.cpp @@ -86,8 +86,7 @@ int Media::getHeight() const } void Media::update() { - if (impl_->runVideo()) - bitsChanged = true; + impl_->update(); } void Media::play() @@ -113,11 +112,17 @@ void Media::unlockMutex() { impl_->unlockMutex(); } -const uchar* Media::_getBits() const +const uchar* Media::getBits() { return this->impl_->getBits(); } +bool Media::bitsHaveChanged() const +{ + return this->impl_->bitsHaveChanged(); +} + + bool Media::hasVideoSupport() { return MediaImpl::hasVideoSupport(); diff --git a/Paint.h b/Paint.h index 4fe3b57..3fb7ef3 100644 --- a/Paint.h +++ b/Paint.h @@ -124,8 +124,7 @@ protected: Paint(id), textureId(0), x(0), - y(0), - bitsChanged(true) + y(0) { glGenTextures(1, &textureId); } @@ -142,13 +141,10 @@ public: virtual int getHeight() const = 0; /// Returns image bits data. Next call to bitsHaveChanged() will be false. - virtual const uchar* getBits() const { - bitsChanged = false; - return _getBits(); - } + virtual const uchar* getBits() = 0; /// Returns true iff bits have changed since last call to getBits(). - bool bitsHaveChanged() const { return bitsChanged; } + virtual bool bitsHaveChanged() const = 0; virtual void setPosition(GLfloat xPos, GLfloat yPos) { x = xPos; @@ -156,9 +152,6 @@ public: } virtual GLfloat getX() const { return x; } virtual GLfloat getY() const { return y; } - -protected: - virtual const uchar* _getBits() const = 0; }; /** @@ -192,8 +185,12 @@ public: virtual int getWidth() const { return image.width(); } virtual int getHeight() const { return image.height(); } -protected: - virtual const uchar* _getBits() const { return image.bits(); } + virtual const uchar* getBits() { + bitsChanged = false; + return image.bits(); + } + + virtual bool bitsHaveChanged() const { return bitsChanged; } }; class MediaImpl; // forward declaration @@ -236,14 +233,15 @@ public: virtual int getWidth() const; virtual int getHeight() const; + virtual const uchar* getBits(); + + virtual bool bitsHaveChanged() const; + /** * Checks whether or not video is supported on this platform. */ static bool hasVideoSupport(); -protected: - virtual const uchar* _getBits() const; - private: /** * Private implementation, so that GStreamer headers don't need