Skip to content

Commit

Permalink
fixed windows desktop duplication capturing.
Browse files Browse the repository at this point in the history
  • Loading branch information
smasherprog committed Dec 27, 2016
1 parent 3703390 commit 2a59141
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 43 deletions.
12 changes: 5 additions & 7 deletions Example/Screen_Capture_Example.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "windows\ScreenCaptureDX.h"
#include "ScreenCaptureManager.h"
#include <iostream>
#include <chrono>
#include <atomic>

#include <thread>
#include <string>
#define TJE_IMPLEMENTATION
#include "tiny_jpeg.h"
Expand All @@ -12,26 +12,24 @@ int main()

std::atomic<int> 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;
Expand Down
33 changes: 10 additions & 23 deletions src/ScreenCaptureManager.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
#include "ScreenCaptureManager.h"
#include <assert.h>
#include <algorithm>
#include <fstream>
#include <thread>
#include <mutex>
#include <deque>
#include <condition_variable>

#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();
}
};

Expand Down
26 changes: 13 additions & 13 deletions src/windows/DXFrameProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace SL {
if (Data->DirtyCount)
{
auto dirtyrects = reinterpret_cast<RECT*>(Data->MetaData.get() + (Data->MoveCount * sizeof(DXGI_OUTDUPL_MOVE_RECT)));

Ret = CopyDirty(Data->Frame.Get(), dirtyrects, Data->DirtyCount, DeskDesc);

D3D11_TEXTURE2D_DESC ThisDesc;
Expand Down Expand Up @@ -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);
Expand All @@ -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<char>(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
Expand Down Expand Up @@ -328,14 +328,14 @@ namespace SL {
}

// Set positions
Vertices[0].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast<FLOAT>(CenterX),
Vertices[0].Pos = XMFLOAT3((DestDirty.left- CenterX) / static_cast<FLOAT>(CenterX),
-1 * (DestDirty.bottom - CenterY) / static_cast<FLOAT>(CenterY),
0.0f);
Vertices[1].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast<FLOAT>(CenterX),
-1 * (DestDirty.top - CenterY) / static_cast<FLOAT>(CenterY),
Vertices[1].Pos = XMFLOAT3((DestDirty.left - CenterX) / static_cast<FLOAT>(CenterX),
-1 * (DestDirty.top - CenterY) / static_cast<FLOAT>(CenterY),
0.0f);
Vertices[2].Pos = XMFLOAT3((DestDirty.right - CenterX) / static_cast<FLOAT>(CenterX),
-1 * (DestDirty.bottom - CenterY) / static_cast<FLOAT>(CenterY),
Vertices[2].Pos = XMFLOAT3((DestDirty.right- CenterX) / static_cast<FLOAT>(CenterX),
-1 * (DestDirty.bottom - CenterY) / static_cast<FLOAT>(CenterY),
0.0f);
Vertices[3].Pos = Vertices[2].Pos;
Vertices[4].Pos = Vertices[1].Pos;
Expand Down

0 comments on commit 2a59141

Please sign in to comment.