diff --git a/src/frontend/qt_sdl/CameraManager.cpp b/src/frontend/qt_sdl/CameraManager.cpp index c7b4b813..82aaf277 100644 --- a/src/frontend/qt_sdl/CameraManager.cpp +++ b/src/frontend/qt_sdl/CameraManager.cpp @@ -21,6 +21,7 @@ CameraFrameDumper::CameraFrameDumper(QObject* parent) : QAbstractVideoSurface(parent) { + CamList.append((CameraManager*)parent); } bool CameraFrameDumper::present(const QVideoFrame& _frame) @@ -28,8 +29,11 @@ bool CameraFrameDumper::present(const QVideoFrame& _frame) QVideoFrame frame(_frame); if (!frame.map(QAbstractVideoBuffer::ReadOnly)) return false; -printf("FRAMEZORZ!! %d %d %d\n", frame.pixelFormat(), frame.isMapped(), frame.isReadable()); - //NDS::CamInputFrame(0, (u32*)frame.bits(), frame.width(), frame.height(), false); + if (!frame.isReadable()) + return false; + + for (CameraManager* cam : CamList) + cam->FeedFrame((u32*)frame.bits(), frame.width(), frame.height(), frame.pixelFormat() == QVideoFrame::Format_YUYV); frame.unmap(); @@ -47,10 +51,14 @@ QList CameraFrameDumper::supportedPixelFormats(QAbstra } -CameraManager::CameraManager(int num, int width, int height, bool yuv) +CameraManager::CameraManager(int num, int width, int height, bool yuv) : QObject() { Num = num; + // QCamera needs to be controlled from the UI thread, hence this + connect(this, SIGNAL(CamStartSignal()), this, SLOT(CamStart())); + connect(this, SIGNAL(CamStopSignal()), this, SLOT(CamStop())); + FrameWidth = width; FrameHeight = height; FrameFormatYUV = yuv; @@ -81,6 +89,12 @@ void CameraManager::Init() InputType = 0; InputType = 1; ImagePath = "test.jpg"; + if(Num==0) + { + InputType = 2; + const QList cameras = QCameraInfo::availableCameras(); + CamDeviceName = cameras[0].deviceName(); + } { // fill the framebuffer with black @@ -118,21 +132,68 @@ void CameraManager::Init() } } } + else if (InputType == 2) + { + // physical camera + + CamDevice = new QCamera(CamDeviceName.toUtf8()); + CamDumper = new CameraFrameDumper(this); + CamDevice->setViewfinder(CamDumper); + + /*CamDevice->load(); + QCameraViewfinderSettings settings; + + auto resolutions = CamDevice->supportedViewfinderResolutions(); + for (auto& res : resolutions) + { + printf("RESOLUTION: %d x %d\n", res.width(), res.height()); + } + + CamDevice->unload();*/ + + QCameraViewfinderSettings settings; + settings.setResolution(640, 480); + settings.setPixelFormat(QVideoFrame::Format_YUYV); + CamDevice->setViewfinderSettings(settings); + } } void CameraManager::DeInit() { + if (InputType == 2) + { + CamDevice->stop(); + delete CamDevice; + delete CamDumper; + } + InputType = -1; } void CameraManager::Start() { - // + if (InputType == 2) + { + emit CamStartSignal(); + } } void CameraManager::Stop() { - // + if (InputType == 2) + { + emit CamStopSignal(); + } +} + +void CameraManager::CamStart() +{ + CamDevice->start(); +} + +void CameraManager::CamStop() +{ + CamDevice->stop(); } void CameraManager::CaptureFrame(u32* frame, int width, int height, bool yuv) @@ -147,7 +208,55 @@ void CameraManager::CaptureFrame(u32* frame, int width, int height, bool yuv) } else { - // + if (yuv == FrameFormatYUV) + { + CopyFrame_Straight(FrameBuffer, FrameWidth, FrameHeight, + frame, width, height, + yuv); + } + else if (yuv) + { + CopyFrame_RGBtoYUV(FrameBuffer, FrameWidth, FrameHeight, + frame, width, height); + } + else + { + CopyFrame_YUVtoRGB(FrameBuffer, FrameWidth, FrameHeight, + frame, width, height); + } + } + + FrameMutex.unlock(); +} + +void CameraManager::FeedFrame(u32* frame, int width, int height, bool yuv) +{ + FrameMutex.lock(); + + if (width == FrameWidth && height == FrameHeight && yuv == FrameFormatYUV) + { + int len = width * height; + if (yuv) len /= 2; + memcpy(FrameBuffer, frame, len * sizeof(u32)); + } + else + { + if (yuv == FrameFormatYUV) + { + CopyFrame_Straight(frame, width, height, + FrameBuffer, FrameWidth, FrameHeight, + yuv); + } + else if (yuv) + { + CopyFrame_RGBtoYUV(frame, width, height, + FrameBuffer, FrameWidth, FrameHeight); + } + else + { + CopyFrame_YUVtoRGB(frame, width, height, + FrameBuffer, FrameWidth, FrameHeight); + } } FrameMutex.unlock(); diff --git a/src/frontend/qt_sdl/CameraManager.h b/src/frontend/qt_sdl/CameraManager.h index 81ed9523..3b26d9cb 100644 --- a/src/frontend/qt_sdl/CameraManager.h +++ b/src/frontend/qt_sdl/CameraManager.h @@ -27,6 +27,8 @@ #include "types.h" +class CameraManager; + class CameraFrameDumper : public QAbstractVideoSurface { Q_OBJECT @@ -36,10 +38,15 @@ public: bool present(const QVideoFrame& frame) override; QList supportedPixelFormats(QAbstractVideoBuffer::HandleType type = QAbstractVideoBuffer::NoHandle) const override; + +private: + QList CamList; }; -class CameraManager +class CameraManager : public QObject { + Q_OBJECT + public: CameraManager(int num, int width, int height, bool yuv); ~CameraManager(); @@ -52,6 +59,16 @@ public: void CaptureFrame(u32* frame, int width, int height, bool yuv); + void FeedFrame(u32* frame, int width, int height, bool yuv); + +signals: + void CamStartSignal(); + void CamStopSignal(); + +private slots: + void CamStart(); + void CamStop(); + private: int Num; @@ -59,6 +76,9 @@ private: QString ImagePath; QString CamDeviceName; + QCamera* CamDevice; + CameraFrameDumper* CamDumper; + int FrameWidth, FrameHeight; bool FrameFormatYUV; u32* FrameBuffer; diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index b9f676ee..213a09da 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -1770,38 +1770,6 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) const QList cameras = QCameraInfo::availableCameras(); for (const QCameraInfo &cameraInfo : cameras) printf("CAMERAFAZIL: %s\n", cameraInfo.deviceName().toStdString().c_str()); - QCamera* camera = new QCamera(cameras[0]); - CameraFrameDumper* dumper = new CameraFrameDumper(); - //QCameraViewfinder* derp = new QCameraViewfinder(); - printf("PROAON\n"); - //camera->setCaptureMode(QCamera::CaptureVideo); - //camera->setCaptureMode(QCamera::CaptureViewfinder); - printf("PROOT\n"); - //camera->unload(); - camera->setViewfinder(dumper); - //camera->setViewfinder(derp); - //camera->load(); - - // if (status != QCamera::LoadedStatus || m_camera->state() == QCamera::ActiveState) { return - printf("STATUS %d STATE %d\n", camera->status(), camera->state()); - -printf("CHIASSE\n"); - /*QCameraViewfinderSettings settings; - auto zorp = camera->supportedViewfinderResolutions(settings); - for (auto& res : zorp) printf("RESOLUTION: %d x %d\n", res.width(), res.height()); - auto zarp = camera->supportedViewfinderPixelFormats(settings); - for (auto& pf : zarp) printf("PIXEL FORMAT: %d\n", pf); - - settings.setResolution(640, 480); - //settings.setPixelFormat(QVideoFrame::Format_RGB32); - settings.setPixelFormat(QVideoFrame::Format_YUYV); - printf("PRALIPET\n"); - camera->setViewfinderSettings(settings);*/ -printf("PROULON\n"); - //dumper->start(); - //QVideoSurfaceFormat blarf(QSize(640,480), QVideoFrame::Format_RGB32); - //dumper->start(blarf); - camera->start(); } MainWindow::~MainWindow()