Browse Source

Z991239-6451 #comment 优化虚拟摄像头组件和终端实体通信功能

80274480 1 month ago
parent
commit
643d132b5a

+ 1 - 1
Module/mod_mediacontroller/capture.h

@@ -52,7 +52,7 @@ extern "C"{
 #endif // !RVC_MAX_DELAY_TIME
 
 #ifndef RVC_CAM_INTERVAL_TIME
-#define RVC_CAM_INTERVAL_TIME 3000
+#define RVC_CAM_INTERVAL_TIME 300
 #endif // !RVC_CAM_INTERVAL_TIME
 
 #ifndef AUDIO_BUG_THRESHOLD_TIME

+ 13 - 1
Module/mod_mediacontroller/mod_mediacontroller.cpp

@@ -272,9 +272,21 @@ bool CMediaControllerEntity::HandleVirtucalCamMsg(const char* strMsg)
 	if (strstr(strMsg, "Start")) {
 		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Start virtual Camera %s.", strMsg);
 		StartAllCameras();
+		bRet = true;
 	}
-	else {
+	else if (strstr(strMsg, "Stop")) {
 		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Stop virtual Camera %s.", strMsg);
+		CSimpleStringA strState;
+		GetFunction()->GetSysVar("UIState", strState);
+		if (strState.GetLength() > 0 && strState[0] == 'M') {
+			if (m_bStartCamera) {
+				DelayCloseCameras();
+			}
+		}
+		bRet = true;
+	}
+	else {
+		DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("virtual camera send message %s.", strMsg);
 	}
 
 	return bRet;

+ 143 - 109
Other/win/virtualcam/virtualcam.cpp

@@ -42,6 +42,129 @@
 #define BUFFER_SIZE 256 
 #endif
 
+enum eMsgType {
+	eStartCamera,
+	eCapProcess,
+	eStopCamera,
+	eMsgDefault
+};
+
+static char* GetCameraInfo(int mode)
+{
+	switch (mode) {
+		case ModeVideoEnv:
+		{
+			return "RVC_ENV_Virtual_Camera";
+		}
+		case ModeVideoOpt:
+		{
+			return "RVC_OPT_Virtual_Camera";
+		}
+		default: {
+			return "Error_Camera_Info";
+		}
+	}
+}
+
+static bool GetStartCameraMsg(char* szbuffer, int ibufferlen, int imode, char* pstrmsg)
+{
+	char* strCameraInfo = GetCameraInfo(imode);
+
+	snprintf(szbuffer, ibufferlen, "%s %s+%s", pstrmsg, strCameraInfo, "Start");
+
+	return true;
+}
+
+static bool GetStopCameraMsg(char* szbuffer, int ibufferlen, int imode, char* pstrmsg, int icaptime, int iframenums)
+{
+	char* strCameraInfo = GetCameraInfo(imode);
+
+	snprintf(szbuffer, ibufferlen, "%s %s+%s", pstrmsg, strCameraInfo, "Stop");
+
+	return true;
+}
+
+static bool GetDefaultCameraMsg(char* szbuffer, int ibufferlen, int imode, char* pstrmsg)
+{
+	char* strCameraInfo = GetCameraInfo(imode);
+
+	snprintf(szbuffer, ibufferlen, "%s+%s", strCameraInfo, pstrmsg);
+
+	return true;
+}
+
+static bool ConstructMsg(char* szbuffer, int ibufferlen, int imode, eMsgType eType, char* pstrmsg, int icaptime, int iframenums)
+{
+	switch (eType) {
+		case eStartCamera:
+		{
+			return GetStartCameraMsg(szbuffer, ibufferlen, imode, pstrmsg);
+		}
+		case eCapProcess:
+		{
+			return true;
+		}
+		case eStopCamera:
+		{
+			return GetStopCameraMsg(szbuffer, ibufferlen, imode, pstrmsg, icaptime, iframenums);
+		}
+		case eMsgDefault: 
+		{
+			return GetDefaultCameraMsg(szbuffer, ibufferlen, imode, pstrmsg);
+		}
+		default:
+			return false;
+	}
+}
+
+static bool PostCameraStateMsg(int imode, eMsgType eType, char* pstrmsg, int icaptime = 0, int iframenums = 0)
+{
+	bool bRet = false;
+	//Connect to the server mailslot using CreateFile()
+	HANDLE hMailslot = CreateFileA(
+		szMailslot,          // mailslot name 
+		GENERIC_WRITE,         // mailslot write only 
+		FILE_SHARE_READ,       // required for mailslots
+		NULL,                  // default security attributes
+		OPEN_EXISTING,         // opens existing mailslot 
+		FILE_ATTRIBUTE_NORMAL, // normal attributes 
+		NULL);                 // no template file 
+
+	if (INVALID_HANDLE_VALUE == hMailslot) {
+		return bRet;  //Error
+	}
+
+
+	//We are done connecting to the mailslot, 
+	//Mailslot communication is one-way, 
+	//client will just write to mailslot
+	//Using WriteFile()
+
+	char szBuffer[BUFFER_SIZE] = { 0 };
+	ConstructMsg(szBuffer, BUFFER_SIZE, imode, eType, pstrmsg, icaptime, iframenums);
+
+	DWORD cbBytes;
+
+	//Send the message to server
+	BOOL bResult = WriteFile(
+		hMailslot,            // handle to mailslot 
+		szBuffer,             // buffer to write from 
+		strlen(szBuffer) + 1, // number of bytes to write, include the NULL
+		&cbBytes,             // number of bytes written 
+		NULL);                // not overlapped I/O 
+
+	if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes)) {
+		CloseHandle(hMailslot);
+		return false;  //Error
+	}
+
+	CloseHandle(hMailslot);
+
+	bRet = true;
+
+	return bRet; //Success
+}
+
 
 CUnknown * WINAPI CreateEnvInstance(LPUNKNOWN lpunk, HRESULT *phr)
 {
@@ -166,6 +289,12 @@ HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
 	REFERENCE_TIME end_time = 0;
 	REFERENCE_TIME duration = 0;
 
+	static bool bloged = false;
+	if (!bloged) {
+		PostCameraStateMsg(m_queue_mode, eMsgDefault, __FUNCTION__);
+		bloged = true;
+	}
+
 	hr = pms->GetPointer((BYTE**)&m_dst);
 	long lsize = pms->GetSize();
 	
@@ -183,6 +312,18 @@ HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
 			shared_queue_get_video_format(m_queue_mode, &m_format, &m_frame_width, &m_frame_height, &m_time_perframe);
 			SetGetTimeout();
 			m_sync_timeout = 0;
+			static bool bsucloged = false;
+			if (!bsucloged) {
+				PostCameraStateMsg(m_queue_mode, eMsgDefault, "shared_queue_open success");
+				bsucloged = true;
+			}
+		}
+		else {
+			static bool bfailloged = false;
+			if (!bfailloged) {
+				PostCameraStateMsg(m_queue_mode, eMsgDefault, "shared_queue_open failed");
+				bfailloged = true;
+			}
 		}
 	}
 
@@ -385,113 +526,6 @@ HRESULT CVCamStream::DecideBufferSize(IMemAllocator *pAlloc, ALLOCATOR_PROPERTIE
 	return NOERROR;
 } 
 
-static char* GetCameraInfo(int mode)
-{
-	switch (mode) {
-		case ModeVideoEnv:
-		{
-			return "RVC_ENV_Virtual_Camera";
-		}
-		case ModeVideoOpt:
-		{
-			return "RVC_OPT_Virtual_Camera";
-		}
-		default: {
-			return "Error_Camera_Info";
-		}
-	}
-}
-
-static bool GetStartCameraMsg(char* szbuffer, int ibufferlen, int imode)
-{
-	char* strCameraInfo = GetCameraInfo(imode);
-
-	_snprintf(szbuffer, ibufferlen, "%s+%s", strCameraInfo, "Start");
-
-	return true;
-}
-
-static bool GetStopCameraMsg(char* szbuffer, int ibufferlen, int imode, int icaptime, int iframenums)
-{
-	char* strCameraInfo = GetCameraInfo(imode);
-
-	_snprintf(szbuffer, ibufferlen, "%s+%s", strCameraInfo, "Stop");
-
-	return true;
-}
-
-static bool ConstructMsg(char* szbuffer, int ibufferlen, int imode, bool bstart, int icaptime, int iframenums)
-{
-	bool bRet = false;
-	if (bstart) {
-		bRet = GetStartCameraMsg(szbuffer, ibufferlen, imode);
-	}
-	else {
-		bRet = GetStopCameraMsg(szbuffer, ibufferlen, imode, icaptime, iframenums);
-	}
-
-	return bRet;
-}
-
-static bool PostCameraStateMsg(int imode, bool bstart, int icaptime = 0, int iframenums = 0)
-{
-	bool bRet = false;
-	//Connect to the server mailslot using CreateFile()
-	HANDLE hMailslot = CreateFileA(
-		szMailslot,          // mailslot name 
-		GENERIC_WRITE,         // mailslot write only 
-		FILE_SHARE_READ,       // required for mailslots
-		NULL,                  // default security attributes
-		OPEN_EXISTING,         // opens existing mailslot 
-		FILE_ATTRIBUTE_NORMAL, // normal attributes 
-		NULL);                 // no template file 
-
-	if (INVALID_HANDLE_VALUE == hMailslot)
-	{
-		printf("\nError occurred while connecting"
-			" to the server: %d", GetLastError());
-		return bRet;  //Error
-	}
-	else
-	{
-		printf("\nCreateFile() was successful.");
-	}
-
-	//We are done connecting to the mailslot, 
-	//Mailslot communication is one-way, 
-	//client will just write to mailslot
-	//Using WriteFile()
-
-	char szBuffer[BUFFER_SIZE] = {0};
-	ConstructMsg(szBuffer, BUFFER_SIZE, imode, bstart, icaptime, iframenums);
-
-	DWORD cbBytes;
-
-	//Send the message to server
-	BOOL bResult = WriteFile(
-		hMailslot,            // handle to mailslot 
-		szBuffer,             // buffer to write from 
-		strlen(szBuffer) + 1, // number of bytes to write, include the NULL
-		&cbBytes,             // number of bytes written 
-		NULL);                // not overlapped I/O 
-
-	if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
-	{
-		printf("\nError occurred while writing"
-			" to the server: %d", GetLastError());
-		CloseHandle(hMailslot);
-		return false;  //Error
-	}
-	else
-	{
-		printf("\nWriteFile() was successful.");
-	}
-
-	CloseHandle(hMailslot);
-	bRet = true;
-
-	return bRet; //Success
-}
 
 HRESULT CVCamStream::OnThreadCreate()
 {
@@ -500,7 +534,7 @@ HRESULT CVCamStream::OnThreadCreate()
 	m_dshow_start_ts = 0;
 	m_system_start_time = 0;
 	m_camera_started = true;
-	PostCameraStateMsg(m_queue_mode, m_camera_started);
+	PostCameraStateMsg(m_queue_mode, eStartCamera, __FUNCTION__);
 
 	return NOERROR;
 }
@@ -513,7 +547,7 @@ HRESULT CVCamStream::OnThreadDestroy()
 	if (m_queue.header) {
 		shared_queue_read_close(&m_queue);
 	}
-	PostCameraStateMsg(m_queue_mode, m_camera_started);
+	PostCameraStateMsg(m_queue_mode, eStopCamera, __FUNCTION__);
 
 	return NOERROR;
 }