教学、会议、展厅大并发无线互联网直播同屏实现之LibEasyScreenLive通过GDI方式实现屏幕捕获采集方法介绍

本文主要是介绍教学、会议、展厅大并发无线互联网直播同屏实现之LibEasyScreenLive通过GDI方式实现屏幕捕获采集方法介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

EasyScreenLive功能介绍

EasyScreenLive是一款简单、高效、稳定的集采集,编码,组播,推流和流媒体RTSP服务于一身的同屏功能组件,具低延时,高效能,低丢包等特点。目前支持Windows,Android平台,通过EasyScreenLive我们就可以避免接触到稍显复杂的音视频源采集,编码和流媒体推送以及RTSP/RTP/RTCP/RTMP服务流程,只需要调用EasyScreenLive的几个API接口,就能轻松、稳定地把流媒体音视频数据RTMP推送给EasyDSS服务器以及发布RTSPServer服务, RTSP同屏服务支持组播和单播两种模式。

EasyScreenLive

LibEasyScreenLive通过GDI方式实现屏幕捕获采集

windows端最通用的屏幕捕获方式就是通过GDI(图形设备接口)获取桌面的设备上下文DC,然后将其内容转换为RGB位图,从而转换成视频帧实现屏幕的采集,GDI是一种微软较为古老的技术,其采集效率相对较低,不过实现30fps帧率的桌面采集还是绰绰有余的,而且在设备性能较差或者操作系统版本较低的系统上也能兼容。

LibEasyScreenLive通过GDI方式实现屏幕捕获采集的实现主要是通过CCaptureScreen类实现,该类声明如下:

class CCaptureScreen
{
public:CCaptureScreen(void);~CCaptureScreen(void);public://Interface Functionint SetCaptureCursor(bool bShow);int Init(HWND hShowWnd);void UnInit();int StartScreenCapture(int nCapMode = 2, int fps = 25);void StopScreenCapture();void GetCaptureScreenSize(int& nWidth, int& nHeight );//设置捕获数据回调函数void SetCaptureScreenCallback(CaptureScreenCallback callBack, void * pMaster);BOOL IsInCapturing(){return m_bCapturing;}public:
////屏幕捕获帧主函数//Use these 2 functions to create frames and free framesvoid* CaptureScreenFrame(int left,int top,int width, int height,int tempDisableRect);void FreeFrame(void*) ;void InsertHighLight(HDC hdc,int xoffset, int yoffset);//inner call functionBOOL isRectEqual(RECT a, RECT b);void SaveBitmapCopy(HDC hdc,HDC hdcbits, int x, int y, int sx, int sy);void RestoreBitmapCopy(HDC hdc,HDC hdcbits, int x, int y, int sx, int sy) ;void DeleteBitmapCopy();void NormalizeRect(LPRECT prc);void FixRectSizePos(LPRECT prc,int maxxScreen, int maxyScreen);//Mouse Capture functions HCURSOR FetchCursorHandle();HANDLE Bitmap2Dib(HBITMAP, UINT);//创建线程进行屏幕捕获int CreateCaptureScreenThread();static UINT WINAPI CaptureScreenThread(LPVOID pParam);void CaptureVideoProcess();
};

主要桌面采集流程在线程CaptureScreenThread中实现,采集线程执行体函数实现代码如下:

void CCaptureScreen::CaptureVideoProcess()
{int top=m_rcUse.top;int left=m_rcUse.left;int width=m_rcUse.right-m_rcUse.left+1;int height=m_rcUse.bottom - m_rcUse.top + 1;//  [1/27/2016 SwordTwelve]//长宽做一下修正,修正为16的倍数int nDivW = width%16;int nDivH = height%16;if (nDivW<8)width -= nDivW;elsewidth += (16 - nDivW);if (nDivH<8)height -= nDivH;elseheight += (16 - nDivH);if (width>m_nMaxxScreen){width = m_nMaxxScreen;}if (height>m_nMaxyScreen){height = m_nMaxyScreen;}//设置捕获帧率int nFps = m_nFps;/*LPBITMAPINFOHEADER*/VOID*  alpbi = NULL;RECT panrect_current;RECT panrect_dest;if (m_bAutopan) {panrect_current.left = left;panrect_current.top = top;panrect_current.right = left + width - 1;panrect_current.bottom = top + height - 1;}_LARGE_INTEGER	nowTime;_LARGE_INTEGER	lastTime;LARGE_INTEGER	cpuFreq;		//cpu频率timeBeginPeriod(1);QueryPerformanceFrequency(&cpuFreq);timeEndPeriod(1);//Into Capture Loopwhile (m_bCapturing){timeBeginPeriod(1);QueryPerformanceCounter(&lastTime);timeEndPeriod(1);//Autopanif ((m_bAutopan) && (width < m_nMaxxScreen) && (height < m_nMaxyScreen)){POINT xPoint;GetCursorPos(&xPoint);int extleft = ((panrect_current.right - panrect_current.left)*1)/4 + panrect_current.left;int extright = ((panrect_current.right - panrect_current.left)*3)/4 + panrect_current.left;int exttop = ((panrect_current.bottom - panrect_current.top)*1)/4 + panrect_current.top;int extbottom = ((panrect_current.bottom - panrect_current.top)*3)/4 + panrect_current.top;				if (xPoint.x  < extleft )  //need to pan left{panrect_dest.left = xPoint.x - width/2;panrect_dest.right = panrect_dest.left +  width - 1;if (panrect_dest.left < 0) {panrect_dest.left = 0;panrect_dest.right = panrect_dest.left +  width - 1;}}else if (xPoint.x  > extright ) //need to pan right{ panrect_dest.left = xPoint.x - width/2;						panrect_dest.right = panrect_dest.left +  width - 1;if (panrect_dest.right >= m_nMaxxScreen){panrect_dest.right = m_nMaxxScreen - 1;panrect_dest.left  = panrect_dest.right - width + 1;	}}else {panrect_dest.right = panrect_current.right;panrect_dest.left  = panrect_current.left;}if (xPoint.y  < exttop )  //need to pan up{panrect_dest.top = xPoint.y - height/2;panrect_dest.bottom = panrect_dest.top +  height - 1;if (panrect_dest.top < 0) {panrect_dest.top = 0;panrect_dest.bottom = panrect_dest.top +  height - 1;}}else if (xPoint.y  > extbottom ) { //need to pan downpanrect_dest.top = xPoint.y - height/2;						panrect_dest.bottom = panrect_dest.top +  height - 1;if (panrect_dest.bottom >= m_nMaxyScreen){panrect_dest.bottom = m_nMaxyScreen - 1;panrect_dest.top  = panrect_dest.bottom - height + 1;	}}else {panrect_dest.top = panrect_current.top;panrect_dest.bottom  = panrect_current.bottom;}//Determine Pan Valuesint xdiff,ydiff;xdiff = panrect_dest.left - panrect_current.left;ydiff = panrect_dest.top - panrect_current.top;if (abs(xdiff) < m_nAutopanSpeed) {panrect_current.left += xdiff;}else{if (xdiff<0) panrect_current.left -= m_nAutopanSpeed;elsepanrect_current.left += m_nAutopanSpeed;}if (abs(ydiff) < m_nAutopanSpeed){panrect_current.top += ydiff;}else{if (ydiff<0) panrect_current.top -= m_nAutopanSpeed;elsepanrect_current.top += m_nAutopanSpeed;}				panrect_current.right = panrect_current.left + width - 1;panrect_current.bottom =  panrect_current.top + height - 1;alpbi=CaptureScreenFrame(panrect_current.left,panrect_current.top,width, height,0);					}elsealpbi=CaptureScreenFrame(left,top,width, height,0);	#if 1if (m_hMainWnd&&IsWindowVisible(m_hMainWnd)&&IsWindow(m_hMainWnd)){RECT rcClient;::GetClientRect(m_hMainWnd, &rcClient);if (rcClient.right-rcClient.left>0&&rcClient.bottom-rcClient.top>0){vdev_setrect(m_videoRender, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);// Capture Data callBack//测试显示void* buffer = NULL;int stride = 0;vdev_request (m_videoRender, &buffer, &stride);int swnew = ((VDEV_COMMON_CTXT*)m_videoRender)->sw;int shnew = ((VDEV_COMMON_CTXT*)m_videoRender)->sh;//memcpy(buffer, alpbi, width*height*3);vdev_convert(AV_PIX_FMT_BGR24, width, height, alpbi, AV_PIX_FMT_RGB32, swnew, shnew, (unsigned char**)&buffer);vdev_post (m_videoRender, clock());}}if (m_pCallback&&m_pMaster){ScreenCapDataInfo sCapScreenInfo;sCapScreenInfo.nWidth = width;sCapScreenInfo.nHeight = height;sCapScreenInfo.nDataType = 24;strcpy(sCapScreenInfo.strDataType, "RGB24");m_pCallback(m_nId, (unsigned char*)(alpbi), /*alpbi->biSizeImage*/m_dibBufferLen, 1, &sCapScreenInfo, m_pMaster);} 
#endiftimeBeginPeriod(1);QueryPerformanceCounter(&nowTime);timeEndPeriod(1);if (cpuFreq.QuadPart < 1)	cpuFreq.QuadPart = 1;int lInterval = (int)(((nowTime.QuadPart - lastTime.QuadPart) / (double)cpuFreq.QuadPart * (double)1000));//Slowly thread By framerateint nDelay = 1000/nFps - lInterval;#if 0char sMsg[64] = {0,};sprintf(sMsg, "lInterval = %d   nFps =%d  nDelay = %d\r\n", lInterval, nFps, nDelay);OutputDebugStringA( sMsg );
#endifif (nDelay>0){Sleep(nDelay);}}
}

其中,通过GDI获取屏幕DC并将其内容转换为RGB图像实现核心函数如下:

void* CCaptureScreen::CaptureScreenFrame(int left,int top,int width, int height,int tempDisableRect)
{//获取桌面屏幕设备DCHDC hScreenDC = ::GetDC(NULL);HDC hMemDC = ::CreateCompatibleDC(hScreenDC);     HBITMAP hbm;hbm = CreateCompatibleBitmap(hScreenDC, width, height);HBITMAP oldbm = (HBITMAP) SelectObject(hMemDC, hbm);	 BitBlt(hMemDC, 0, 0, width, height, hScreenDC, left, top, SRCCOPY);	 	//Get Cursor PosPOINT xPoint; GetCursorPos( &xPoint ); HCURSOR hcur= FetchCursorHandle();xPoint.x-=left;xPoint.y-=top;//Draw the HighLight	if (m_bHighlightCursor==1){	POINT highlightPoint; 		highlightPoint.x = xPoint.x -64 ;highlightPoint.y = xPoint.y -64 ;		InsertHighLight( hMemDC, highlightPoint.x, highlightPoint.y);}//Draw the Cursor	if (m_bRecordCursor==1){ICONINFO iconinfo ;	BOOL ret;ret	= GetIconInfo( hcur,  &iconinfo ); if (ret) {xPoint.x -= iconinfo.xHotspot;xPoint.y -= iconinfo.yHotspot;//need to delete the hbmMask and hbmColor bitmaps//otherwise the program will crash after a while after running out of resourceif (iconinfo.hbmMask) DeleteObject(iconinfo.hbmMask);if (iconinfo.hbmColor) DeleteObject(iconinfo.hbmColor);}		// 修正鼠标信息 [7/19/2018 SwordTwelve]	 ::DrawIcon( hMemDC,  xPoint.x*m_fScreenxScale,  xPoint.y*m_fScreenyScale, hcur); 							}SelectObject(hMemDC,oldbm);    			void* pBM_HEADER = /*(LPBITMAPINFOHEADER)*/(Bitmap2Dib(hbm, 24));	//m_nColorBits//LPBITMAPINFOHEADER pBM_HEADER = (LPBITMAPINFOHEADER)GlobalLock(Bitmap2Dib(hbm, 24));	if (pBM_HEADER == NULL) { return NULL;}    DeleteObject(hbm);			DeleteDC(hMemDC);	ReleaseDC(NULL,hScreenDC) ;	return pBM_HEADER;
}

这篇关于教学、会议、展厅大并发无线互联网直播同屏实现之LibEasyScreenLive通过GDI方式实现屏幕捕获采集方法介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/930899

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

Python使用国内镜像加速pip安装的方法讲解

《Python使用国内镜像加速pip安装的方法讲解》在Python开发中,pip是一个非常重要的工具,用于安装和管理Python的第三方库,然而,在国内使用pip安装依赖时,往往会因为网络问题而导致速... 目录一、pip 工具简介1. 什么是 pip?2. 什么是 -i 参数?二、国内镜像源的选择三、如何

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程