From 457a54d59109fa5e224ad1967d8876e42e1cb88e Mon Sep 17 00:00:00 2001 From: "Tux3 / Mlkj / !Lev.uXFMLA" Date: Sat, 12 Jul 2014 12:50:40 +0200 Subject: [PATCH] Fix video sending --- core.cpp | 23 +++++++++++++++-------- core.h | 4 ++-- widget/camera.cpp | 1 + widget/netcamview.cpp | 6 +++++- widget/videosurface.cpp | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/core.cpp b/core.cpp index 6cc02e133..1383cc8fe 100644 --- a/core.cpp +++ b/core.cpp @@ -33,10 +33,14 @@ const QString Core::CONFIG_FILE_NAME = "data"; QList Core::fileSendQueue; 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; Core::Core(Camera* cam, QThread *coreThread) : tox(nullptr), camera(cam) { + videobuf = new uint8_t[videobufsize]; + toxTimer = new QTimer(this); toxTimer->setSingleShot(true); //saveTimer = new QTimer(this); @@ -70,6 +74,12 @@ Core::~Core() toxav_kill(toxav); tox_kill(tox); } + + if (videobuf) + { + delete[] videobuf; + videobuf=nullptr; + } } void Core::start() @@ -1171,8 +1181,8 @@ void Core::prepareCall(int friendId, int callId, ToxAv* toxav, bool videoEnabled calls[callId].callId = callId; calls[callId].friendId = friendId; calls[callId].codecSettings = av_DefaultSettings; - calls[callId].codecSettings.max_video_width = TOXAV_VIDEO_WIDTH; - calls[callId].codecSettings.max_video_height = TOXAV_VIDEO_HEIGHT; + calls[callId].codecSettings.max_video_width = TOXAV_MAX_VIDEO_WIDTH; + calls[callId].codecSettings.max_video_height = TOXAV_MAX_VIDEO_HEIGHT; calls[callId].videoEnabled = videoEnabled; toxav_prepare_transmission(toxav, callId, &calls[callId].codecSettings, videoEnabled); @@ -1316,15 +1326,14 @@ void Core::sendCallVideo(int callId) if (!calls[callId].active || !calls[callId].videoEnabled) return; - static const int bufsize = TOXAV_MAX_VIDEO_WIDTH * TOXAV_MAX_VIDEO_HEIGHT * 4; - static uint8_t* videobuf = new uint8_t[bufsize]; vpx_image frame = camera->getLastVPXImage(); if (frame.w && frame.h) { int result; - if((result = toxav_prepare_video_frame(toxav, callId, videobuf, bufsize, &frame)) < 0) + if((result = toxav_prepare_video_frame(toxav, callId, videobuf, videobufsize, &frame)) < 0) { qDebug() << QString("Core: toxav_prepare_video_frame: error %1").arg(result); + vpx_img_free(&frame); calls[callId].sendVideoTimer->start(); return; } @@ -1332,9 +1341,7 @@ void Core::sendCallVideo(int callId) if((result = toxav_send_video(toxav, callId, (uint8_t*)videobuf, result)) < 0) qDebug() << QString("Core: toxav_send_video error: %1").arg(result); - - emit Widget::getInstance()->getCore()->videoFrameReceived(frame); - + vpx_img_free(&frame); } else qDebug("Core::sendCallVideo: Invalid frame (bad camera ?)"); diff --git a/core.h b/core.h index fb36079d2..175b8abf0 100644 --- a/core.h +++ b/core.h @@ -44,8 +44,6 @@ // TODO: Put that in the settings #define TOXAV_MAX_VIDEO_WIDTH 1600 #define TOXAV_MAX_VIDEO_HEIGHT 1200 -#define TOXAV_VIDEO_WIDTH 640 -#define TOXAV_VIDEO_HEIGHT 480 class Camera; @@ -289,6 +287,8 @@ private: static ToxCall calls[TOXAV_MAX_CALLS]; static const QString CONFIG_FILE_NAME; + static const int videobufsize; + static uint8_t* videobuf; }; #endif // CORE_HPP diff --git a/widget/camera.cpp b/widget/camera.cpp index a54ac2479..5c6c15e5b 100644 --- a/widget/camera.cpp +++ b/widget/camera.cpp @@ -231,6 +231,7 @@ QImage Camera::getLastImage() vpx_image Camera::getLastVPXImage() { vpx_image img; + img.w = img.h = 0; if (!lastFrame.isValid()) return img; if (!lastFrame.map(QAbstractVideoBuffer::ReadOnly)) diff --git a/widget/netcamview.cpp b/widget/netcamview.cpp index c60f255ca..97ba9e06c 100644 --- a/widget/netcamview.cpp +++ b/widget/netcamview.cpp @@ -32,6 +32,10 @@ NetCamView::NetCamView(QWidget* parent) void NetCamView::updateDisplay(vpx_image frame) { int w = frame.d_w, h = frame.d_h; + + if (!frame.w || !frame.h || !w || !h) + return; + int bpl = frame.stride[VPX_PLANE_Y], cxbpl = frame.stride[VPX_PLANE_V]; QImage img(w, h, QImage::Format_RGB32); @@ -55,6 +59,6 @@ void NetCamView::updateDisplay(vpx_image frame) } } - lastFrame = img; + vpx_img_free(&frame); displayLabel->setPixmap(QPixmap::fromImage(img)); } diff --git a/widget/videosurface.cpp b/widget/videosurface.cpp index 468eb34d7..68ec9ff49 100644 --- a/widget/videosurface.cpp +++ b/widget/videosurface.cpp @@ -22,7 +22,7 @@ VideoSurface::VideoSurface() : QAbstractVideoSurface() { - vpx_img_alloc(&input, VPX_IMG_FMT_YV12, TOXAV_VIDEO_WIDTH, TOXAV_VIDEO_HEIGHT, 1); + vpx_img_alloc(&input, VPX_IMG_FMT_YV12, TOXAV_MAX_VIDEO_WIDTH, TOXAV_MAX_VIDEO_HEIGHT, 1); } bool VideoSurface::start(const QVideoSurfaceFormat &format)