mod_recorder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpIni.h"
  4. #include "libwmvrecord.h"
  5. #include "rec_common.h"
  6. #include "fileutil.h"
  7. #include "array.h"
  8. #include <memutil.h>
  9. #include <algorithm>
  10. #include "mod_customeraware/Event.h"
  11. #include "mod_facetracking/sysvar.h"
  12. #include <assert.h>
  13. #include "EventCode.h"
  14. extern "C"
  15. {
  16. #include <libavutil/avutil.h>
  17. }
  18. #define LOG_EVT_UI_RETURNMENU 0x30B00006 //退出到主菜单
  19. #define LOG_EVT_RECORDFAILED 0x31200001 //录像失败
  20. static void logCallbacks(void* ptr, int level, const char* fmt, va_list list)
  21. {
  22. vDbg(fmt, list);
  23. }
  24. class CRecorderEntity : public CEntityBase, public CWmvHostApi, public ILogListener,public ISysVarListener
  25. {
  26. public:
  27. CRecorderEntity(): m_bStarted(FALSE), m_pRecorder(NULL) {}
  28. virtual ~CRecorderEntity() {}
  29. virtual const char *GetEntityName() const { return "Recorder"; }
  30. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  31. {
  32. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  33. pTransactionContext->SendAnswer(Error);
  34. }
  35. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  36. {
  37. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  38. pTransactionContext->SendAnswer(Error);
  39. }
  40. BOOL InitRecorder()
  41. {
  42. BOOL bRet = FALSE;
  43. if ((ePadtype == m_eDeviceType) || (eMobilePadType == m_eDeviceType) || (eDesk2SType == m_eDeviceType))
  44. {
  45. //pad 版增加远端视频队列
  46. m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  47. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, NULL);
  48. }
  49. else
  50. {
  51. // == 2
  52. m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  53. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  54. }
  55. Dbg("init libwmvrecord success!");
  56. return bRet;
  57. }
  58. BOOL ReleaseRecorder()
  59. {
  60. if (m_pRecorder) {
  61. delete m_pRecorder;
  62. m_pRecorder = NULL;
  63. }
  64. }
  65. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  66. {
  67. LOG_FUNCTION();
  68. //MessageBoxA(0,0,0,0);
  69. m_eDeviceType = eStand2sType;
  70. //is Pad Version
  71. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  72. CSystemStaticInfo stStaticinfo;
  73. spFunction->GetSystemStaticInfo(stStaticinfo);
  74. if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
  75. {
  76. if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
  77. {
  78. LOG_TRACE("the type is mobile pad");
  79. m_eDeviceType = eMobilePadType;
  80. }
  81. else
  82. {
  83. LOG_TRACE("the type is pad");
  84. m_eDeviceType = ePadtype;
  85. }
  86. }
  87. else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
  88. {
  89. LOG_TRACE("the type is rpm.stand1s");
  90. m_eDeviceType = eRpm1sType;
  91. }
  92. else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
  93. {
  94. LOG_TRACE("the type is Desk2S");
  95. m_eDeviceType = eDesk2SType;
  96. }
  97. else
  98. {
  99. LOG_TRACE("the type is standard");
  100. m_eDeviceType = eStand2sType;
  101. }
  102. m_terminalNo = stStaticinfo.strTerminalID;
  103. if (preOperationError != Error_Succeed)
  104. return preOperationError;
  105. ErrorCodeEnum Error = Error_Succeed;
  106. nActiveCamera = CAMERA_TYPE_ENV;
  107. m_iCameraState = 'N';
  108. //int nCameraCount = 0;
  109. //Error = DecideCameraCount(nCameraCount);
  110. //if (Error != Error_Succeed || nCameraCount < 0 || nCameraCount > 2) {
  111. // LOG_TRACE("decide camera count failed!");
  112. // return Error;
  113. //}
  114. InitRecorder();
  115. GetFunction()->SubscribeLog(m_SubIDStartRecord, this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_BEGIN_RECORD, NULL, false);
  116. GetFunction()->SubscribeLog(m_SubIDStopRecord, this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_END_RECORD, NULL, false);
  117. GetFunction()->SubscribeLog(m_SubIDReturnMenu, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, NULL, false);
  118. GetFunction()->RegistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA,this);
  119. GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE,this);
  120. CSimpleStringA strValue;
  121. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  122. m_iCameraState = strValue[0];
  123. if (strValue[0] == 'E')
  124. {
  125. nActiveCamera = CAMERA_TYPE_OPT;
  126. }
  127. else if (strValue[0] == 'O')
  128. {
  129. nActiveCamera = CAMERA_TYPE_ENV;
  130. }
  131. else if(strValue[0] == 'B') ///////显示贴图
  132. {
  133. nActiveCamera = CAMERA_TYPE_ERROR;
  134. }
  135. else if (strValue[0] == 'N')
  136. {
  137. nActiveCamera = CAMERA_TYPE_ENV;
  138. }
  139. Error = GetFunction()->RegistSysVarEvent("SessionID", this);
  140. if (Error != Error_Succeed)
  141. {
  142. LOG_TRACE("register sysvar %s failed!", "SessionID");
  143. }
  144. // add by ly 2018/02/13
  145. Error = GetFunction()->GetPath("Temp", m_TempDir);
  146. if (Error != Error_Succeed) {
  147. LOG_TRACE("get global record temp path failed!");
  148. }
  149. if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != SPLIT_SLASH) {
  150. m_TempDir += SPLIT_SLASH_STR;
  151. }
  152. Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
  153. if (Error != Error_Succeed) {
  154. LOG_TRACE("get global record save path failed!");
  155. }
  156. if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
  157. m_RecordSaveDir += SPLIT_SLASH_STR;
  158. }
  159. //av_log_set_level(AV_LOG_DEBUG);
  160. //av_log_set_callback(logCallbacks);
  161. return Error;
  162. }
  163. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  164. {
  165. LOG_FUNCTION();
  166. if (preOperationError != Error_Succeed)
  167. return preOperationError;
  168. GetFunction()->UnsubscribeLog(m_SubIDStopRecord);
  169. GetFunction()->UnsubscribeLog(m_SubIDStartRecord);
  170. GetFunction()->UnsubscribeLog(m_SubIDReturnMenu);
  171. GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
  172. GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
  173. StopRecord();
  174. //av_log_set_callback(NULL);
  175. return Error_Succeed;
  176. }
  177. // CAviHostAPI
  178. virtual void Debug(const char *fmt, ...)
  179. {
  180. va_list arg;
  181. va_start(arg, fmt);
  182. vDbg(fmt, arg);
  183. va_end(arg);
  184. }
  185. // CWmpHostAPI
  186. virtual void WmpDebug(const char *fmt, ...)
  187. {
  188. va_list arg;
  189. va_start(arg, fmt);
  190. vDbg(fmt, arg);
  191. va_end(arg);
  192. }
  193. virtual int GetActiveCamera()
  194. {
  195. //Debug("get camera = %d",nActiveCamera);
  196. return nActiveCamera;
  197. }
  198. virtual void OnRecordFailed(bool bRecordDevFault)
  199. {
  200. Dbg("OnRecordFailed!");
  201. if (!bRecordDevFault)
  202. {
  203. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"0");
  204. }
  205. else
  206. {
  207. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"1");
  208. }
  209. }
  210. virtual void OnRecordEntityExcption()
  211. {
  212. Dbg("OnRecordEntityExcption!");
  213. }
  214. virtual void OnRecordFinished()
  215. {
  216. Dbg("OnRecordFinished!");
  217. }
  218. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  219. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  220. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
  221. {
  222. if (/*(dwUserCode == EVENT_MOD_CUSTOMERAWARE_BEGIN)||*/(dwUserCode == EVENT_MOD_BEGIN_RECORD))
  223. {
  224. Dbg("start record!");
  225. StartRecord(pszMessage);
  226. }
  227. else if (/*(dwUserCode == EVENT_MOD_CUSTOMERAWARE_END)||*/(dwUserCode == EVENT_MOD_END_RECORD))
  228. {
  229. Dbg("stop record!");
  230. StopRecord();
  231. //ReleaseRecorder();
  232. }
  233. else if (dwUserCode == LOG_EVT_UI_RETURNMENU) // 返回主菜单
  234. {
  235. //本地录像,退回到首页关闭当前文件,重新开始录制
  236. if (m_bStarted&&m_pRecorder)
  237. {
  238. Dbg("return menu,close video file!");
  239. m_pRecorder->CloseVideoFile();
  240. }
  241. }
  242. }
  243. private:
  244. virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  245. {
  246. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  247. {
  248. Dbg("camera state from : %c to %c", pszOldValue[0], pszValue[0]);
  249. m_iCameraState = pszValue[0];
  250. if (pszValue[0] == 'E')
  251. {
  252. nActiveCamera = CAMERA_TYPE_OPT;
  253. }
  254. else if (pszValue[0] == 'O')
  255. {
  256. nActiveCamera = CAMERA_TYPE_ENV;
  257. }
  258. else if(pszValue[0] == 'B') ///////显示贴图
  259. {
  260. nActiveCamera = CAMERA_TYPE_ERROR;
  261. }
  262. else if (pszValue[0] == 'N')
  263. {
  264. nActiveCamera = CAMERA_TYPE_AUTO;
  265. }
  266. }
  267. else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
  268. {
  269. if (m_iCameraState == 'N')
  270. {
  271. if (pszValue[0] == 'E')
  272. {
  273. nActiveCamera = CAMERA_TYPE_ENV;
  274. }
  275. else if (pszValue[0] == 'O')
  276. {
  277. nActiveCamera = CAMERA_TYPE_OPT;
  278. }
  279. }
  280. }
  281. else if(_stricmp(pszKey,"SessionID")==0)
  282. {
  283. CSimpleStringA strSessionID;
  284. GetFunction()->GetSysVar("SessionID",strSessionID);
  285. //如果sessionid改变且不为空,切换录像文件
  286. if(_stricmp(strSessionID,"N")!=0)
  287. {
  288. if (m_bStarted)
  289. {
  290. Dbg("Sessionid change to %s,close record and start new video file",strSessionID);
  291. m_pRecorder->ReNameVideoFile(strSessionID);
  292. }
  293. }
  294. }
  295. }
  296. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  297. {
  298. if (Test_ShakeHand == eTestType)
  299. {
  300. pTransactionContext->SendAnswer(Error_Succeed);
  301. }
  302. }
  303. // edit by ly@2018/06/07
  304. void StartRecord(const char *wmvfilename)
  305. {
  306. Dbg("wmvfilename = %s", wmvfilename);
  307. //Dbg("strPath = %s", (LPCSTR)m_RecordSaveDir);
  308. int fps = 5;
  309. Rvc_RecordAudioParam_t tAudioParams;
  310. tAudioParams.eRecordType = eSingleSide;
  311. tAudioParams.eOutPutType = eLowDefinition;
  312. tAudioParams.bIsNsOn = true;
  313. tAudioParams.iNsPolicy = 2;
  314. tAudioParams.iAudioOutBitRate = 8;
  315. tAudioParams.bIsTransOn = false;
  316. if (m_pRecorder->StartWmvRecord(fps, 75, &tAudioParams, NULL, FALSE, TRUE, (LPCSTR)m_RecordSaveDir, m_RecordSaveDir.GetLength(), wmvfilename, strlen(wmvfilename)))
  317. {
  318. m_bStarted = TRUE;
  319. }
  320. }
  321. // we use root.ini Video section config to decide camera count
  322. ErrorCodeEnum DecideCameraCount(int &nCount)
  323. {
  324. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  325. CSmartPointer<IConfigInfo> spConfig;
  326. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
  327. if (Error == Error_Succeed) {
  328. CSimpleStringA strVideoEnv;
  329. CSimpleStringA strVideoOpt;
  330. SpIniMappingTable table;
  331. nCount = 0;
  332. table.AddEntryString("Video", "EnvCamera", strVideoEnv, "$");
  333. table.AddEntryString("Video", "OptCamera", strVideoOpt, "$");
  334. Error = table.Load(spConfig);
  335. if (Error == Error_Succeed) {
  336. if (strVideoEnv.GetLength() > 1)
  337. nCount++;
  338. if (strVideoOpt.GetLength() > 1)
  339. nCount++;
  340. }
  341. }
  342. return Error;
  343. }
  344. void StopRecord()
  345. {
  346. if (m_bStarted)
  347. {
  348. m_pRecorder->StopWmvRecord();
  349. m_bStarted = FALSE;
  350. }
  351. }
  352. DeviceTypeEnum m_eDeviceType;
  353. int nActiveCamera;
  354. int m_iCameraState;
  355. CUUID m_SubIDStartRecord;
  356. CUUID m_SubIDStopRecord;
  357. CUUID m_SubIDReturnMenu;
  358. BOOL m_bStarted;
  359. Clibwmvrecord *m_pRecorder;
  360. CSimpleStringA m_terminalNo; // 设备终端号 add by ly 20180213
  361. CSimpleStringA m_TempDir; // 录像临时目录 add by ly 20180213
  362. CSimpleStringA m_RecordSaveDir; // 录像上传目录 add by ly 20180213
  363. };
  364. SP_BEGIN_ENTITY_MAP()
  365. SP_ENTITY(CRecorderEntity)
  366. SP_END_ENTITY_MAP()