整合功能到主界面,但是出现playerwidget如果作为子widget图像会变成绿色的底色

master
a7458969 2020-05-27 00:54:03 +08:00
parent 0e3f6f3b97
commit 67836fb226
7 changed files with 209 additions and 83 deletions

View File

@ -203,7 +203,7 @@ void CPlayWidget::initializeGL()
if(this->mType == TYPE_RGB32){ if(this->mType == TYPE_RGB32){
initShaderRgb(); initShaderRgb();
} }
glClearColor(0.3,0.3,0.3,0.0);//设置背景色 glClearColor(0.0,0.0,0.0,0.0);//设置背景色
} }
void CPlayWidget::resizeGL(int w, int h) void CPlayWidget::resizeGL(int w, int h)
{ {

View File

@ -3,6 +3,7 @@
#include "cplaywidget.h" #include "cplaywidget.h"
#include <QTimer> #include <QTimer>
#include "CameraCapture.h" #include "CameraCapture.h"
#include "mainwindow.h"
void CameraDataCallback(double ts, BYTE *dat, LONG size){ void CameraDataCallback(double ts, BYTE *dat, LONG size){
qDebug()<<"reieve cnt"<<size; qDebug()<<"reieve cnt"<<size;
@ -11,22 +12,26 @@ void CameraDataCallback(double ts, BYTE *dat, LONG size){
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
MainWindow main;
main.show();
/*
CPlayWidget gPlayer(nullptr); CPlayWidget gPlayer(nullptr);
gPlayer.SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32); gPlayer.SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
gPlayer.SetImgSize(640,480); gPlayer.SetImgSize(640,480);
gPlayer.show(); gPlayer.show();
Camera *gCam;
Camera *gCam = Camera::GetInstance(); std::vector<std::wstring> names = Camera::EnumAllCamera();
std::vector<std::wstring> names = gCam->EnumAllCamera();
for(auto itr = names.begin();itr != names.end();itr++){ for(auto itr = names.begin();itr != names.end();itr++){
qDebug()<<QString::fromStdWString(*itr);
}
gCam->SetObserver(&gPlayer);
if(gCam->Open(*names.begin()) ){ if(gCam->Open(*names.begin()) ){
Camera *gCam = new Camera(*names.begin());
gCam->SetObserver(&gPlayer);
qDebug("open success"); qDebug("open success");
}else{ }else{
qDebug("open fail"); qDebug("open fail");
} }
qDebug()<<QString::fromStdWString(*itr);
}
*/
return a.exec(); return a.exec();
} }

View File

@ -3,12 +3,44 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
ui(new Ui::MainWindow) ui(new Ui::MainWindow),
m_bCameraOpen(false),
mCamera(nullptr)
{ {
ui->setupUi(this); ui->setupUi(this);
std::vector<std::wstring> cameras = Camera::EnumAllCamera();
for(std::wstring x : cameras){
ui->comboBox->addItem(QString::fromWCharArray(x.c_str(),x.size()),
QString::fromWCharArray(x.c_str(),x.size()));
}
} }
MainWindow::~MainWindow() MainWindow::~MainWindow(){
{
delete ui; delete ui;
} }
void MainWindow::on_pushButton_clicked(){
if(nullptr == mPlayerWidget){
mPlayerWidget = new CPlayWidget(nullptr);
}
if(!m_bCameraOpen){
ui->widget->SetDataType(CPlayWidget::IMG_TYPE::TYPE_RGB32);
ui->widget->SetImgSize(640,480);
qDebug()<<ui->comboBox->currentText().size()<<ui->comboBox->currentText();
wchar_t *opencamera = new wchar_t[ui->comboBox->currentText().size()];
ui->comboBox->currentText().toWCharArray(opencamera);
wstring ss = wstring(opencamera,ui->comboBox->currentText().size());
if(nullptr == mCamera){
this->mCamera = new Camera(ss);
}
this->mCamera->SetObserver(ui->widget);
qDebug()<<ui->comboBox->currentText();
ui->pushButton->setText("关闭摄像头");
m_bCameraOpen = true;
ui->widget->show();
}else{
m_bCameraOpen = false;
ui->pushButton->setText("打开摄像头");
}
}

View File

@ -2,6 +2,8 @@
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QMainWindow> #include <QMainWindow>
#include "media/CameraCapture.h"
#include "cplaywidget.h"
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -15,8 +17,15 @@ public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
private slots:
void on_pushButton_clicked();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
Camera *mCamera;
QStringList mCameraList;
bool m_bCameraOpen;
CPlayWidget *mPlayerWidget;
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@ -6,31 +6,106 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1208</width> <width>1383</width>
<height>943</height> <height>1116</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>MainWindow</string>
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
<widget class="CPlayWidget" name="widget" native="true"> <property name="sizePolicy">
<property name="geometry"> <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<rect> <horstretch>0</horstretch>
<x>19</x> <verstretch>0</verstretch>
<y>20</y> </sizepolicy>
<width>1171</width> </property>
<height>851</height> <layout class="QVBoxLayout" name="verticalLayout_2">
</rect> <item>
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,10">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,2,13">
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item>
<widget class="QPushButton" name="pushButton">
<property name="minimumSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="sizeIncrement">
<size>
<width>0</width>
<height>6</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>打开摄像头</string>
</property> </property>
</widget> </widget>
</item>
<item>
<widget class="QComboBox" name="comboBox">
<property name="minimumSize">
<size>
<width>0</width>
<height>35</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="CPlayWidget" name="widget" native="true"/>
</item>
</layout>
</item>
</layout>
</widget> </widget>
<widget class="QMenuBar" name="menuBar"> <widget class="QMenuBar" name="menuBar">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1208</width> <width>1383</width>
<height>26</height> <height>26</height>
</rect> </rect>
</property> </property>

View File

@ -4,10 +4,10 @@
#pragma comment(lib, "strmiids") #pragma comment(lib, "strmiids")
//define release maco //define release maco
#define ReleaseInterface(x) \ #define ReleaseInterface(x) \
if ( NULL != x ) \ if ( nullptr != x ) \
{ \ { \
x->Release( ); \ x->Release( ); \
x = NULL; \ x = nullptr; \
} }
// Application-defined message to notify app of filter graph events // Application-defined message to notify app of filter graph events
#define WM_GRAPHNOTIFY WM_APP+100 #define WM_GRAPHNOTIFY WM_APP+100
@ -16,19 +16,32 @@ Camera::Camera():
mInitOK(false), mInitOK(false),
mVideoHeight(0), mVideoHeight(0),
mVideoWidth(0), mVideoWidth(0),
mDevFilter(NULL), mDevFilter(nullptr),
mCaptureGB(NULL), mCaptureGB(nullptr),
mGraphBuilder(NULL), mGraphBuilder(nullptr),
mMediaControl(NULL), mMediaControl(nullptr),
mMediaEvent(NULL), mMediaEvent(nullptr),
mSampGrabber(NULL), mSampGrabber(nullptr),
mIsVideoOpened(false), mIsVideoOpened(false),
mDebug(false) mDebug(false)
{ {
//HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
//if (SUCCEEDED(hr)) }
{
mInitOK = true; Camera::Camera(wstring camera)
{
mInitOK = false;
mVideoHeight = 0;
mVideoWidth = 0;
mDevFilter = nullptr;
mCaptureGB = nullptr;
mGraphBuilder = nullptr;
mMediaControl = nullptr;
mMediaEvent = nullptr;
mSampGrabber = nullptr;
mIsVideoOpened = false;
if(!this->Open(camera)){
mStatus = FAIL;
} }
mStatus = STOP; mStatus = STOP;
} }
@ -43,13 +56,13 @@ HRESULT Camera::InitializeEnv() {
HRESULT hr; HRESULT hr;
//Create the filter graph //Create the filter graph
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, hr = CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (LPVOID*)&mGraphBuilder); IID_IGraphBuilder, (LPVOID*)&mGraphBuilder);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
//Create the capture graph builder //Create the capture graph builder
hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, nullptr, CLSCTX_INPROC_SERVER,
IID_ICaptureGraphBuilder2, (LPVOID*)&mCaptureGB); IID_ICaptureGraphBuilder2, (LPVOID*)&mCaptureGB);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -71,14 +84,12 @@ HRESULT Camera::InitializeEnv() {
} }
std::vector<std::wstring> Camera::EnumAllCamera(void) { std::vector<std::wstring> Camera::EnumAllCamera(void) {
if (mInitOK == false)
return std::vector<std::wstring>();
std::vector<std::wstring> names; std::vector<std::wstring> names;
IEnumMoniker *pEnum = nullptr; IEnumMoniker *pEnum = nullptr;
// Create the System Device Enumerator. // Create the System Device Enumerator.
ICreateDevEnum *pDevEnum; ICreateDevEnum *pDevEnum;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum));
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
@ -95,12 +106,12 @@ std::vector<std::wstring> Camera::EnumAllCamera(void) {
if (!SUCCEEDED(hr)) if (!SUCCEEDED(hr))
return std::vector<std::wstring>(); return std::vector<std::wstring>();
IMoniker *pMoniker = NULL; IMoniker *pMoniker = nullptr;
while (pEnum->Next(1, &pMoniker, NULL) == S_OK) while (pEnum->Next(1, &pMoniker, nullptr) == S_OK)
{ {
IPropertyBag *pPropBag; IPropertyBag *pPropBag;
IBindCtx* bindCtx = NULL; IBindCtx* bindCtx = nullptr;
LPOLESTR str = NULL; LPOLESTR str = nullptr;
VARIANT var; VARIANT var;
VariantInit(&var); VariantInit(&var);
@ -137,7 +148,7 @@ HRESULT Camera::BindFilter(int deviceID, IBaseFilter **pBaseFilter) {
ICreateDevEnum *pDevEnum; ICreateDevEnum *pDevEnum;
IEnumMoniker *pEnumMon; IEnumMoniker *pEnumMon;
IMoniker *pMoniker; IMoniker *pMoniker;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (LPVOID*)&pDevEnum); IID_ICreateDevEnum, (LPVOID*)&pDevEnum);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
@ -185,12 +196,14 @@ int Camera::SampleGrabberCallback::SetObserver(CameraObserver *p) {
return -1; return -1;
mMux.lock(); mMux.lock();
for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) { for (auto itr = this->mObserver.begin(); itr != mObserver.end(); itr++) {
if (p == *itr) return 0; if (p == *itr) {
mMux.unlock();
return 0;
}
} }
this->mObserver.push_back(p); this->mObserver.push_back(p);
mMux.unlock(); mMux.unlock();
return 0; return 0;
return 0;
} }
int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p) int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p)
@ -213,14 +226,9 @@ int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p)
bool Camera::Open(std::wstring &camera_name) bool Camera::Open(std::wstring &camera_name)
{ {
if (mInitOK == false)
return false;
if (mIsVideoOpened) if (mIsVideoOpened)
return true; return true;
HRESULT hr; HRESULT hr;
#define CHECK_HR(x) do{ hr = (x); if (FAILED(hr)){ Close(); return false;}}while(0) #define CHECK_HR(x) do{ hr = (x); if (FAILED(hr)){ Close(); return false;}}while(0)
CHECK_HR(InitializeEnv()); CHECK_HR(InitializeEnv());
@ -246,7 +254,7 @@ bool Camera::Open(std::wstring &camera_name)
return false; return false;
} }
// create grabber filter instance // create grabber filter instance
CHECK_HR(CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, CHECK_HR(CoCreateInstance(CLSID_SampleGrabber, nullptr, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (LPVOID*)&pSampleGrabberFilter)); IID_IBaseFilter, (LPVOID*)&pSampleGrabberFilter));
// bind source device // bind source device
@ -262,9 +270,9 @@ bool Camera::Open(std::wstring &camera_name)
CHECK_HR(pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&mSampGrabber)); CHECK_HR(pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&mSampGrabber));
// find the current bit depth // find the current bit depth
HDC hdc = GetDC(NULL); HDC hdc = GetDC(nullptr);
mBitDepth = GetDeviceCaps(hdc, BITSPIXEL); mBitDepth = GetDeviceCaps(hdc, BITSPIXEL);
ReleaseDC(NULL, hdc); ReleaseDC(nullptr, hdc);
// set the media type for grabber filter // set the media type for grabber filter
AM_MEDIA_TYPE mediaType; AM_MEDIA_TYPE mediaType;
@ -291,8 +299,8 @@ bool Camera::Open(std::wstring &camera_name)
mediaType.formattype = FORMAT_VideoInfo; mediaType.formattype = FORMAT_VideoInfo;
hr = mSampGrabber->SetMediaType(&mediaType); hr = mSampGrabber->SetMediaType(&mediaType);
// 意味着最后的数据是丢掉的 // 意味着最后的数据是丢掉的
CHECK_HR(CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter))); CHECK_HR(CoCreateInstance(CLSID_NullRenderer, nullptr, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter)));
mGraphBuilder->AddFilter(dest_filter, L"NullRenderer"); mGraphBuilder->AddFilter(dest_filter, L"nullptrRenderer");
// connect source filter to grabber filter // connect source filter to grabber filter
CHECK_HR(mCaptureGB->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, CHECK_HR(mCaptureGB->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
@ -329,12 +337,12 @@ bool Camera::Open(std::wstring &camera_name)
{ {
CoTaskMemFree((PVOID)mediaType.pbFormat); CoTaskMemFree((PVOID)mediaType.pbFormat);
mediaType.cbFormat = 0; mediaType.cbFormat = 0;
mediaType.pbFormat = NULL; mediaType.pbFormat = nullptr;
} }
if (mediaType.pUnk != NULL) if (mediaType.pUnk != nullptr)
{ {
mediaType.pUnk->Release(); mediaType.pUnk->Release();
mediaType.pUnk = NULL; mediaType.pUnk = nullptr;
} }
mIsVideoOpened = TRUE; mIsVideoOpened = TRUE;
mStatus = RUNNING; mStatus = RUNNING;
@ -375,7 +383,7 @@ ULONG STDMETHODCALLTYPE Camera::SampleGrabberCallback::Release() {
} }
HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::QueryInterface(REFIID riid, void** ppvObject) { HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::QueryInterface(REFIID riid, void** ppvObject) {
if (NULL == ppvObject) return E_POINTER; if (nullptr == ppvObject) return E_POINTER;
if (riid == __uuidof(IUnknown)) if (riid == __uuidof(IUnknown))
{ {
*ppvObject = static_cast<IUnknown*>(this); *ppvObject = static_cast<IUnknown*>(this);
@ -395,12 +403,13 @@ HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::SampleCB(double Time, I
HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::BufferCB(double Time, BYTE * pBuffer, long BufferLen) HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::BufferCB(double Time, BYTE * pBuffer, long BufferLen)
{ {
//Debuger::Debug(L"receieve %d \r\n", BufferLen); #define DEBUG_CAMERA
#ifdef DEBUG_CAMERA #ifdef DEBUG_CAMERA
static FILE *p = fopen("camera_test.yuv","wb+"); static FILE *p = fopen("camera_test.yuv","wb+");
fwrite(pBuffer,BufferLen,1,p); fwrite(pBuffer,BufferLen,1,p);
fflush(p); fflush(p);
#endif #endif
if (mNewDataCallBack) if (mNewDataCallBack)
{ {
mNewDataCallBack(Time, pBuffer, BufferLen); mNewDataCallBack(Time, pBuffer, BufferLen);

View File

@ -22,12 +22,14 @@ public:
public: public:
virtual int OnCameraData(uint8_t *dat, uint32_t size) { return 0; }; virtual int OnCameraData(uint8_t *dat, uint32_t size) { return 0; };
}; };
private: Camera(wstring camera);
Camera();
Camera(const Camera &) = delete; Camera(const Camera &) = delete;
Camera& operator =(const Camera&) = delete; Camera& operator =(const Camera&) = delete;
~Camera(); ~Camera();
private:
Camera();
bool mInitOK; bool mInitOK;
bool mIsVideoOpened; bool mIsVideoOpened;
@ -66,15 +68,9 @@ public:
int SetObserver(CameraObserver *); int SetObserver(CameraObserver *);
int RemoveObserver(CameraObserver *p); int RemoveObserver(CameraObserver *p);
CAP_STATUS mStatus; CAP_STATUS mStatus;
static Camera *GetInstance(void)
{
static Camera *instance = nullptr;
if (nullptr == instance)
instance = new Camera();
return instance;
}
void SetDebug(bool); void SetDebug(bool);
std::vector<std::wstring> EnumAllCamera(void); static std::vector<std::wstring> EnumAllCamera(void);
GUID mPixFmt; GUID mPixFmt;
bool Open(std::wstring &camera_name); bool Open(std::wstring &camera_name);
bool Close(void); bool Close(void);