123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpIni.h"
- #include "libwmvrecord.h"
- #include "rec_common.h"
- #include "fileutil.h"
- #include "array.h"
- #include <memutil.h>
- #include <algorithm>
- #include "mod_customeraware/Event.h"
- #include "mod_facetracking/sysvar.h"
- #include <assert.h>
- #include "EventCode.h"
- extern "C"
- {
- #include <libavutil/avutil.h>
- }
- #define LOG_EVT_UI_RETURNMENU 0x30B00006 //退出到主菜单
- #define LOG_EVT_RECORDFAILED 0x31200001 //录像失败
- static void logCallbacks(void* ptr, int level, const char* fmt, va_list list)
- {
- vDbg(fmt, list);
- }
- class CRecorderEntity : public CEntityBase, public CWmvHostApi, public ILogListener,public ISysVarListener
- {
- public:
- CRecorderEntity(): m_bStarted(FALSE), m_pRecorder(NULL) {}
- virtual ~CRecorderEntity() {}
- virtual const char *GetEntityName() const { return "Recorder"; }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnStart(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnClose(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- BOOL InitRecorder()
- {
- BOOL bRet = FALSE;
- if ((ePadtype == m_eDeviceType) || (eMobilePadType == m_eDeviceType) || (eDesk2SType == m_eDeviceType))
- {
- //pad 版增加远端视频队列
- m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
- REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, NULL);
- }
- else
- {
- // == 2
- m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
- REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
- }
- Dbg("init libwmvrecord success!");
- return bRet;
- }
- BOOL ReleaseRecorder()
- {
- if (m_pRecorder) {
- delete m_pRecorder;
- m_pRecorder = NULL;
- }
- }
- ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
- {
- LOG_FUNCTION();
- //MessageBoxA(0,0,0,0);
- m_eDeviceType = eStand2sType;
- //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,"RPM.Stand1S")==0)
- {
- LOG_TRACE("the type is rpm.stand1s");
- m_eDeviceType = eRpm1sType;
- }
- else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
- {
- LOG_TRACE("the type is Desk2S");
- m_eDeviceType = eDesk2SType;
- }
- else
- {
- LOG_TRACE("the type is standard");
- m_eDeviceType = eStand2sType;
- }
- m_terminalNo = stStaticinfo.strTerminalID;
- if (preOperationError != Error_Succeed)
- return preOperationError;
- ErrorCodeEnum Error = Error_Succeed;
- nActiveCamera = CAMERA_TYPE_ENV;
- m_iCameraState = 'N';
- //int nCameraCount = 0;
- //Error = DecideCameraCount(nCameraCount);
- //if (Error != Error_Succeed || nCameraCount < 0 || nCameraCount > 2) {
- // LOG_TRACE("decide camera count failed!");
- // return Error;
- //}
- InitRecorder();
-
- GetFunction()->SubscribeLog(m_SubIDStartRecord, this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_BEGIN_RECORD, NULL, false);
- GetFunction()->SubscribeLog(m_SubIDStopRecord, this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_END_RECORD, NULL, false);
- GetFunction()->SubscribeLog(m_SubIDReturnMenu, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, NULL, false);
- GetFunction()->RegistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA,this);
- GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE,this);
- CSimpleStringA strValue;
- GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
- m_iCameraState = strValue[0];
- if (strValue[0] == 'E')
- {
- nActiveCamera = CAMERA_TYPE_OPT;
- }
- else if (strValue[0] == 'O')
- {
- nActiveCamera = CAMERA_TYPE_ENV;
- }
- else if(strValue[0] == 'B') ///////显示贴图
- {
- nActiveCamera = CAMERA_TYPE_ERROR;
- }
- else if (strValue[0] == 'N')
- {
- nActiveCamera = CAMERA_TYPE_ENV;
- }
-
- Error = GetFunction()->RegistSysVarEvent("SessionID", this);
- if (Error != Error_Succeed)
- {
- LOG_TRACE("register sysvar %s failed!", "SessionID");
- }
- // add by ly 2018/02/13
- Error = GetFunction()->GetPath("Temp", m_TempDir);
- if (Error != Error_Succeed) {
- LOG_TRACE("get global record temp path failed!");
- }
- if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != SPLIT_SLASH) {
- m_TempDir += SPLIT_SLASH_STR;
- }
- Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
- if (Error != Error_Succeed) {
- LOG_TRACE("get global record save path failed!");
- }
- if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
- m_RecordSaveDir += SPLIT_SLASH_STR;
- }
- //av_log_set_level(AV_LOG_DEBUG);
- //av_log_set_callback(logCallbacks);
- return Error;
- }
- ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
- {
- LOG_FUNCTION();
- if (preOperationError != Error_Succeed)
- return preOperationError;
- GetFunction()->UnsubscribeLog(m_SubIDStopRecord);
- GetFunction()->UnsubscribeLog(m_SubIDStartRecord);
- GetFunction()->UnsubscribeLog(m_SubIDReturnMenu);
- GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
- GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
- StopRecord();
- //av_log_set_callback(NULL);
- return Error_Succeed;
- }
- // CAviHostAPI
- virtual void Debug(const char *fmt, ...)
- {
- va_list arg;
- va_start(arg, fmt);
- vDbg(fmt, arg);
- va_end(arg);
- }
- // CWmpHostAPI
- virtual void WmpDebug(const char *fmt, ...)
- {
- va_list arg;
- va_start(arg, fmt);
- vDbg(fmt, arg);
- va_end(arg);
- }
- virtual int GetActiveCamera()
- {
- //Debug("get camera = %d",nActiveCamera);
- return nActiveCamera;
- }
- virtual void OnRecordFailed(bool bRecordDevFault)
- {
- Dbg("OnRecordFailed!");
- if (!bRecordDevFault)
- {
- LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"0");
- }
- else
- {
- LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"1");
- }
- }
- virtual void OnRecordEntityExcption()
- {
- Dbg("OnRecordEntityExcption!");
- }
- virtual void OnRecordFinished()
- {
- Dbg("OnRecordFinished!");
- }
- virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
- const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
- const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
- {
- if (/*(dwUserCode == EVENT_MOD_CUSTOMERAWARE_BEGIN)||*/(dwUserCode == EVENT_MOD_BEGIN_RECORD))
- {
- Dbg("start record!");
- StartRecord(pszMessage);
- }
- else if (/*(dwUserCode == EVENT_MOD_CUSTOMERAWARE_END)||*/(dwUserCode == EVENT_MOD_END_RECORD))
- {
- Dbg("stop record!");
- StopRecord();
- //ReleaseRecorder();
- }
- else if (dwUserCode == LOG_EVT_UI_RETURNMENU) // 返回主菜单
- {
- //本地录像,退回到首页关闭当前文件,重新开始录制
- if (m_bStarted&&m_pRecorder)
- {
- Dbg("return menu,close video file!");
- m_pRecorder->CloseVideoFile();
- }
- }
- }
-
- private:
- virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
- {
- Dbg("camera state from : %c to %c", pszOldValue[0], pszValue[0]);
- m_iCameraState = pszValue[0];
- if (pszValue[0] == 'E')
- {
- nActiveCamera = CAMERA_TYPE_OPT;
- }
- else if (pszValue[0] == 'O')
- {
- nActiveCamera = CAMERA_TYPE_ENV;
- }
- else if(pszValue[0] == 'B') ///////显示贴图
- {
- nActiveCamera = CAMERA_TYPE_ERROR;
- }
- else if (pszValue[0] == 'N')
- {
- nActiveCamera = CAMERA_TYPE_AUTO;
- }
- }
- else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
- {
- if (m_iCameraState == 'N')
- {
- if (pszValue[0] == 'E')
- {
- nActiveCamera = CAMERA_TYPE_ENV;
- }
- else if (pszValue[0] == 'O')
- {
- nActiveCamera = CAMERA_TYPE_OPT;
- }
- }
- }
- else if(_stricmp(pszKey,"SessionID")==0)
- {
- CSimpleStringA strSessionID;
- GetFunction()->GetSysVar("SessionID",strSessionID);
- //如果sessionid改变且不为空,切换录像文件
- if(_stricmp(strSessionID,"N")!=0)
- {
- if (m_bStarted)
- {
- Dbg("Sessionid change to %s,close record and start new video file",strSessionID);
- m_pRecorder->ReNameVideoFile(strSessionID);
- }
- }
- }
- }
- virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- if (Test_ShakeHand == eTestType)
- {
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- }
- // edit by ly@2018/06/07
- void StartRecord(const char *wmvfilename)
- {
- Dbg("wmvfilename = %s", wmvfilename);
- //Dbg("strPath = %s", (LPCSTR)m_RecordSaveDir);
- int fps = 5;
- Rvc_RecordAudioParam_t tAudioParams;
- tAudioParams.eRecordType = eSingleSide;
- tAudioParams.eOutPutType = eLowDefinition;
- tAudioParams.bIsNsOn = true;
- tAudioParams.iNsPolicy = 2;
- tAudioParams.iAudioOutBitRate = 8;
- tAudioParams.bIsTransOn = false;
- if (m_pRecorder->StartWmvRecord(fps, 75, &tAudioParams, NULL, FALSE, TRUE, (LPCSTR)m_RecordSaveDir, m_RecordSaveDir.GetLength(), wmvfilename, strlen(wmvfilename)))
- {
- m_bStarted = TRUE;
- }
- }
- // we use root.ini Video section config to decide camera count
- ErrorCodeEnum DecideCameraCount(int &nCount)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
- if (Error == Error_Succeed) {
- CSimpleStringA strVideoEnv;
- CSimpleStringA strVideoOpt;
- SpIniMappingTable table;
- nCount = 0;
- table.AddEntryString("Video", "EnvCamera", strVideoEnv, "$");
- table.AddEntryString("Video", "OptCamera", strVideoOpt, "$");
- Error = table.Load(spConfig);
- if (Error == Error_Succeed) {
- if (strVideoEnv.GetLength() > 1)
- nCount++;
- if (strVideoOpt.GetLength() > 1)
- nCount++;
- }
- }
- return Error;
- }
- void StopRecord()
- {
- if (m_bStarted)
- {
- m_pRecorder->StopWmvRecord();
- m_bStarted = FALSE;
- }
- }
- DeviceTypeEnum m_eDeviceType;
- int nActiveCamera;
- int m_iCameraState;
- CUUID m_SubIDStartRecord;
- CUUID m_SubIDStopRecord;
- CUUID m_SubIDReturnMenu;
- BOOL m_bStarted;
- Clibwmvrecord *m_pRecorder;
- CSimpleStringA m_terminalNo; // 设备终端号 add by ly 20180213
- CSimpleStringA m_TempDir; // 录像临时目录 add by ly 20180213
- CSimpleStringA m_RecordSaveDir; // 录像上传目录 add by ly 20180213
- };
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CRecorderEntity)
- SP_END_ENTITY_MAP()
|