diff --git a/client/qt_gl_render/yuvgl/cplaywidget.cpp b/client/qt_gl_render/yuvgl/cplaywidget.cpp index 5e9292c..a876fff 100644 --- a/client/qt_gl_render/yuvgl/cplaywidget.cpp +++ b/client/qt_gl_render/yuvgl/cplaywidget.cpp @@ -203,7 +203,7 @@ void CPlayWidget::initializeGL() if(this->mType == TYPE_RGB32){ 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) { diff --git a/client/qt_gl_render/yuvgl/main.cpp b/client/qt_gl_render/yuvgl/main.cpp index bd20a1b..76e3ebd 100644 --- a/client/qt_gl_render/yuvgl/main.cpp +++ b/client/qt_gl_render/yuvgl/main.cpp @@ -3,6 +3,7 @@ #include "cplaywidget.h" #include #include "CameraCapture.h" +#include "mainwindow.h" void CameraDataCallback(double ts, BYTE *dat, LONG size){ qDebug()<<"reieve cnt"< names = gCam->EnumAllCamera(); + Camera *gCam; + std::vector names = Camera::EnumAllCamera(); for(auto itr = names.begin();itr != names.end();itr++){ + if(gCam->Open(*names.begin()) ){ + Camera *gCam = new Camera(*names.begin()); + gCam->SetObserver(&gPlayer); + qDebug("open success"); + }else{ + qDebug("open fail"); + } qDebug()<SetObserver(&gPlayer); - if(gCam->Open(*names.begin()) ){ - qDebug("open success"); - }else{ - qDebug("open fail"); - - } +*/ return a.exec(); } diff --git a/client/qt_gl_render/yuvgl/mainwindow.cpp b/client/qt_gl_render/yuvgl/mainwindow.cpp index 49d64fc..ab5c092 100644 --- a/client/qt_gl_render/yuvgl/mainwindow.cpp +++ b/client/qt_gl_render/yuvgl/mainwindow.cpp @@ -3,12 +3,44 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + m_bCameraOpen(false), + mCamera(nullptr) { ui->setupUi(this); + std::vector 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; } + +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()<comboBox->currentText().size()<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()<comboBox->currentText(); + ui->pushButton->setText("关闭摄像头"); + m_bCameraOpen = true; + ui->widget->show(); + }else{ + m_bCameraOpen = false; + ui->pushButton->setText("打开摄像头"); + } +} diff --git a/client/qt_gl_render/yuvgl/mainwindow.h b/client/qt_gl_render/yuvgl/mainwindow.h index 9353441..86a9b40 100644 --- a/client/qt_gl_render/yuvgl/mainwindow.h +++ b/client/qt_gl_render/yuvgl/mainwindow.h @@ -2,6 +2,8 @@ #define MAINWINDOW_H #include +#include "media/CameraCapture.h" +#include "cplaywidget.h" namespace Ui { class MainWindow; @@ -15,8 +17,15 @@ public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); +private slots: + void on_pushButton_clicked(); + private: Ui::MainWindow *ui; + Camera *mCamera; + QStringList mCameraList; + bool m_bCameraOpen; + CPlayWidget *mPlayerWidget; }; #endif // MAINWINDOW_H diff --git a/client/qt_gl_render/yuvgl/mainwindow.ui b/client/qt_gl_render/yuvgl/mainwindow.ui index 5fa21e8..aacf4dd 100644 --- a/client/qt_gl_render/yuvgl/mainwindow.ui +++ b/client/qt_gl_render/yuvgl/mainwindow.ui @@ -6,31 +6,106 @@ 0 0 - 1208 - 943 + 1383 + 1116 MainWindow - - - - 19 - 20 - 1171 - 851 - - - + + + 0 + 0 + + + + + + + + + 2 + + + 2 + + + 2 + + + 1 + + + + + + 0 + 50 + + + + + 0 + 6 + + + + + 0 + 50 + + + + 打开摄像头 + + + + + + + + 0 + 35 + + + + + 0 + 50 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + 0 0 - 1208 + 1383 26 diff --git a/client/qt_gl_render/yuvgl/media/CameraCapture.cpp b/client/qt_gl_render/yuvgl/media/CameraCapture.cpp index 362c0f2..c18f2fd 100644 --- a/client/qt_gl_render/yuvgl/media/CameraCapture.cpp +++ b/client/qt_gl_render/yuvgl/media/CameraCapture.cpp @@ -4,10 +4,10 @@ #pragma comment(lib, "strmiids") //define release maco #define ReleaseInterface(x) \ - if ( NULL != x ) \ + if ( nullptr != x ) \ { \ x->Release( ); \ - x = NULL; \ + x = nullptr; \ } // Application-defined message to notify app of filter graph events #define WM_GRAPHNOTIFY WM_APP+100 @@ -16,21 +16,34 @@ Camera::Camera(): mInitOK(false), mVideoHeight(0), mVideoWidth(0), - mDevFilter(NULL), - mCaptureGB(NULL), - mGraphBuilder(NULL), - mMediaControl(NULL), - mMediaEvent(NULL), - mSampGrabber(NULL), + mDevFilter(nullptr), + mCaptureGB(nullptr), + mGraphBuilder(nullptr), + mMediaControl(nullptr), + mMediaEvent(nullptr), + mSampGrabber(nullptr), mIsVideoOpened(false), mDebug(false) { - //HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); - //if (SUCCEEDED(hr)) - { - mInitOK = true; - } - mStatus = STOP; + +} + +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; } Camera::~Camera() @@ -43,13 +56,13 @@ HRESULT Camera::InitializeEnv() { HRESULT hr; //Create the filter graph - hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, + hr = CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (LPVOID*)&mGraphBuilder); if (FAILED(hr)) return hr; //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); if (FAILED(hr)) return hr; @@ -71,14 +84,12 @@ HRESULT Camera::InitializeEnv() { } std::vector Camera::EnumAllCamera(void) { - if (mInitOK == false) - return std::vector(); std::vector names; - IEnumMoniker *pEnum = nullptr; + IEnumMoniker *pEnum = nullptr; // Create the System Device Enumerator. ICreateDevEnum *pDevEnum; - HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, + HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); if (SUCCEEDED(hr)) @@ -95,12 +106,12 @@ std::vector Camera::EnumAllCamera(void) { if (!SUCCEEDED(hr)) return std::vector(); - IMoniker *pMoniker = NULL; - while (pEnum->Next(1, &pMoniker, NULL) == S_OK) + IMoniker *pMoniker = nullptr; + while (pEnum->Next(1, &pMoniker, nullptr) == S_OK) { IPropertyBag *pPropBag; - IBindCtx* bindCtx = NULL; - LPOLESTR str = NULL; + IBindCtx* bindCtx = nullptr; + LPOLESTR str = nullptr; VARIANT var; VariantInit(&var); @@ -137,7 +148,7 @@ HRESULT Camera::BindFilter(int deviceID, IBaseFilter **pBaseFilter) { ICreateDevEnum *pDevEnum; IEnumMoniker *pEnumMon; IMoniker *pMoniker; - HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, + HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, nullptr, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (LPVOID*)&pDevEnum); if (SUCCEEDED(hr)) { @@ -181,16 +192,18 @@ void Camera::SetDebug(bool isDebug) { } int Camera::SampleGrabberCallback::SetObserver(CameraObserver *p) { - if (nullptr == p) + if (nullptr == p) return -1; mMux.lock(); 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); mMux.unlock(); return 0; - return 0; } int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p) @@ -213,14 +226,9 @@ int Camera::SampleGrabberCallback::RemoveObserver(CameraObserver * p) bool Camera::Open(std::wstring &camera_name) { - if (mInitOK == false) - return false; - if (mIsVideoOpened) return true; - HRESULT hr; - #define CHECK_HR(x) do{ hr = (x); if (FAILED(hr)){ Close(); return false;}}while(0) CHECK_HR(InitializeEnv()); @@ -246,7 +254,7 @@ bool Camera::Open(std::wstring &camera_name) return false; } // 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)); // bind source device @@ -262,9 +270,9 @@ bool Camera::Open(std::wstring &camera_name) CHECK_HR(pSampleGrabberFilter->QueryInterface(IID_ISampleGrabber, (LPVOID*)&mSampGrabber)); // find the current bit depth - HDC hdc = GetDC(NULL); + HDC hdc = GetDC(nullptr); mBitDepth = GetDeviceCaps(hdc, BITSPIXEL); - ReleaseDC(NULL, hdc); + ReleaseDC(nullptr, hdc); // set the media type for grabber filter AM_MEDIA_TYPE mediaType; @@ -291,8 +299,8 @@ bool Camera::Open(std::wstring &camera_name) mediaType.formattype = FORMAT_VideoInfo; hr = mSampGrabber->SetMediaType(&mediaType); // 意味着最后的数据是丢掉的 - CHECK_HR(CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter))); - mGraphBuilder->AddFilter(dest_filter, L"NullRenderer"); + CHECK_HR(CoCreateInstance(CLSID_NullRenderer, nullptr, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)(&dest_filter))); + mGraphBuilder->AddFilter(dest_filter, L"nullptrRenderer"); // connect source filter to grabber filter CHECK_HR(mCaptureGB->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, @@ -329,12 +337,12 @@ bool Camera::Open(std::wstring &camera_name) { CoTaskMemFree((PVOID)mediaType.pbFormat); mediaType.cbFormat = 0; - mediaType.pbFormat = NULL; + mediaType.pbFormat = nullptr; } - if (mediaType.pUnk != NULL) + if (mediaType.pUnk != nullptr) { mediaType.pUnk->Release(); - mediaType.pUnk = NULL; + mediaType.pUnk = nullptr; } mIsVideoOpened = TRUE; mStatus = RUNNING; @@ -348,7 +356,7 @@ bool Camera::Close() { } if (mMediaEvent) { - mMediaEvent->SetNotifyWindow(NULL, WM_GRAPHNOTIFY, 0); + mMediaEvent->SetNotifyWindow(NULL, WM_GRAPHNOTIFY, 0); } mIsVideoOpened = false; //release interface @@ -375,7 +383,7 @@ ULONG STDMETHODCALLTYPE Camera::SampleGrabberCallback::Release() { } 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)) { *ppvObject = static_cast(this); @@ -395,12 +403,13 @@ HRESULT STDMETHODCALLTYPE Camera::SampleGrabberCallback::SampleCB(double Time, I 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 static FILE *p = fopen("camera_test.yuv","wb+"); fwrite(pBuffer,BufferLen,1,p); fflush(p); #endif + if (mNewDataCallBack) { mNewDataCallBack(Time, pBuffer, BufferLen); diff --git a/client/qt_gl_render/yuvgl/media/CameraCapture.h b/client/qt_gl_render/yuvgl/media/CameraCapture.h index 679bd57..d0d0f8b 100644 --- a/client/qt_gl_render/yuvgl/media/CameraCapture.h +++ b/client/qt_gl_render/yuvgl/media/CameraCapture.h @@ -22,11 +22,13 @@ public: public: virtual int OnCameraData(uint8_t *dat, uint32_t size) { return 0; }; }; + Camera(wstring camera); + Camera(const Camera &) = delete; + Camera& operator =(const Camera&) = delete; + ~Camera(); + private: - Camera(); - Camera(const Camera &) = delete; - Camera& operator =(const Camera&) = delete; - ~Camera(); + Camera(); bool mInitOK; bool mIsVideoOpened; @@ -66,15 +68,9 @@ public: int SetObserver(CameraObserver *); int RemoveObserver(CameraObserver *p); CAP_STATUS mStatus; - static Camera *GetInstance(void) - { - static Camera *instance = nullptr; - if (nullptr == instance) - instance = new Camera(); - return instance; - } + void SetDebug(bool); - std::vector EnumAllCamera(void); + static std::vector EnumAllCamera(void); GUID mPixFmt; bool Open(std::wstring &camera_name); bool Close(void);