windows使用webrtc截取桌面

#include "../webrtc/modules/desktop_capture/shared_desktop_frame.h"
#include "../webrtc/modules/desktop_capture/desktop_capturer.h"
#include "../webrtc/modules/desktop_capture/cropping_window_capturer.h"
#include "../webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "../webrtc/base/logging.h"
#include "../webrtc/base/profiler.h"

#include <iostream>

#ifdef _WIN32
#include <Windows.h>
#pragma comment(lib,"d3d11.lib") 
#endif

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace std;

class CapCallback : public webrtc::DesktopCapturer::Callback {
public:
	void OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame){
		
		webrtc::DesktopFrame* df = frame.get();
		LOG(INFO) << "Get Frame width:" << df->size().width() << " width:" << df->size().height();
		CvSize cs;
		cs.width = df->size().width();
		cs.height = df->size().height();
		IplImage* pImage = cvCreateImage(cs, 8, 4);
		memcpy(pImage->imageData, df->data(), cs.width*cs.height * 4);
		cvShowImage("test", pImage);
		cvWaitKey(10);
		cvReleaseImage(&pImage);
		pImage = NULL;
	}

protected:
	virtual ~CapCallback() {
		LOG(INFO) << "Destructure";
	}
};

int main(int argc, char* argv[])
{
	cout << "test desktop" << endl;
	LOG(INFO) << "test capture";
	webrtc::DesktopCaptureOptions opts = webrtc::DesktopCaptureOptions::CreateDefault();
	opts.set_allow_directx_capturer(true);
	webrtc::WindowCapturer* pCap = webrtc::CroppingWindowCapturer::Create(opts);
	webrtc::DesktopRegion dregion;
	/*webrtc::DesktopSize ds;
	ds.set(100, 100);
	webrtc::DesktopRect drect = webrtc::DesktopRect::MakeSize(ds);
	dregion.SetRect(drect);*/
	webrtc::WindowId wid = (webrtc::WindowId)GetDesktopWindow();
	pCap->SelectWindow(wid);
	CapCallback* ccb = new CapCallback;
	pCap->Start(ccb);
	
	cout << "test desktop start" << endl;
	while (1)
	{
		pCap->Capture(dregion);
		Sleep(1);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章