mod_recorder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #include "mod_recorder.h"
  2. #include "fileutil.h"
  3. #include "array.h"
  4. #include <memutil.h>
  5. #include <algorithm>
  6. #include <sys/stat.h>
  7. #include "mod_customeraware/Event.h"
  8. #include "mod_facetracking/sysvar.h"
  9. #include <assert.h>
  10. #include "Event.h"
  11. using namespace Recorder;
  12. #ifndef MAX_LOG_LEN
  13. #define MAX_LOG_LEN 512
  14. #endif
  15. #ifndef RVC_TRANSATCION_RECORD_SUFFIX
  16. #define RVC_TRANSATCION_RECORD_SUFFIX "B_"
  17. #endif // !RVC_TRANSATCION_RECORD_SUFFIX
  18. void RecordServiceSession::Handle_StartTransactionRecord(SpReqAnsContext<RecorderSerVice_StartTransactionRecord_Req, RecorderSerVice_StartTransactionRecord_Ans>::Pointer ctx)
  19. {
  20. if (m_pEntity->GetStartFlag()) {
  21. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Current is recording, stop it.");
  22. m_pEntity->StopRecord();
  23. }
  24. char strVideoName[MAX_PATH] = { 0 };
  25. snprintf(strVideoName, MAX_PATH, "%s%s",RVC_TRANSATCION_RECORD_SUFFIX, ctx->Req.VideoName.GetData());
  26. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("StartRecord and video name is %s.", strVideoName);
  27. m_pEntity->StartRecord(strVideoName);
  28. ctx->Answer(Error_Succeed);
  29. }
  30. void RecordServiceSession::Handle_StopTransactionRecord(SpReqAnsContext<RecorderSerVice_StopTransactionRecord_Req, RecorderSerVice_StopTransactionRecord_Ans>::Pointer ctx)
  31. {
  32. m_pEntity->StopRecord();
  33. ctx->Answer(Error_Succeed);
  34. }
  35. void CRecorderEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  36. {
  37. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  38. pTransactionContext->SendAnswer(Error);
  39. }
  40. void CRecorderEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  41. {
  42. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  43. pTransactionContext->SendAnswer(Error);
  44. }
  45. ErrorCodeEnum CRecorderEntity::__OnStart(ErrorCodeEnum preOperationError)
  46. {
  47. ErrorCodeEnum Error = Error_Succeed;
  48. m_eDeviceType = RvcGetDeviceType();
  49. if (preOperationError != Error_Succeed) {
  50. return preOperationError;
  51. }
  52. m_iActiveCamera = CAMERA_TYPE_ENV;
  53. m_iCameraState = 'N';
  54. int iRecordMode = 0;
  55. CSmartPointer<IConfigInfo> spConfig;
  56. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  57. if (spFunction->OpenConfig(Config_CenterSetting, spConfig) == Error_Succeed) {
  58. spConfig->ReadConfigValueInt("InteractiveControl", "RecordMode", iRecordMode);
  59. }
  60. if (1 == iRecordMode) {
  61. m_iRecordMode = 1;
  62. }
  63. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Record Mode is %d.", m_iRecordMode);
  64. InitRecorder();
  65. int i = 0;
  66. m_arrListener.Init(5);
  67. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_BEGIN_RECORD, NULL, false);
  68. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_END_RECORD, NULL, false);
  69. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, NULL, false);
  70. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_SECTION_FINISHED, NULL, false);
  71. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_WHOLE_FINISHED, NULL, false);
  72. GetFunction()->RegistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA,this);
  73. GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE,this);
  74. CSimpleStringA strValue;
  75. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  76. m_iCameraState = strValue[0];
  77. if (strValue[0] == 'E'){
  78. m_iActiveCamera = CAMERA_TYPE_OPT;
  79. }
  80. else if (strValue[0] == 'O'){
  81. m_iActiveCamera = CAMERA_TYPE_ENV;
  82. }
  83. else if(strValue[0] == 'B'){
  84. m_iActiveCamera = CAMERA_TYPE_ERROR;
  85. }
  86. else if (strValue[0] == 'N'){
  87. m_iActiveCamera = CAMERA_TYPE_ENV;
  88. }
  89. Error = GetFunction()->RegistSysVarEvent("SessionID", this);
  90. if (Error != Error_Succeed) {
  91. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "SessionID");
  92. }
  93. Error = GetFunction()->GetPath("Temp", m_TempDir);
  94. if (Error != Error_Succeed) {
  95. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record temp path failed!");
  96. }
  97. if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != SPLIT_SLASH) {
  98. m_TempDir += SPLIT_SLASH_STR;
  99. }
  100. Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
  101. if (Error != Error_Succeed) {
  102. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record save path failed!");
  103. }
  104. if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
  105. m_RecordSaveDir += SPLIT_SLASH_STR;
  106. }
  107. return Error;
  108. }
  109. ErrorCodeEnum CRecorderEntity::__OnClose(ErrorCodeEnum preOperationError)
  110. {
  111. if (preOperationError != Error_Succeed) {
  112. return preOperationError;
  113. }
  114. for (int i = 0; i < m_arrListener.GetCount(); ++i) {
  115. GetFunction()->UnsubscribeLog(m_arrListener[i]);
  116. }
  117. GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
  118. GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
  119. StopRecord();
  120. return Error_Succeed;
  121. }
  122. bool CRecorderEntity::InitRecorder()
  123. {
  124. bool bRet = false;
  125. if ((ePadtype == m_eDeviceType) || (eMobilePadType == m_eDeviceType) || (eDesk2SType == m_eDeviceType)) {
  126. //pad 版增加远端视频队列
  127. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  128. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, NULL);
  129. }
  130. else {
  131. // == 2
  132. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  133. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  134. }
  135. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init libvideorecord success!");
  136. return bRet;
  137. }
  138. bool CRecorderEntity::ReleaseRecorder()
  139. {
  140. if (m_pRecorder) {
  141. delete m_pRecorder;
  142. m_pRecorder = NULL;
  143. }
  144. }
  145. CServerSessionBase* CRecorderEntity::OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
  146. {
  147. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s connected class = %s!", pszRemoteEntityName, pszClass);
  148. return new RecordServiceSession(this);
  149. }
  150. void CRecorderEntity::Debug(record_loglevel elevel, const char *fmt, ...)
  151. {
  152. if (RECORD_LOG_INFO <= elevel) {
  153. va_list arg;
  154. va_start(arg, fmt);
  155. int n = vsnprintf(NULL, 0, fmt, arg);
  156. if (n >= MAX_LOG_LEN) {
  157. char* buf = (char*)malloc((size_t)(n + 1));
  158. vsnprintf(buf, n + 1, fmt, arg);
  159. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  160. free(buf);
  161. }
  162. else{
  163. char strlog[MAX_LOG_LEN] = {0};
  164. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  165. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  166. }
  167. va_end(arg);
  168. }
  169. }
  170. void CRecorderEntity::vDebug(record_loglevel elevel, const char* str, va_list list)
  171. {
  172. if (RECORD_LOG_INFO <= elevel) {
  173. int n = vsnprintf(NULL, 0, str, list);
  174. if (n >= MAX_LOG_LEN) {
  175. char* buf = (char*)malloc((size_t)(n + 1));
  176. vsnprintf(buf, n + 1, str, list);
  177. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  178. free(buf);
  179. }
  180. else {
  181. char strlog[MAX_LOG_LEN] = { 0 };
  182. vsnprintf(strlog, MAX_LOG_LEN, str, list);
  183. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  184. }
  185. }
  186. }
  187. int CRecorderEntity::GetActiveCamera()
  188. {
  189. return m_iActiveCamera;
  190. }
  191. int CRecorderEntity::GetCameraState()
  192. {
  193. return m_iCameraState;
  194. }
  195. void CRecorderEntity::OnRecordFailed(eRvcRecordFailedCase eCase, const char *pszMessage, bool bRecordDevFault)
  196. {
  197. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnRecordFailed!");
  198. if (!bRecordDevFault){
  199. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"0");
  200. char strmsg[MAX_PATH] = {0};
  201. snprintf(strmsg, MAX_PATH, "{%s} 本地录音录像失败,已停止!", pszMessage ? pszMessage : " ");
  202. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDING_FAILED, strmsg);
  203. }
  204. else{
  205. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"1");
  206. char strinfo[MAX_PATH] = {0};
  207. snprintf(strinfo, MAX_PATH, "{%s} 本地录音录像设备故障,尝试恢复中(预计10s内),请稍等", pszMessage ? pszMessage : " ");
  208. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDING_FAILED, strinfo);
  209. }
  210. }
  211. void CRecorderEntity::OnRecordEntityExcption()
  212. {
  213. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_ENTITY_EXCEPTION, "OnRecordEntityExcption!");
  214. }
  215. void CRecorderEntity::OnRecordFinished()
  216. {
  217. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnRecordFinished!");
  218. }
  219. static unsigned long GetFileSize(const char* filename)
  220. {
  221. struct stat statbuf;
  222. stat(filename, &statbuf);
  223. return statbuf.st_size;
  224. }
  225. static void LogVideoSizeInfo(const char* pszMessage)
  226. {
  227. unsigned long ufilesize = GetFileSize(pszMessage);
  228. char strmsg[MAX_PATH] = { 0 };
  229. snprintf(strmsg, MAX_PATH, "%s file size is %u byte.", pszMessage, ufilesize);
  230. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, strmsg);
  231. }
  232. void CRecorderEntity::OnASectionFinished(const char* pszMessage, int iSerialNum, bool bfinished)
  233. {
  234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("file %s finished.(SerialNum %d)", pszMessage, iSerialNum);
  235. if (false == bfinished) {
  236. LogEvent(Severity_Middle, LOG_EVT_RECORDER_SECTION_FINISHED, pszMessage);
  237. }
  238. else {
  239. LogEvent(Severity_Middle, LOG_EVT_RECORDER_WHOLE_FINISHED, pszMessage);
  240. }
  241. }
  242. void CRecorderEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  243. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  244. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext& pLinkInfo)
  245. {
  246. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("user_code = 0x%08x.", dwUserCode);
  247. switch (dwUserCode)
  248. {
  249. case EVENT_MOD_BEGIN_RECORD:
  250. if (0 == m_iRecordMode) {
  251. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start record!");
  252. StartRecord(pszMessage);
  253. }
  254. break;
  255. case EVENT_MOD_END_RECORD:
  256. if (0 == m_iRecordMode) {
  257. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop record!");
  258. StopRecord();
  259. }
  260. break;
  261. case LOG_EVT_UI_RETURNMENU:
  262. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("exit to main!");
  263. if (0 == m_iRecordMode) {
  264. //本地录像,退回到首页关闭当前文件,重新开始录制
  265. if (m_bStarted) {
  266. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("return menu,close video file!");
  267. m_pRecorder->CloseVideoFile();
  268. }
  269. }
  270. break;
  271. case LOG_EVT_RECORDER_SECTION_FINISHED:
  272. {
  273. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recorder section finished, and message is %s.", pszMessage);
  274. if (m_iRecordMode) {
  275. LogVideoSizeInfo(pszMessage);
  276. }
  277. }
  278. break;
  279. case LOG_EVT_RECORDER_WHOLE_FINISHED:
  280. {
  281. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recorder whole finished, and message is %s.", pszMessage);
  282. if (m_iRecordMode) {
  283. LogVideoSizeInfo(pszMessage);
  284. }
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. }
  291. void CRecorderEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  292. {
  293. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  294. {
  295. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("camera state from : %c to %c", pszOldValue[0], pszValue[0]);
  296. m_iCameraState = pszValue[0];
  297. if (pszValue[0] == 'E'){
  298. m_iActiveCamera = CAMERA_TYPE_OPT;
  299. }
  300. else if (pszValue[0] == 'O'){
  301. m_iActiveCamera = CAMERA_TYPE_ENV;
  302. }
  303. else if(pszValue[0] == 'B'){
  304. m_iActiveCamera = CAMERA_TYPE_ERROR;
  305. }
  306. else if (pszValue[0] == 'N'){
  307. m_iActiveCamera = CAMERA_TYPE_ENV;
  308. }
  309. }
  310. else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
  311. {
  312. if (m_iCameraState == 'N'){
  313. if (pszValue[0] == 'E'){
  314. m_iActiveCamera = CAMERA_TYPE_ENV;
  315. }
  316. else if (pszValue[0] == 'O'){
  317. m_iActiveCamera = CAMERA_TYPE_OPT;
  318. }
  319. }
  320. }
  321. else if(_stricmp(pszKey,"SessionID")==0)
  322. {
  323. CSimpleStringA strSessionID;
  324. GetFunction()->GetSysVar("SessionID",strSessionID);
  325. //如果sessionid改变且不为空,切换录像文件
  326. if(_stricmp(strSessionID,"N")!=0){
  327. if (m_bStarted && (0 == m_iRecordMode)){
  328. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Session Id change to %s, close record and start new video file.",strSessionID.GetData());
  329. m_pRecorder->ReNameVideoFile(strSessionID);
  330. }
  331. }
  332. }
  333. }
  334. void CRecorderEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  335. {
  336. if (Test_ShakeHand == eTestType){
  337. pTransactionContext->SendAnswer(Error_Succeed);
  338. }
  339. }
  340. void CRecorderEntity::StartRecord(const char *videofilename)
  341. {
  342. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videofilename = %s", videofilename);
  343. int fps = 5;
  344. Rvc_RecordAudioParam_t tAudioParams;
  345. tAudioParams.eRecordType = eSingleSide;
  346. tAudioParams.eOutPutType = eLowDefinition;
  347. tAudioParams.bIsNsOn = true;
  348. tAudioParams.iNsPolicy = 2;
  349. tAudioParams.iAudioOutBitRate = 8;
  350. tAudioParams.bIsTransOn = false;
  351. tAudioParams.iAudioChannels = 1;
  352. if (m_pRecorder->StartVideoRecord(fps, 75, m_eRecordType, &tAudioParams, NULL, FALSE, TRUE, (LPCSTR)m_RecordSaveDir, m_RecordSaveDir.GetLength(), videofilename, strlen(videofilename)))
  353. {
  354. m_bStarted = TRUE;
  355. }
  356. }
  357. void CRecorderEntity::StopRecord()
  358. {
  359. if (m_bStarted) {
  360. m_pRecorder->StopVideoRecord();
  361. m_bStarted = false;
  362. }
  363. }
  364. DeviceTypeEnum CRecorderEntity::RvcGetDeviceType()
  365. {
  366. DeviceTypeEnum eType = eStand2sType;
  367. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  368. CSystemStaticInfo stStaticinfo;
  369. spFunction->GetSystemStaticInfo(stStaticinfo);
  370. if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
  371. eType = eStand1SPlusType;
  372. }
  373. else if (_stricmp(stStaticinfo.strMachineType, "RVC.PAD") == 0) {
  374. if (_stricmp(stStaticinfo.strSite, "CMB.FLB") == 0) {
  375. eType = eMobilePadType;
  376. }
  377. else {
  378. eType = ePadtype;
  379. }
  380. }
  381. else if (_stricmp(stStaticinfo.strMachineType, "RVC.Desk2S") == 0) {
  382. eType = eDesk2SType;
  383. WORD nMajor = stStaticinfo.MachineVersion.GetMajor();
  384. WORD nMinor = stStaticinfo.MachineVersion.GetMinor();
  385. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("MachineVersion is %d.%d", nMajor, nMinor);
  386. if (2 == nMajor) {
  387. eType = eDesk2SIntegratedType;
  388. }
  389. }
  390. else if (_stricmp(stStaticinfo.strMachineType, "RPM.Stand1S") == 0) {
  391. eType = eRpm1sType;
  392. }
  393. else if (_stricmp(stStaticinfo.strMachineType, "RVC.Desk1S") == 0) {
  394. eType = eDesk1SType;
  395. }
  396. else if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
  397. eType = eCardStore;
  398. }
  399. else {
  400. eType = eStand2sType;
  401. }
  402. if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
  403. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  404. }
  405. return eType;
  406. }
  407. SP_BEGIN_ENTITY_MAP()
  408. SP_ENTITY(CRecorderEntity)
  409. SP_END_ENTITY_MAP()