diff --git a/src/adbprocess.cpp b/src/adbprocess.cpp index 66e330f..99b8705 100644 --- a/src/adbprocess.cpp +++ b/src/adbprocess.cpp @@ -55,7 +55,7 @@ void AdbProcess::initSignals() } else { emit adbProcessResult(AER_ERROR_START); QString err = QString("qprocess start error:%1 %2").arg(program()).arg(arguments().join(" ")); - qCritical(err.toStdString().c_str()); + qCritical(err.toStdString().c_str()); } }); diff --git a/src/decoder.cpp b/src/decoder.cpp index 893fa65..68adda6 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -64,7 +64,7 @@ qint32 Decoder::recvData(quint8* buf, qint32 bufSize) } } qint64 readSize = qMin(m_deviceSocket->bytesAvailable(), (qint64)bufSize); - qDebug() << "ready recv data " << readSize; + //qDebug() << "ready recv data " << readSize; return m_deviceSocket->read((char*)buf, readSize); } return 0; @@ -90,14 +90,14 @@ void Decoder::stopDecode() } void Decoder::run() -{ +{ unsigned char *decoderBuffer = Q_NULLPTR; AVIOContext *avioCtx = Q_NULLPTR; AVFormatContext *formatCtx = Q_NULLPTR; AVCodec *codec = Q_NULLPTR; AVCodecContext *codecCtx = Q_NULLPTR; bool isFormatCtxOpen = false; - bool isCodecCtxOpen = false; + bool isCodecCtxOpen = false; // decoder buffer decoderBuffer = (unsigned char*)av_malloc(BUFSIZE); @@ -151,7 +151,7 @@ void Decoder::run() AVPacket packet; av_init_packet(&packet); packet.data = Q_NULLPTR; - packet.size = 0; + packet.size = 0; while (!m_quit && !av_read_frame(formatCtx, &packet)) { AVFrame* decodingFrame = m_frames->decodingFrame(); @@ -220,7 +220,7 @@ void Decoder::run() break; } } - qDebug() << "End of frames"; + qDebug() << "End of frames"; runQuit: if (avioCtx) { @@ -251,7 +251,6 @@ void Decoder::pushFrame() if (!previousFrameConsumed) { // the previous newFrame will consume this frame return; - } - //qDebug() << "------------>" << QTime::currentTime(); + } emit newFrame(); } diff --git a/src/decoder.h b/src/decoder.h index 7988304..572b346 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -6,8 +6,6 @@ #include #include -//#include "convert.h" - extern "C" { #include "libavcodec/avcodec.h" diff --git a/src/fpscounter.cpp b/src/fpscounter.cpp index 255d51f..43deed2 100644 --- a/src/fpscounter.cpp +++ b/src/fpscounter.cpp @@ -1,8 +1,9 @@ +#include #include #include "fpscounter.h" -FpsCounter::FpsCounter() +FpsCounter::FpsCounter(QObject* parent) : QObject(parent) { } @@ -12,63 +13,59 @@ FpsCounter::~FpsCounter() } -void FpsCounter::fpsCounterInit() +void FpsCounter::start() { - m_started = false; - // no need to initialize the other fields, they are meaningful only when - // started is true + resetCounter(); + startCounterTimer(); } -void FpsCounter::fpsCounterStart() -{ - m_started = true; - m_timeCounter.start(); - m_rendered = 0; -#ifdef SKIP_FRAMES - m_skipped = 0; -#endif +void FpsCounter::stop() +{ + stopCounterTimer(); + resetCounter(); } -void FpsCounter::fpsCounterStop() +bool FpsCounter::isStarted() { - m_started = false; + return m_counterTimer; } -void FpsCounter::fpsCounterAddRenderedFrame() +void FpsCounter::addRenderedFrame() { - checkExpired(); m_rendered++; } -void FpsCounter::checkExpired() +void FpsCounter::addSkippedFrame() { - if (m_timeCounter.elapsed() >= 1000) { - displayFps(); - m_timeCounter.restart(); - m_rendered = 0; -#ifdef SKIP_FRAMES - m_skipped = 0; -#endif - } -} - -void FpsCounter::displayFps() -{ -#ifdef SKIP_FRAMES - if (m_skipped) { - //qInfo << "%d fps (+%d frames skipped)", m_rendered, m_skipped); - } else { -#endif - //qInfo m_rendered << "fps"; -#ifdef SKIP_FRAMES - } -#endif -} - -#ifdef SKIP_FRAMES -void FpsCounter::fpsCounterAddSkippedFrame() -{ - checkExpired(); m_skipped++; } -#endif + +void FpsCounter::timerEvent(QTimerEvent *event) +{ + if (event && m_counterTimer == event->timerId()) { + m_curRendered = m_rendered; + m_curSkipped = m_skipped; + resetCounter(); + qInfo("FPS:%d Discard:%d", m_curRendered, m_skipped); + } +} + +void FpsCounter::startCounterTimer() +{ + stopCounterTimer(); + m_counterTimer = startTimer(1000); +} + +void FpsCounter::stopCounterTimer() +{ + if (m_counterTimer) { + killTimer(m_counterTimer); + m_counterTimer = 0; + } +} + +void FpsCounter::resetCounter() +{ + m_rendered = 0; + m_skipped = 0; +} diff --git a/src/fpscounter.h b/src/fpscounter.h index baac75c..bfad3d2 100644 --- a/src/fpscounter.h +++ b/src/fpscounter.h @@ -1,32 +1,35 @@ #ifndef FPSCOUNTER_H #define FPSCOUNTER_H -#include +#include -class FpsCounter +class FpsCounter : public QObject { + Q_OBJECT public: - FpsCounter(); + FpsCounter(QObject* parent = Q_NULLPTR); virtual ~FpsCounter(); - void fpsCounterInit(); - void fpsCounterStart(); - void fpsCounterStop(); - void fpsCounterAddRenderedFrame(); -#ifdef SKIP_FRAMES - void fpsCounterAddSkippedFrame(); -#endif + void start(); + void stop(); + bool isStarted(); + void addRenderedFrame(); + void addSkippedFrame(); + +protected: + virtual void timerEvent(QTimerEvent *event); private: - void checkExpired(); - void displayFps(); + void startCounterTimer(); + void stopCounterTimer(); + void resetCounter(); + +private: + quint32 m_counterTimer = 0; + quint32 m_curRendered = 0; + quint32 m_curSkipped = 0; -private: - bool m_started = false; - QTime m_timeCounter; quint32 m_rendered = 0; -#ifdef SKIP_FRAMES quint32 m_skipped = 0; -#endif }; #endif // FPSCOUNTER_H diff --git a/src/frames.cpp b/src/frames.cpp index 922bb97..07ae0b9 100644 --- a/src/frames.cpp +++ b/src/frames.cpp @@ -30,6 +30,8 @@ bool Frames::init() // there is initially no rendering frame, so consider it has already been // consumed m_renderingFrameConsumed = true; + + m_fpsCounter.start(); return true; error: @@ -47,6 +49,7 @@ void Frames::deInit() av_frame_free(&m_renderingframe); m_renderingframe = Q_NULLPTR; } + m_fpsCounter.stop(); } void Frames::lock() @@ -73,9 +76,11 @@ bool Frames::offerDecodedFrame() // frame to be consumed while (!m_renderingFrameConsumed && !m_stopped) { m_renderingFrameConsumedCond.wait(&m_mutex); - } + } #else - + if (m_fpsCounter.isStarted() && !m_renderingFrameConsumed) { + m_fpsCounter.addSkippedFrame(); + } #endif swap(); @@ -89,7 +94,9 @@ const AVFrame *Frames::consumeRenderedFrame() { Q_ASSERT(!m_renderingFrameConsumed); m_renderingFrameConsumed = true; - + if (m_fpsCounter.isStarted()) { + m_fpsCounter.addRenderedFrame(); + } #ifndef SKIP_FRAMES // if SKIP_FRAMES is disabled, then notify the decoder the current frame is // consumed, so that it may push a new one diff --git a/src/frames.h b/src/frames.h index 919efef..6200926 100644 --- a/src/frames.h +++ b/src/frames.h @@ -3,6 +3,8 @@ #include #include +#include "fpscounter.h" + // forward declarations typedef struct AVFrame AVFrame; @@ -40,10 +42,11 @@ private: AVFrame* m_renderingframe = Q_NULLPTR; QMutex m_mutex; bool m_renderingFrameConsumed = true; + FpsCounter m_fpsCounter; #ifndef SKIP_FRAMES QWaitCondition m_renderingFrameConsumedCond; - bool m_stopped = true; + bool m_stopped = true; #endif };