教学、会议、展厅大并发无线互联网直播同屏实现之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

相关文章

性能测试介绍

性能测试是一种测试方法,旨在评估系统、应用程序或组件在现实场景中的性能表现和可靠性。它通常用于衡量系统在不同负载条件下的响应时间、吞吐量、资源利用率、稳定性和可扩展性等关键指标。 为什么要进行性能测试 通过性能测试,可以确定系统是否能够满足预期的性能要求,找出性能瓶颈和潜在的问题,并进行优化和调整。 发现性能瓶颈:性能测试可以帮助发现系统的性能瓶颈,即系统在高负载或高并发情况下可能出现的问题

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

内核启动时减少log的方式

内核引导选项 内核引导选项大体上可以分为两类:一类与设备无关、另一类与设备有关。与设备有关的引导选项多如牛毛,需要你自己阅读内核中的相应驱动程序源码以获取其能够接受的引导选项。比如,如果你想知道可以向 AHA1542 SCSI 驱动程序传递哪些引导选项,那么就查看 drivers/scsi/aha1542.c 文件,一般在前面 100 行注释里就可以找到所接受的引导选项说明。大多数选项是通过"_

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount