From e8fd8f81955964ee24c0c613956f63656a3f4d80 Mon Sep 17 00:00:00 2001 From: zcy Date: Thu, 12 May 2022 17:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84capturer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/webrtc_capture/qss/css/QssTitleBar.css | 2 +- .../qss/css/corrugation_form.css | 2 +- .../webrtc_capture/src/camera_video_sink.cpp | 35 +++++++++-- client/webrtc_capture/src/camera_video_sink.h | 10 +++- client/webrtc_capture/src/cplaywidget.cpp | 8 ++- client/webrtc_capture/src/main.cpp | 2 +- client/webrtc_capture/src/mainwindow.cpp | 59 ++++++++++++++++++- client/webrtc_capture/src/mainwindow.h | 2 +- client/webrtc_capture/webrtc_capture.pro | 2 +- 9 files changed, 102 insertions(+), 20 deletions(-) diff --git a/client/webrtc_capture/qss/css/QssTitleBar.css b/client/webrtc_capture/qss/css/QssTitleBar.css index 90be03b..8c9032a 100644 --- a/client/webrtc_capture/qss/css/QssTitleBar.css +++ b/client/webrtc_capture/qss/css/QssTitleBar.css @@ -43,7 +43,7 @@ QWidget#qssTitleBar >QPushButton#titlebarrestorebtn{ image: url(":/qss/icon/btn_max_normal.svg"); background: rgb(187, 212, 238); width: 25px; - height:25px; + height:25pt; margin-left: 1px; margin-right: 2px; } diff --git a/client/webrtc_capture/qss/css/corrugation_form.css b/client/webrtc_capture/qss/css/corrugation_form.css index b7e5d55..76b75dc 100644 --- a/client/webrtc_capture/qss/css/corrugation_form.css +++ b/client/webrtc_capture/qss/css/corrugation_form.css @@ -22,7 +22,7 @@ QPushButton:enabled:pressed { QPushButton#pushButton:enabled { background: white; color: black; - border-radius:3px; + border-radius:3pd; } QPushButton#pushButton:!enabled { diff --git a/client/webrtc_capture/src/camera_video_sink.cpp b/client/webrtc_capture/src/camera_video_sink.cpp index baf2f60..40ea1a6 100644 --- a/client/webrtc_capture/src/camera_video_sink.cpp +++ b/client/webrtc_capture/src/camera_video_sink.cpp @@ -1,4 +1,5 @@ #include "camera_video_sink.h" +#include CameraVideoSink::CameraVideoSink() : m_vcm(nullptr) { rtc::LogMessage::SetLogToStderr(true); @@ -75,30 +76,52 @@ void CameraVideoSink::OnFrame(const webrtc::VideoFrame& frame) { std::chrono::system_clock::now().time_since_epoch()).count(); static size_t cnt = 0; - rtc::scoped_refptr frameBuffer = frame.video_frame_buffer()->ToI420(); + qDebug()<type()); + rtc::scoped_refptr frameBuffer = + frame.video_frame_buffer()->ToI420(); cnt++; - m_buf.PushFirst(&frameBuffer); + int width = this->m_capability.width; + int height = this->m_capability.height; + qDebug()<Capability().height<Capability().width + <Capability().videoType) + <Capability().interlaced<GetI420()->type()); + memcpy(data,frameBuffer->GetI420()->DataY(),width*height); + memcpy(data + width*height ,frameBuffer->GetI420()->DataU(),width*height/4); + memcpy(data + width*height + width*height/4, + frameBuffer->GetI420()->DataY(),width*height/4); + + m_buf.PushFirst(data); auto timestamp_curr = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); if(timestamp_curr - timestamp > 1000) { RTC_LOG(LS_INFO) << "FPS: " << cnt< *FrameBufferList::TakeLast() + +webrtc::VideoCaptureCapability CameraVideoSink::Capability() +{ + return this->m_capability; +} + +uint8_t *FrameBufferList::TakeLast() { std::lock_guard guard(this->m_mutex); if(m_data.size() != 0){ - rtc::scoped_refptr * ret = *m_data.begin(); + auto ret = *m_data.begin(); m_data.pop_front(); return ret; } } -void FrameBufferList::PushFirst(rtc::scoped_refptr *v) +void FrameBufferList::PushFirst(uint8_t*v) { std::lock_guard guard(this->m_mutex); m_data.push_back(v); diff --git a/client/webrtc_capture/src/camera_video_sink.h b/client/webrtc_capture/src/camera_video_sink.h index 930ef4a..4eb512f 100644 --- a/client/webrtc_capture/src/camera_video_sink.h +++ b/client/webrtc_capture/src/camera_video_sink.h @@ -12,11 +12,11 @@ class FrameBufferList{ public: - rtc::scoped_refptr* TakeLast(); - void PushFirst(rtc::scoped_refptr*); + uint8_t* TakeLast(); + void PushFirst(uint8_t*); uint16_t Size(); private: - std::list*> m_data; + std::list m_data; std::mutex m_mutex; }; @@ -33,6 +33,10 @@ public: size_t capture_device_index); virtual ~CameraVideoSink(); void OnFrame(const webrtc::VideoFrame& frame) override; + webrtc::VideoCaptureCapability Capability(); + FrameBufferList *VideoBuffer(){ + return &this->m_buf; + } signals: void UpdateFrame(rtc::scoped_refptr& buffer); private: diff --git a/client/webrtc_capture/src/cplaywidget.cpp b/client/webrtc_capture/src/cplaywidget.cpp index e42148c..6cf0960 100644 --- a/client/webrtc_capture/src/cplaywidget.cpp +++ b/client/webrtc_capture/src/cplaywidget.cpp @@ -107,7 +107,7 @@ CPlayWidget::CPlayWidget(QWidget *parent):QOpenGLWidget(parent) { m_pTextureV = NULL; m_nVideoH = 0; m_nVideoW = 0; - mType = TYPE_UNSET; + mType = TYPE_I420; } CPlayWidget::~CPlayWidget() { @@ -116,6 +116,7 @@ CPlayWidget::~CPlayWidget() { int CPlayWidget::SetDataType(CPlayWidget::IMG_TYPE type){ this->mType = type; +// initializeGL(); return 0; } @@ -132,7 +133,7 @@ void CPlayWidget::OnCameraData( rtc::scoped_refptr int CPlayWidget::OnCameraData(uint8_t *p) { - memcpy(m_pBufYuv420p,p,640*480/2*3); + memcpy(m_pBufYuv420p,p,m_nVideoH*m_nVideoW/2*3); update(); return 0; } @@ -180,7 +181,7 @@ void CPlayWidget::initializeGL() bool bCompile = m_pVSHader->compileSourceCode(vsrcyuv); if(!bCompile) { - // todo ????????? + } m_pFSHader = new QOpenGLShader(QOpenGLShader::Fragment, this); if(mType == TYPE_RGB32){ @@ -194,6 +195,7 @@ void CPlayWidget::initializeGL() } if(!bCompile) { + } #define PROGRAM_VERTEX_ATTRIBUTE 0 #define PROGRAM_TEXCOORD_ATTRIBUTE 1 diff --git a/client/webrtc_capture/src/main.cpp b/client/webrtc_capture/src/main.cpp index c911c7e..bc8a5d4 100644 --- a/client/webrtc_capture/src/main.cpp +++ b/client/webrtc_capture/src/main.cpp @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) qRegisterMetaType>("rtc::scoped_refptr"); qRegisterMetaType>("rtc::scoped_refptr&"); - QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv); MainWindow w; // QObject::connect((CameraVideoSink*)capturer.get(),SIGNAL(UpdateFrame(rtc::scoped_refptr&)),&w, diff --git a/client/webrtc_capture/src/mainwindow.cpp b/client/webrtc_capture/src/mainwindow.cpp index 8f3815e..52b3a1c 100644 --- a/client/webrtc_capture/src/mainwindow.cpp +++ b/client/webrtc_capture/src/mainwindow.cpp @@ -1,12 +1,51 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -# +#include "camera_video_sink.h" + +class AsyncRennder :public QSSASyncProcess{ +public: + AsyncRennder(QWidget * parent,FrameBufferList* p,CPlayWidget *render_ui){ + this->mParent = parent; + this->mfbs = p; + mUI = render_ui; + this->state = true; + } + // 停止渲染 + void StopRender(){ + state = false; + } + void Run(void *) override{ + QEventLoop eventLoop; + auto qtimer = new QTimer(this); + qtimer->setSingleShot(false); + + QObject::connect(qtimer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); + qtimer->start(10); + + while(state){ + if(mfbs ->Size() > 0){ + uint8_t *frame = this->mfbs->TakeLast(); + mUI->OnCameraData(frame); + eventLoop.exec(); // 渲染一次 + delete frame; + } + + } + } +private: + FrameBufferList *mfbs; + bool state = false; + CPlayWidget *mUI; +}; + +AsyncRennder *gRender; + MainWindow::MainWindow(QWidget *parent) : QssMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); - ui->openGLWidget->SetImgSize(640,480); +// ui->openGLWidget->SetImgSize(640,480); ui->openGLWidget->show(); std::unique_ptr info( @@ -24,6 +63,7 @@ MainWindow::MainWindow(QWidget *parent) // break; // } } + } MainWindow::~MainWindow() @@ -36,7 +76,6 @@ void MainWindow::OnUpdateFrame( rtc::scoped_refptr& ui->openGLWidget->OnCameraData(buffer); } - void MainWindow::on_pushButton_clicked() { int id = ui->comboBox->currentData().toInt(); @@ -45,5 +84,19 @@ void MainWindow::on_pushButton_clicked() if (!m_capturer) { qDebug()<<"error"; } + ui->openGLWidget->SetDataType(CPlayWidget::TYPE_YUV420P); + ui->openGLWidget->SetImgSize(m_capturer->Capability().width, + m_capturer->Capability().height); +// ui->openGLWidget->moveToThread(&gRender->Thread()); + if(gRender == nullptr){ + gRender = new AsyncRennder(this,m_capturer->VideoBuffer(),ui->openGLWidget); + gRender->Start(this); + connect(gRender,&QSSASyncProcess::Done,this,&MainWindow::RenderDone); + } +} + +void MainWindow::RenderDone() +{ + } diff --git a/client/webrtc_capture/src/mainwindow.h b/client/webrtc_capture/src/mainwindow.h index a7cf2bf..dac11ee 100644 --- a/client/webrtc_capture/src/mainwindow.h +++ b/client/webrtc_capture/src/mainwindow.h @@ -20,7 +20,7 @@ public slots: void OnUpdateFrame( rtc::scoped_refptr& buffer); private slots: void on_pushButton_clicked(); - + void RenderDone(); private: Ui::MainWindow *ui; std::unique_ptr m_capturer; diff --git a/client/webrtc_capture/webrtc_capture.pro b/client/webrtc_capture/webrtc_capture.pro index ddeffaa..81f2da9 100644 --- a/client/webrtc_capture/webrtc_capture.pro +++ b/client/webrtc_capture/webrtc_capture.pro @@ -1,6 +1,6 @@ QT += core gui -include(D:\\project\\c++qt\\qsswraper\\qsswraper.pri) +include(D:\\project\\qt_project\\qsswraper\\qsswraper.pri) RESOURCES += $$PWD/qss/qss.qrc greaterThan(QT_MAJOR_VERSION, 4): QT += widgets