Browse Source

Z991239-4157 #comment snapshot实体启动优化

80274480 2 years ago
parent
commit
b3f4cd45e2
1 changed files with 138 additions and 110 deletions
  1. 138 110
      Module/mod_snapshot/mod_snapshot.cpp

+ 138 - 110
Module/mod_snapshot/mod_snapshot.cpp

@@ -80,7 +80,7 @@ int RotationDown(unsigned char* src, int srcW, int srcH, int channel)
 class CPhotoCaptureEntity : public CEntityBase, public ITimerListener, public ILogListener
 {
 public:
-	CPhotoCaptureEntity() : m_video_env_q(NULL), m_video_opt_q(NULL), m_dwCapture(0) {}
+	CPhotoCaptureEntity() : m_video_env_q(NULL), m_video_opt_q(NULL), m_dwCapture(0), m_bConnectAssist(FALSE) {m_pChannelClient = NULL;}
 	virtual ~CPhotoCaptureEntity() {}
 	virtual const char *GetEntityName() const { return "Snapshot"; }
 
@@ -89,6 +89,7 @@ public:
 		ErrorCodeEnum Error = __OnStart(Error_Succeed);
 		pTransactionContext->SendAnswer(Error);
 	}
+
 	virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext) 
 	{ 
 		ErrorCodeEnum Error = __OnClose(Error_Succeed);
@@ -101,70 +102,18 @@ public:
 
 		if (preOperationError != Error_Succeed)
 			return preOperationError;
+
 		//is Pad Version
-		CSmartPointer<IEntityFunction> spFunction = GetFunction();
-		CSystemStaticInfo stStaticinfo;
-		spFunction->GetSystemStaticInfo(stStaticinfo);
-		if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
-		{
-			if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
-			{
-				LOG_TRACE("the type is mobile pad");
-				m_eDeviceType = eMobilePadType;
-			}
-			else
-			{
-				LOG_TRACE("the type is pad");
-				m_eDeviceType = ePadtype;
-			}
-		}
-		else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
-		{
-			LOG_TRACE("the type is Desk2S");
-			m_eDeviceType = eDesk2SType;
-		}
-		else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
-		{
-			LOG_TRACE("the type is RPM.Stand1S");
-			m_eDeviceType = eRpm1sType;
-		}
-		else
-		{
-			LOG_TRACE("the type is standard");
-			m_eDeviceType = eStand2sType;
-		}
-		ErrorCodeEnum Error;
-		//int nCameraCount = 0;
-		//ErrorCodeEnum Error;
-		//Error = DecideCameraCount(nCameraCount);
-		//if (Error != Error_Succeed || nCameraCount < 0 || nCameraCount > 2) {
-		//	LOG_TRACE("decide camera count failed!");
-		//	return Error;
-		//}
-
-		//if ((ePadtype == m_eDeviceType)||(eMobilePadType == m_eDeviceType)||(eDesk2SType == m_eDeviceType) )
-		//{
-		//	m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
-		//} 
-		//else 
-		//{ //  == 2
-		//	m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
-		//	m_video_opt_q = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
-		//}
+		m_eDeviceType = RvcGetDeviceType();
 
-		m_pChannelClient = new ChannelClient(this);
-		Error = m_pChannelClient->Connect();
-		if (Error != Error_Succeed) {
-			m_pChannelClient->SafeDelete();
-			return Error;
-		}
-		ChannelService_BeginRecv_Sub Sub;
-		Sub.type = ACM_TYPE_PHT;
-		Error = m_pChannelClient->BeginRecv(Sub);
-		if (Error != Error_Succeed) {
-			m_pChannelClient->GetFunction()->CloseSession();
-			m_pChannelClient = NULL;
-			return Error;
+		if ((ePadtype == m_eDeviceType)||(eMobilePadType == m_eDeviceType)||(eDesk2SType == m_eDeviceType)||(eDesk1SType == m_eDeviceType)||(eDesk2SIntegratedType == m_eDeviceType))
+		{
+			m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
+		} 
+		else 
+		{ //  == 2
+			m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
+			m_video_opt_q = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
 		}
 
 		CSmartPointer<IEntityFunction> spFunc = GetFunction();
@@ -173,8 +122,17 @@ public:
 		spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None,  Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV);
 		spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None,  Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT);
 		spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None,  Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT);
-		spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE,NULL,false);
-		return Error;
+		spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS,NULL,false);
+
+		return Error_Succeed;
+	}
+
+	void OnStarted()
+	{
+		m_pChannelClient = new ChannelClient(this);
+		if (Error_Succeed == ConnectAssistChannel()) {
+			m_bConnectAssist = TRUE;
+		}
 	}
 
 	ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
@@ -217,11 +175,11 @@ public:
 			frm.height = srcfrm->height;
 			frm.framesize = srcfrm->width * srcfrm->height * 3;
 			if (!SnapShot(&frm)) {
-				Dbg("write stamp failed!");
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("write stamp failed!");
 			}
 		}
 		__except (EXCEPTION_EXECUTE_HANDLER) {
-			Dbg("snapshot throw windows exception!");
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("snapshot throw windows exception!");
 		}
 #else
 		CImageFrame frm;
@@ -230,7 +188,7 @@ public:
 		frm.height = srcfrm->height;
 		frm.framesize = srcfrm->width * srcfrm->height * 3;
 		if (!SnapShot(&frm)) {
-			Dbg("write stamp failed!");
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("write stamp failed!");
 		}
 #endif
 	}
@@ -256,14 +214,14 @@ public:
 					tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
 					DWORD time = m_video_env_q->GetLastFrameTime();
 					y2k_time_t nowtime = y2k_time_now();
-					Dbg("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
+					DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
 					if ((nowtime - time) < ENV_CAP_TIMEOUT)
 					{
 						m_video_env_q->GetVideo2(&tmp_frm, 0);
 					}
 					else
 					{
-						Dbg("the env snapshot videoqueque image timeout!");
+						DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the env snapshot videoqueque image timeout!");
 					}
 				}
 				// get opt snapshot
@@ -276,14 +234,14 @@ public:
 					tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
 					DWORD time = m_video_opt_q->GetLastFrameTime();
 					y2k_time_t nowtime = y2k_time_now();
-					Dbg("%s:%d opt now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
+					DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d opt now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
 					if ((nowtime - time) < OPT_CAP_TIMEOUT)
 					{
 						m_video_opt_q->GetVideo2(&tmp_frm, 0);
 					}
 					else
 					{
-						Dbg("the opt snapshot videoqueque image timeout!");
+						DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the opt snapshot video queque image timeout!");
 					}
 				}
 			} 
@@ -299,14 +257,14 @@ public:
 				tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
 				DWORD time = m_video_env_q->GetLastFrameTime();
 				y2k_time_t nowtime = y2k_time_now();
-				Dbg("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
 				if ((nowtime - time) < ENV_CAP_TIMEOUT)
 				{
 					m_video_env_q->GetVideo2(&tmp_frm, 0);
 				}
 				else
 				{
-					Dbg("the env snapshot videoqueque image timeout!");
+					DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the env snapshot video queque image timeout!");
 				}
 
 			} 
@@ -320,7 +278,7 @@ public:
 			{
 				//linux需翻转环境摄像头图像
 				int size = frm->linesize[0] * frm->height;
-				Dbg("size = %d, width= %d height= %d", size, frm->width, frm->height);
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("size = %d, width= %d height= %d", size, frm->width, frm->height);
 
 				RotationDown(frm->data[0], frm->width, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, 3);
 			}
@@ -383,11 +341,11 @@ public:
 		}
 
 		if (!m_video_env_q && !m_video_opt_q) {
-			Dbg("cannot start capture!"); // never go here
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cannot start capture!"); // never go here
 			return Error_Unexpect;
 		}
 		if (m_dwCapture) {
-			LOG_TRACE("already start capture!");
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("already start capture!");
 			return Error_Duplication;
 		}
 		//触发对应的摄像头进行图像采集
@@ -416,12 +374,65 @@ public:
 		m_dwCapture = 0;
 	}
 
+	DeviceTypeEnum RvcGetDeviceType()
+	{
+		DeviceTypeEnum eType = eStand2sType;
+		CSmartPointer<IEntityFunction> spFunction = GetFunction();
+		CSystemStaticInfo stStaticinfo;
+		spFunction->GetSystemStaticInfo(stStaticinfo);
+		if (stricmp(stStaticinfo.strMachineType, "RVC.PAD") == 0) {
+			if (stricmp(stStaticinfo.strSite, "CMB.FLB") == 0) {
+				eType = eMobilePadType;
+			}
+			else {
+				eType = ePadtype;
+			}
+		}
+		else if (stricmp(stStaticinfo.strMachineType, "RVC.Desk2S") == 0) {
+			eType = eDesk2SType;
+			WORD nMajor = stStaticinfo.MachineVersion.GetMajor();
+			WORD nMinor = stStaticinfo.MachineVersion.GetMinor();
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("MachineVersion is %d.%d", nMajor, nMinor);
+			if (2 == nMajor) {
+				eType = eDesk2SIntegratedType;
+			}
+		}
+		else if (stricmp(stStaticinfo.strMachineType, "RPM.Stand1S") == 0) {
+			eType = eRpm1sType;
+		}
+		else if (stricmp(stStaticinfo.strMachineType, "RVC.Desk1S") == 0) {
+			eType = eDesk1SType;
+		}
+		else {
+			eType = eStand2sType;
+		}
+
+		if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
+		}
+
+		return eType;
+	}
+
 	virtual void OnTimeout(DWORD dwTimerID)
 	{
-		Capture(Error_TimeOut);
-		Dbg("timeout!");
-		GetFunction()->KillTimer(dwTimerID);
-		m_dwCapture = 0;
+		if (1 == dwTimerID) {
+			Capture(Error_TimeOut);
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("timeout!");
+			GetFunction()->KillTimer(dwTimerID);
+			m_dwCapture = 0;
+		}
+		else  if (2 == dwTimerID) {
+			if (FALSE == m_bConnectAssist) {
+				if (Error_Succeed == ConnectAssistChannel()) {
+					m_bConnectAssist = TRUE;
+				}
+			}
+
+			if (TRUE == m_bConnectAssist) {
+				GetFunction()->KillTimer(2);
+			}
+		}
 	}
 
 	virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
@@ -433,43 +444,27 @@ public:
 			dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV) 
 		{
 			DWORD dwNow = SP::Module::Comm::RVCGetTickCount();
-			LOG_TRACE("capture duration:%dms", dwNow - m_dwCapture);
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("capture duration:%dms", dwNow - m_dwCapture);
 			Capture(Error_Succeed);
 			StopCapture();
 		}
-		else if (dwUserCode == LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE)
+		else if (LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS == dwUserCode)
 		{
-			Dbg("recv LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE");
-			if (m_pChannelClient != NULL)
-			{
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS");
+			Sleep(975);
+			if (m_pChannelClient != NULL){
 				m_pChannelClient->GetFunction()->CloseSession();
 				m_pChannelClient = NULL;
-				Dbg("Close AssistChannel Session ");
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Close AssistChannel Session ");
+				m_bConnectAssist = FALSE;
 			}
 
-			if (m_pChannelClient == NULL)
-			{
-				Dbg("ReConnection AssistChannel Session");
-				m_pChannelClient = new ChannelClient(this);
-				ErrorCodeEnum Error = m_pChannelClient->Connect();
-				if (Error != Error_Succeed) 
-				{
-					m_pChannelClient->SafeDelete();
-					m_pChannelClient = NULL;
-					Dbg("AssistChannelClient connect fail!");
-				}
-
-				if (Error == Error_Succeed)
-				{
-					ChannelService_BeginRecv_Sub Sub;
-					Sub.type = ACM_TYPE_PHT;
-					Error = m_pChannelClient->BeginRecv(Sub);
-					if (Error != Error_Succeed) 
-					{
-						m_pChannelClient->GetFunction()->CloseSession();
-						m_pChannelClient = NULL;
-					}
-				}
+			if (Error_Succeed == ConnectAssistChannel()) {
+				m_bConnectAssist = TRUE;
+			}
+			else {
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start timer for reconnect to assist channel.");
+				GetFunction()->SetTimer(2, this, 3370);
 			}
 		}
 	}
@@ -501,6 +496,38 @@ private:
 		return Error;
 	}
 
+	ErrorCodeEnum ConnectAssistChannel()
+	{
+		if (m_pChannelClient == NULL){
+			m_pChannelClient = new ChannelClient(this);
+		}
+		
+		ErrorCodeEnum Error = m_pChannelClient->Connect();
+		if (Error_Succeed != Error){
+			m_pChannelClient = NULL;
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("AssistChannelClient connect fail!");
+			return Error;
+		}
+		else {
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("AssistChannelClient connect success!");
+		}
+
+		if (Error_Succeed == Error){
+			ChannelService_BeginRecv_Sub Sub;
+			Sub.type = ACM_TYPE_PHT;
+			Error = m_pChannelClient->BeginRecv(Sub);
+			if (Error_Succeed != Error){
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("BeginRecv ACM_TYPE_PHT fail!");
+				m_pChannelClient->GetFunction()->CloseSession();
+				m_pChannelClient = NULL;
+				return Error;
+			}
+			else {
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("BeginRecv ACM_TYPE_PHT success!");
+			}
+		}
+		return Error;
+	}
 private:
 	Clibvideoqueue *m_video_env_q;
 	Clibvideoqueue *m_video_opt_q;
@@ -509,6 +536,7 @@ private:
 	DWORD m_dwCapture;
 	DeviceTypeEnum m_eDeviceType;
 	CAutoArray<CUUID> m_arrListener;
+	BOOL m_bConnectAssist;
 };
 
 void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )