Explorar o código

Z991239-5471 #comment 优化录像加密流程,去除误报加密失败错误

80274480 hai 1 ano
pai
achega
0796fa8392

+ 3 - 1
Module/mod_recorder/Event.h

@@ -3,7 +3,9 @@
 #define LOG_EVT_RECORDFAILED						0x31200001				//录像失败
 #define LOG_EVT_RECORDER_SECTION_FINISHED			0x31200002				//一段录像完成
 #define LOG_EVT_RECORDER_WHOLE_FINISHED			    0x31200003				//整个录像完成
-#define LOG_EVT_RECORDER_VIDEO_SIZE					0x31200004				//录像文件大小
+#define LOG_EVT_RECORDER_VIDEO_SIZE					0x31200004				//加密前录像文件大小
+#define LOG_EVT_ENCRYPTED_RECORDER_VIDEO_SIZE		0x31200005				//加密后录像文件大小
+#define LOG_EVT_INVALID_RECORD_FILE					0x31200006				//无效的录像文件
 
 #define LOG_EVT_RECORDER_ENCRYPT_FAILED			    0x31210001				//文件加密失败
 #define LOG_EVT_RECORDER_DELETE_FAILED			    0x31210002				//录像删除失败

+ 63 - 24
Module/mod_recorder/mod_recorder.cpp

@@ -39,6 +39,9 @@ using namespace Recorder;
 #define RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX "enc_B_"
 #endif // !RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX
 
+#ifndef RVC_MIN_RECORD_FILESIZE
+#define RVC_MIN_RECORD_FILESIZE 1024
+#endif
 
 static unsigned long GetFileSize(const char* pfilename)
 {
@@ -72,10 +75,17 @@ static const char* GetFileName(const char* pfilename)
 	return strstr(pfilename, RVC_TRANSATCION_RECORD_SUFFIX);
 }
 
-static void LogVideoSizeInfo(const char* pszMessage)
+static unsigned long GetRecordFileSize(const char* pszMessage, bool bencrypted)
 {
 	unsigned long ufilesize = GetFileSize(pszMessage);
-	LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("%s file size is %u byte.", pszMessage, ufilesize).GetData());
+	if (false == bencrypted) {
+		LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("%s file size is %u byte.", pszMessage, ufilesize).GetData());
+	}
+	else {
+		LogWarn(Severity_Low, Error_Debug, LOG_EVT_ENCRYPTED_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("encrypted file %s size is %u byte.", pszMessage, ufilesize).GetData());
+	}
+
+	return ufilesize;
 }
 
 static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
@@ -458,17 +468,8 @@ ErrorCodeEnum CRecorderEntity::HandleExceptionRecordVideos()
 	}
 
 	SetRecordSessionID(strSession + strlen(RVC_TRANSATCION_RECORD_SUFFIX));
-
-	while (--iSeriesNum >= 0) {
-		CSimpleStringA strFilePath("");
-		strFilePath = CSimpleStringA::Format("%s%s_%d.%s", m_RecordSaveDir.GetData(), strSession, iSeriesNum, strFormat);
-		if (ExistsFile(strFilePath.GetData())) {
-			AddToVideoRecordList(strFilePath.GetData());
-		}
-		else {
-			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File {%s} is not exist.", strFilePath.GetData());
-		}
-	}
+	
+	Error = Error_Succeed;
 
 	return Error;
 }
@@ -561,22 +562,34 @@ void CRecorderEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,
 
 	case LOG_EVT_RECORDER_SECTION_FINISHED:
 		{
-			LogVideoSizeInfo(pszMessage);
-			if (m_bEncFlag){
-				HandleEncryptVideoRecord(pszMessage);
+			unsigned long uSize = GetRecordFileSize(pszMessage, false);
+			if (RVC_MIN_RECORD_FILESIZE < uSize) {
+				if (m_bEncFlag) {
+					HandleEncryptVideoRecord(pszMessage);
+				}
+				HandleSaveVideoRecord(pszMessage);
+			}
+			else {
+				LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
+				RvcDeleteFile(pszMessage);
 			}
-			HandleSaveVideoRecord(pszMessage);
 		}
 		break;
 
 	case LOG_EVT_RECORDER_WHOLE_FINISHED:
 		{
-			LogVideoSizeInfo(pszMessage);
-			if (m_bEncFlag){
-				HandleEncryptVideoRecord(pszMessage);
+			unsigned long uSize = GetRecordFileSize(pszMessage, false);
+			if (RVC_MIN_RECORD_FILESIZE < uSize) {
+				if (m_bEncFlag) {
+					HandleEncryptVideoRecord(pszMessage);
+				}
+				HandleFinishedVideoRecord(pszMessage);
+				PostVideoRecordInfos();
+			}
+			else {
+				LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
+				RvcDeleteFile(pszMessage);
 			}
-			HandleFinishedVideoRecord(pszMessage);
-			PostVideoRecordInfos();
 		}
 		break;
 
@@ -774,6 +787,11 @@ int CRecorderEntity::HandleEncryptVideoRecord(const char* videofilename)
 		return iRet;
 	}
 
+	if (!ExistsFile(videofilename)) {
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File %s is not exist.", videofilename);
+		return iRet;
+	}
+
 	filecryption_callback_t cb = {0};
 	cb.dbg = &rvcDbg;
 	
@@ -788,6 +806,9 @@ int CRecorderEntity::HandleEncryptVideoRecord(const char* videofilename)
 		}
 		return iRet;
 	}
+	else {
+		GetRecordFileSize(strOutFile, true);
+	}
 
 	bool bRet = RvcDeleteFile(videofilename);
 	if(!bRet) {
@@ -952,11 +973,20 @@ int CRecorderEntity::SaveExceptionRecordVideos()
 			if (0 == memcmp(FindFileData.cFileName, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
 				0 == memcmp(FindFileData.cFileName, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))){
 				CSimpleStringA srcfile = CSimpleStringA::Format("%s%s",m_TempDir.GetData(), FindFileData.cFileName);
+				unsigned long uSize = GetRecordFileSize(srcfile.GetData(), false);
+				if (RVC_MIN_RECORD_FILESIZE >= uSize) {
+					LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", srcfile.GetData()).GetData());
+					RvcDeleteFile(srcfile.GetData());
+					goto on_next;
+				}
+
 				if (m_bEncFlag){
 					filecryption_callback_t cb = {0};
 					cb.dbg = &rvcDbg;
 					if (false == is_file_encrypted(srcfile.GetData(), &cb)){
-						HandleEncryptVideoRecord(srcfile.GetData());
+						if (is_file_completed(srcfile.GetData(), &cb)) {
+							HandleEncryptVideoRecord(srcfile.GetData());
+						}
 					}
 					else{
 						char* pIndex = NULL;
@@ -1011,11 +1041,20 @@ on_next:
 		if (0 == memcmp(ptr->d_name, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
 			0 == memcmp(ptr->d_name, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))) {
 			CSimpleStringA srcfile = CSimpleStringA::Format("%s%s", m_TempDir.GetData(), ptr->d_name);
+			unsigned long uSize = GetRecordFileSize(srcfile.GetData(), false);
+			if (RVC_MIN_RECORD_FILESIZE >= uSize) {
+				LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
+				RvcDeleteFile(pszMessage);
+				continue;
+			}
+
 			if (m_bEncFlag) {
 				filecryption_callback_t cb = { 0 };
 				cb.dbg = &rvcDbg;
 				if (false == is_file_encrypted(srcfile.GetData(), &cb)) {
-					HandleEncryptVideoRecord(srcfile.GetData());
+					if (is_file_completed(srcfile.GetData(), &cb)) {
+						HandleEncryptVideoRecord(srcfile.GetData());
+					}
 				}
 				else {
 					char* pIndex = NULL;

+ 6 - 1
Other/libfilecryption/filecryption.cpp

@@ -147,7 +147,7 @@ static int encrypt_mp4_file(char* poutfilename, char* phead, uint32_t uheaderlen
 	int32_t iencdatalen = umoovboxlen + SM4ENC_BLOCK_SIZE;
 	unsigned char* pobjdataenc = (unsigned char*)malloc(iencdatalen);
 	SM4EncECBMode(pstrkey, pmoovboxdata, umoovboxlen, pobjdataenc, &iencdatalen);
-	safe_log(pcallback, FILECRYPT_LOG_DEBUG, "after encode, moov box length is %u.", iencdatalen);
+	safe_log(pcallback, FILECRYPT_LOG_INFO, "after encode, moov box length is %u.", iencdatalen);
 
 	//3. 填充加密后的moov box长度
 	fwrite(&iencdatalen, 1, sizeof(uint32_t), pDestFile);
@@ -511,3 +511,8 @@ int rvc_free_data(void** pdechead)
 	return iret;
 }
 
+
+bool is_file_completed(const char* pfilename, const filecryption_callback_t* pcallback)
+{
+	return is_mp4file_completed(pfilename, pcallback);
+}

+ 1 - 0
Other/libfilecryption/filecryption.h

@@ -61,6 +61,7 @@ FILECRYPTION_API(int) decryption_file(char* poutfile, uint32_t uoutlen, const ch
 FILECRYPTION_API(bool) is_file_encrypted(const char* pfilename, const filecryption_callback_t* pcallback);
 FILECRYPTION_API(int) rvc_file_decrypt(unsigned char** pdechead, uint32_t* udecheadlen, int* ioffset, char** pstrjson, uint32_t* ujsonlen,const char* pfilename, const filecryption_callback_t* pcallback = NULL, eRvcCryptionVersion eversion = eVerA);
 FILECRYPTION_API(int) rvc_free_data(void** pdechead);
+FILECRYPTION_API(bool) is_file_completed(const char* pfilename, const filecryption_callback_t* pcallback);
 
 #ifdef __cplusplus
 } // extern "C" {

+ 26 - 1
Other/libfilecryption/mp4info.cpp

@@ -67,10 +67,35 @@ unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback
 			safe_log(pcallback, FILECRYPT_LOG_ERROR, "get moov box failed.");
 			return 0;
 		}
-		safe_log(pcallback, FILECRYPT_LOG_DEBUG, "get moov box success, box size is %d.", uboxsize);
+		safe_log(pcallback, FILECRYPT_LOG_INFO, "get moov box success, box size is %d.", uboxsize);
 	}
 
 	fseek(pSrcFile, ipostion, SEEK_SET);
 
 	return uret;
+}
+
+
+bool is_mp4file_completed(const char* pSrcName, const filecryption_callback_t* pcallback)
+{
+	bool bret = false;
+	if (NULL == pSrcName) {
+		return bret;
+	}
+
+	FILE* pSrcFile = fopen(pSrcName, "rb");
+	if (NULL == pSrcFile) {
+		return bret;
+	}
+
+	unsigned int unoencryptlen = get_noencrypt_boxs_size(pSrcFile, pcallback, true);
+	fclose(pSrcFile);
+	
+	if (0 == unoencryptlen) {
+		return bret;
+	}
+
+	bret = true;
+
+	return bret;
 }

+ 3 - 1
Other/libfilecryption/mp4info.h

@@ -47,4 +47,6 @@ typedef struct box_head_s{
 }box_head_t;
 
 
-unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback_t* pcallback, bool bgetmoov);
+unsigned int get_noencrypt_boxs_size(FILE* pSrcFile, const filecryption_callback_t* pcallback, bool bgetmoov);
+
+bool is_mp4file_completed(const char* pSrcName, const filecryption_callback_t* pcallback);

+ 4 - 4
Other/unix/libvideorecord/libvideorecord_impl.cpp

@@ -1124,7 +1124,7 @@ int libvideorecord_impl::GetStand2SVideoFrameData(videoq_frame* Video, int flags
 		delete tmp_frm;
 	}
 	else{
-		m_pHostApi->Debug(RECORD_LOG_ERROR, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
+		m_pHostApi->Debug(RECORD_LOG_DEBUG, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
 		return -4;
 	}
 
@@ -1214,7 +1214,7 @@ int libvideorecord_impl::GetSingleSideVideoFrameData(videoq_frame* Video, int fl
 		bRslt = m_env_videoqueue->GetVideo(tmp_frm, flags);
 		delete tmp_frm;
 		if (!bRslt) {
-			//m_pHostApi->Debug(RECORD_LOG_ERROR, "get env video fail!");
+			m_pHostApi->Debug(RECORD_LOG_DEBUG, "get env video fail!");
 			return -1;
 		}
 	}
@@ -1231,12 +1231,12 @@ int libvideorecord_impl::GetSingleSideVideoFrameData(videoq_frame* Video, int fl
 		bRslt = m_opt_videoqueue->GetVideo3(tmp_frm, flags);
 		delete tmp_frm;
 		if (!bRslt) {
-			m_pHostApi->Debug(RECORD_LOG_ERROR, "get opt video fail!");
+			m_pHostApi->Debug(RECORD_LOG_DEBUG, "get opt video fail!");
 			return -2;
 		}
 	}
 	else {
-		m_pHostApi->Debug(RECORD_LOG_ERROR, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
+		m_pHostApi->Debug(RECORD_LOG_DEBUG, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
 		return -1;
 	}
 

+ 3 - 3
Other/win/libvideorecord/libvideorecord_impl.cpp

@@ -1059,7 +1059,7 @@ int libvideorecord_impl::GetStand2SVideoFrameData(videoq_frame* Video, int flags
 		delete tmp_frm;
 	}
 	else{
-		m_pHostApi->Debug(RECORD_LOG_ERROR, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
+		m_pHostApi->Debug(RECORD_LOG_DEBUG, "nActiveCam value is not valid, and it's value is %d.", nActiveCam);
 		return -4;
 	}
 
@@ -1134,7 +1134,7 @@ int libvideorecord_impl::GetSingleSideVideoFrameData(videoq_frame* Video, int fl
 		bRslt = m_env_videoqueue->GetVideo(tmp_frm, flags);
 		delete tmp_frm;
 		if (!bRslt) {
-			m_pHostApi->Debug(RECORD_LOG_ERROR, "get env video fail!");
+			m_pHostApi->Debug(RECORD_LOG_DEBUG, "get env video fail!");
 			return -1;
 		}
 	}
@@ -1151,7 +1151,7 @@ int libvideorecord_impl::GetSingleSideVideoFrameData(videoq_frame* Video, int fl
 		bRslt = m_opt_videoqueue->GetVideo3(tmp_frm, flags);
 		delete tmp_frm;
 		if (!bRslt) {
-			//m_pHostApi->Debug(RECORD_LOG_ERROR, "get opt video fail!");
+			m_pHostApi->Debug(RECORD_LOG_DEBUG, "get opt video fail!");
 			return -2;
 		}
 	}