From 5458b4b3eeb7175209b682641e165e7666fc9876 Mon Sep 17 00:00:00 2001 From: "Tux3 / Mlkj / !Lev.uXFMLA" Date: Sat, 12 Jul 2014 14:10:24 +0200 Subject: [PATCH] Drop video frames when lagging Aka, all the time. We only drop received frames, we never stop sending --- core.cpp | 21 +++++++++++++++++---- core.h | 6 +++++- widget/netcamview.cpp | 31 ++++++++++++++++++++++++------- widget/netcamview.h | 5 ++++- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/core.cpp b/core.cpp index 1383cc8fe..a6c228f24 100644 --- a/core.cpp +++ b/core.cpp @@ -35,11 +35,13 @@ QList Core::fileRecvQueue; ToxCall Core::calls[TOXAV_MAX_CALLS]; const int Core::videobufsize{TOXAV_MAX_VIDEO_WIDTH * TOXAV_MAX_VIDEO_HEIGHT * 4}; uint8_t* Core::videobuf; +int Core::videoBusyness; Core::Core(Camera* cam, QThread *coreThread) : tox(nullptr), camera(cam) { videobuf = new uint8_t[videobufsize]; + videoBusyness=0; toxTimer = new QTimer(this); toxTimer->setSingleShot(true); @@ -1312,13 +1314,14 @@ void Core::sendCallAudio(int callId, ToxAv* toxav) void Core::playCallVideo(ToxAv*, int32_t callId, vpx_image_t* img) { - qDebug() << "Core: Got video frame"; if (!calls[callId].active || !calls[callId].videoEnabled) return; - qDebug() << "Core: Got video frame, call's active"; - - emit Widget::getInstance()->getCore()->videoFrameReceived(*img); + if (videoBusyness >= 2) + qWarning() << "Core: playCallVideo: Busy, dropping current frame"; + else + emit Widget::getInstance()->getCore()->videoFrameReceived(img); + vpx_img_free(img); } void Core::sendCallVideo(int callId) @@ -1358,3 +1361,13 @@ void Core::createGroup() { emit emptyGroupCreated(tox_add_groupchat(tox)); } + +void Core::increaseVideoBusyness() +{ + videoBusyness++; +} + +void Core::decreaseVideoBusyness() +{ + videoBusyness--; +} diff --git a/core.h b/core.h index 175b8abf0..c4e7e2281 100644 --- a/core.h +++ b/core.h @@ -122,6 +122,9 @@ public: QString getUsername(); QString getStatusMessage(); + void increaseVideoBusyness(); + void decreaseVideoBusyness(); + public slots: void start(); void process(); @@ -223,7 +226,7 @@ signals: void avRequestTimeout(int friendId, int callIndex); void avPeerTimeout(int friendId, int callIndex); - void videoFrameReceived(vpx_image frame); + void videoFrameReceived(vpx_image* frame); private: static void onFriendRequest(Tox* tox, const uint8_t* cUserId, const uint8_t* cMessage, uint16_t cMessageSize, void* core); @@ -289,6 +292,7 @@ private: static const QString CONFIG_FILE_NAME; static const int videobufsize; static uint8_t* videobuf; + static int videoBusyness; // Used to know when to drop frames }; #endif // CORE_HPP diff --git a/widget/netcamview.cpp b/widget/netcamview.cpp index 97ba9e06c..9555db926 100644 --- a/widget/netcamview.cpp +++ b/widget/netcamview.cpp @@ -15,6 +15,10 @@ */ #include "netcamview.h" +#include "core.h" +#include "widget.h" +#include +#include NetCamView::NetCamView(QWidget* parent) : QWidget(parent), displayLabel{new QLabel}, @@ -29,13 +33,28 @@ NetCamView::NetCamView(QWidget* parent) mainLayout->addWidget(displayLabel); } -void NetCamView::updateDisplay(vpx_image frame) +void NetCamView::updateDisplay(vpx_image* frame) { - int w = frame.d_w, h = frame.d_h; - - if (!frame.w || !frame.h || !w || !h) + if (!frame->w || !frame->h) return; + Core* core = Widget::getInstance()->getCore(); + + core->increaseVideoBusyness(); + + QFuture future = QtConcurrent::run(convert,*frame); + qApp->processEvents(); + QImage img = future.result(); + + vpx_img_free(frame); + displayLabel->setPixmap(QPixmap::fromImage(img)); + + core->decreaseVideoBusyness(); +} + +QImage NetCamView::convert(vpx_image& frame) +{ + int w = frame.d_w, h = frame.d_h; int bpl = frame.stride[VPX_PLANE_Y], cxbpl = frame.stride[VPX_PLANE_V]; QImage img(w, h, QImage::Format_RGB32); @@ -58,7 +77,5 @@ void NetCamView::updateDisplay(vpx_image frame) scanline[j] = (0xFF<<24) + (R<<16) + (G<<8) + B; } } - - vpx_img_free(&frame); - displayLabel->setPixmap(QPixmap::fromImage(img)); + return img; } diff --git a/widget/netcamview.h b/widget/netcamview.h index 8aa7efe95..7552dd267 100644 --- a/widget/netcamview.h +++ b/widget/netcamview.h @@ -35,7 +35,10 @@ public: NetCamView(QWidget *parent=0); public slots: - void updateDisplay(vpx_image frame); + void updateDisplay(vpx_image* frame); + +private: + static QImage convert(vpx_image& frame); private: QLabel *displayLabel;