完善fps统计

This commit is contained in:
rankun 2018-10-30 13:47:27 +08:00
commit 23d57beb48
7 changed files with 84 additions and 77 deletions

View file

@ -55,7 +55,7 @@ void AdbProcess::initSignals()
} else { } else {
emit adbProcessResult(AER_ERROR_START); emit adbProcessResult(AER_ERROR_START);
QString err = QString("qprocess start error:%1 %2").arg(program()).arg(arguments().join(" ")); QString err = QString("qprocess start error:%1 %2").arg(program()).arg(arguments().join(" "));
qCritical(err.toStdString().c_str()); qCritical(err.toStdString().c_str());
} }
}); });

View file

@ -64,7 +64,7 @@ qint32 Decoder::recvData(quint8* buf, qint32 bufSize)
} }
} }
qint64 readSize = qMin(m_deviceSocket->bytesAvailable(), (qint64)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 m_deviceSocket->read((char*)buf, readSize);
} }
return 0; return 0;
@ -90,14 +90,14 @@ void Decoder::stopDecode()
} }
void Decoder::run() void Decoder::run()
{ {
unsigned char *decoderBuffer = Q_NULLPTR; unsigned char *decoderBuffer = Q_NULLPTR;
AVIOContext *avioCtx = Q_NULLPTR; AVIOContext *avioCtx = Q_NULLPTR;
AVFormatContext *formatCtx = Q_NULLPTR; AVFormatContext *formatCtx = Q_NULLPTR;
AVCodec *codec = Q_NULLPTR; AVCodec *codec = Q_NULLPTR;
AVCodecContext *codecCtx = Q_NULLPTR; AVCodecContext *codecCtx = Q_NULLPTR;
bool isFormatCtxOpen = false; bool isFormatCtxOpen = false;
bool isCodecCtxOpen = false; bool isCodecCtxOpen = false;
// decoder buffer // decoder buffer
decoderBuffer = (unsigned char*)av_malloc(BUFSIZE); decoderBuffer = (unsigned char*)av_malloc(BUFSIZE);
@ -151,7 +151,7 @@ void Decoder::run()
AVPacket packet; AVPacket packet;
av_init_packet(&packet); av_init_packet(&packet);
packet.data = Q_NULLPTR; packet.data = Q_NULLPTR;
packet.size = 0; packet.size = 0;
while (!m_quit && !av_read_frame(formatCtx, &packet)) { while (!m_quit && !av_read_frame(formatCtx, &packet)) {
AVFrame* decodingFrame = m_frames->decodingFrame(); AVFrame* decodingFrame = m_frames->decodingFrame();
@ -220,7 +220,7 @@ void Decoder::run()
break; break;
} }
} }
qDebug() << "End of frames"; qDebug() << "End of frames";
runQuit: runQuit:
if (avioCtx) { if (avioCtx) {
@ -251,7 +251,6 @@ void Decoder::pushFrame()
if (!previousFrameConsumed) { if (!previousFrameConsumed) {
// the previous newFrame will consume this frame // the previous newFrame will consume this frame
return; return;
} }
//qDebug() << "------------>" << QTime::currentTime();
emit newFrame(); emit newFrame();
} }

View file

@ -6,8 +6,6 @@
#include <QPointer> #include <QPointer>
#include <QMutex> #include <QMutex>
//#include "convert.h"
extern "C" extern "C"
{ {
#include "libavcodec/avcodec.h" #include "libavcodec/avcodec.h"

View file

@ -1,8 +1,9 @@
#include <QTimerEvent>
#include <QDebug> #include <QDebug>
#include "fpscounter.h" #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; resetCounter();
// no need to initialize the other fields, they are meaningful only when startCounterTimer();
// started is true
} }
void FpsCounter::fpsCounterStart() void FpsCounter::stop()
{ {
m_started = true; stopCounterTimer();
m_timeCounter.start(); resetCounter();
m_rendered = 0;
#ifdef SKIP_FRAMES
m_skipped = 0;
#endif
} }
void FpsCounter::fpsCounterStop() bool FpsCounter::isStarted()
{ {
m_started = false; return m_counterTimer;
} }
void FpsCounter::fpsCounterAddRenderedFrame() void FpsCounter::addRenderedFrame()
{ {
checkExpired();
m_rendered++; 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++; 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;
}

View file

@ -1,32 +1,35 @@
#ifndef FPSCOUNTER_H #ifndef FPSCOUNTER_H
#define FPSCOUNTER_H #define FPSCOUNTER_H
#include <QTime> #include <QObject>
class FpsCounter class FpsCounter : public QObject
{ {
Q_OBJECT
public: public:
FpsCounter(); FpsCounter(QObject* parent = Q_NULLPTR);
virtual ~FpsCounter(); virtual ~FpsCounter();
void fpsCounterInit(); void start();
void fpsCounterStart(); void stop();
void fpsCounterStop(); bool isStarted();
void fpsCounterAddRenderedFrame(); void addRenderedFrame();
#ifdef SKIP_FRAMES void addSkippedFrame();
void fpsCounterAddSkippedFrame();
#endif protected:
virtual void timerEvent(QTimerEvent *event);
private: private:
void checkExpired(); void startCounterTimer();
void displayFps(); 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; quint32 m_rendered = 0;
#ifdef SKIP_FRAMES
quint32 m_skipped = 0; quint32 m_skipped = 0;
#endif
}; };
#endif // FPSCOUNTER_H #endif // FPSCOUNTER_H

View file

@ -30,6 +30,8 @@ bool Frames::init()
// there is initially no rendering frame, so consider it has already been // there is initially no rendering frame, so consider it has already been
// consumed // consumed
m_renderingFrameConsumed = true; m_renderingFrameConsumed = true;
m_fpsCounter.start();
return true; return true;
error: error:
@ -47,6 +49,7 @@ void Frames::deInit()
av_frame_free(&m_renderingframe); av_frame_free(&m_renderingframe);
m_renderingframe = Q_NULLPTR; m_renderingframe = Q_NULLPTR;
} }
m_fpsCounter.stop();
} }
void Frames::lock() void Frames::lock()
@ -73,9 +76,11 @@ bool Frames::offerDecodedFrame()
// frame to be consumed // frame to be consumed
while (!m_renderingFrameConsumed && !m_stopped) { while (!m_renderingFrameConsumed && !m_stopped) {
m_renderingFrameConsumedCond.wait(&m_mutex); m_renderingFrameConsumedCond.wait(&m_mutex);
} }
#else #else
if (m_fpsCounter.isStarted() && !m_renderingFrameConsumed) {
m_fpsCounter.addSkippedFrame();
}
#endif #endif
swap(); swap();
@ -89,7 +94,9 @@ const AVFrame *Frames::consumeRenderedFrame()
{ {
Q_ASSERT(!m_renderingFrameConsumed); Q_ASSERT(!m_renderingFrameConsumed);
m_renderingFrameConsumed = true; m_renderingFrameConsumed = true;
if (m_fpsCounter.isStarted()) {
m_fpsCounter.addRenderedFrame();
}
#ifndef SKIP_FRAMES #ifndef SKIP_FRAMES
// if SKIP_FRAMES is disabled, then notify the decoder the current frame is // if SKIP_FRAMES is disabled, then notify the decoder the current frame is
// consumed, so that it may push a new one // consumed, so that it may push a new one

View file

@ -3,6 +3,8 @@
#include <QMutex> #include <QMutex>
#include <QWaitCondition> #include <QWaitCondition>
#include "fpscounter.h"
// forward declarations // forward declarations
typedef struct AVFrame AVFrame; typedef struct AVFrame AVFrame;
@ -40,10 +42,11 @@ private:
AVFrame* m_renderingframe = Q_NULLPTR; AVFrame* m_renderingframe = Q_NULLPTR;
QMutex m_mutex; QMutex m_mutex;
bool m_renderingFrameConsumed = true; bool m_renderingFrameConsumed = true;
FpsCounter m_fpsCounter;
#ifndef SKIP_FRAMES #ifndef SKIP_FRAMES
QWaitCondition m_renderingFrameConsumedCond; QWaitCondition m_renderingFrameConsumedCond;
bool m_stopped = true; bool m_stopped = true;
#endif #endif
}; };