From 2a59141f076eabfd6ea317f696551c91c37e2972 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 26 Dec 2016 21:04:23 -0800 Subject: [PATCH] fixed windows desktop duplication capturing. --- Example/Screen_Capture_Example.cpp | 12 +++++------ src/ScreenCaptureManager.cpp | 33 +++++++++--------------------- src/windows/DXFrameProcessor.cpp | 26 +++++++++++------------ 3 files changed, 28 insertions(+), 43 deletions(-) diff --git a/Example/Screen_Capture_Example.cpp b/Example/Screen_Capture_Example.cpp index 9138cde2..e46bfebc 100644 --- a/Example/Screen_Capture_Example.cpp +++ b/Example/Screen_Capture_Example.cpp @@ -1,8 +1,8 @@ -#include "windows\ScreenCaptureDX.h" +#include "ScreenCaptureManager.h" #include #include #include - +#include #include #define TJE_IMPLEMENTATION #include "tiny_jpeg.h" @@ -12,26 +12,24 @@ int main() std::atomic realcounter; realcounter = 0; - SL::Screen_Capture::ScreenCaptureDX dx; + SL::Screen_Capture::ScreenCaptureManager framgrabber; SL::Screen_Capture::ImageCallback func = [&](const SL::Screen_Capture::CapturedImage& img) { - if (img.ScreenIndex != 1) return; std::cout << "Height " << img.Height << ", Width " << img.Width << ", Top " << img.AbsoluteTop << ", Left " << img.AbsoluteLeft << std::endl; auto r = realcounter.fetch_add(1); std::string s("screen"); s += std::to_string(img.AbsoluteTop) + std::string(",") + std::to_string(img.AbsoluteLeft) +std::string(" ") + std::to_string(r) + std::string(".jpg"); - //savetodisk(img, s); if (!tje_encode_to_file(s.c_str(), img.Width, img.Height, 4, (const unsigned char*)img.Data.get())) { std::cout << "Could not write JPEG\n"; } }; - dx.StartProcessing(func); + framgrabber.StartCapturing(func, 100); while (true) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } - dx.StopProcessing(); + framgrabber.StopCapturing(); int k = 0; std::cin >> k; return 0; diff --git a/src/ScreenCaptureManager.cpp b/src/ScreenCaptureManager.cpp index 77e491ef..a151327a 100644 --- a/src/ScreenCaptureManager.cpp +++ b/src/ScreenCaptureManager.cpp @@ -1,46 +1,33 @@ #include "ScreenCaptureManager.h" -#include -#include -#include #include -#include -#include -#include +#ifdef WIN32 +#include "ScreenCaptureDX.h" +#endif // DEBUG namespace SL { namespace Screen_Capture { class ScreenCaptureManagerImpl { - void run() { - while (keeprunning) { - - - } - } +#ifdef WIN32 + SL::Screen_Capture::ScreenCaptureDX FrameGrabber; +#endif public: - std::thread thread; - int sleeptime = 100;//in ms ImageCallback callback; - bool keeprunning = true; ScreenCaptureManagerImpl() { - - + } ~ScreenCaptureManagerImpl() { - + stop(); } void start() { - thread = std::thread(&SL::Screen_Capture::ScreenCaptureManagerImpl::run, this); + FrameGrabber.StartProcessing(callback); } void stop() { - if (thread.joinable()) { - - thread.join(); - } + FrameGrabber.StopProcessing(); } }; diff --git a/src/windows/DXFrameProcessor.cpp b/src/windows/DXFrameProcessor.cpp index 309a7f86..a391d065 100644 --- a/src/windows/DXFrameProcessor.cpp +++ b/src/windows/DXFrameProcessor.cpp @@ -60,7 +60,7 @@ namespace SL { if (Data->DirtyCount) { auto dirtyrects = reinterpret_cast(Data->MetaData.get() + (Data->MoveCount * sizeof(DXGI_OUTDUPL_MOVE_RECT))); - + Ret = CopyDirty(Data->Frame.Get(), dirtyrects, Data->DirtyCount, DeskDesc); D3D11_TEXTURE2D_DESC ThisDesc; @@ -100,6 +100,7 @@ namespace SL { return ProcessFailure(m_Device.Get(), L"DrawSurface_GetPixelColor: Could not read the pixel color because the mapped subresource returned NULL", L"Error", hr, SystemTransitionsExpectedErrors); } else { + CapturedImage img; img.ScreenIndex = Data->SrcreenIndex; img.Height = (Box.bottom - Box.top); @@ -110,15 +111,14 @@ namespace SL { img.AbsoluteTop = DeskDesc->DesktopCoordinates.top + Box.top; auto sizeneeded = 4 * img.Height *img.Width; img.Data = std::shared_ptr(new char[sizeneeded], [](char* ptr) { if (ptr) delete[] ptr; }); - auto startsrc = img.Data.get(); - auto dststart = (char*)MappingDesc.pData + (Box.left * img.PixelStride); + auto dststart = img.Data.get(); + auto srcstart = ((char*)MappingDesc.pData) + (Box.top * MappingDesc.RowPitch) + (Box.left * img.PixelStride); - for (auto t = Box.top; t < img.Height; t++) { - memcpy(startsrc, dststart, img.Width * img.PixelStride); - startsrc += img.Width * img.PixelStride; - dststart += MappingDesc.RowPitch; + for (auto t = Box.top; t < Box.bottom; t++) { + memcpy(dststart, srcstart, img.Width * img.PixelStride); + dststart += img.Width * img.PixelStride; + srcstart += MappingDesc.RowPitch; } - CallBack(img); } // Unlock the memory @@ -328,14 +328,14 @@ namespace SL { } // Set positions - Vertices[0].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast(CenterX), + Vertices[0].Pos = XMFLOAT3((DestDirty.left- CenterX) / static_cast(CenterX), -1 * (DestDirty.bottom - CenterY) / static_cast(CenterY), 0.0f); - Vertices[1].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast(CenterX), - -1 * (DestDirty.top - CenterY) / static_cast(CenterY), + Vertices[1].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast(CenterX), + -1 * (DestDirty.top - CenterY) / static_cast(CenterY), 0.0f); - Vertices[2].Pos = XMFLOAT3((DestDirty.right - CenterX) / static_cast(CenterX), - -1 * (DestDirty.bottom - CenterY) / static_cast(CenterY), + Vertices[2].Pos = XMFLOAT3((DestDirty.right- CenterX) / static_cast(CenterX), + -1 * (DestDirty.bottom - CenterY) / static_cast(CenterY), 0.0f); Vertices[3].Pos = Vertices[2].Pos; Vertices[4].Pos = Vertices[1].Pos;