123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- #include "mod_recorder.h"
- #include "fileutil.h"
- #include "array.h"
- #include <memutil.h>
- #include <algorithm>
- #include <sys/stat.h>
- #include "mod_customeraware/Event.h"
- #include "mod_facetracking/sysvar.h"
- #include <assert.h>
- #include "Event.h"
- using namespace Recorder;
- #ifndef MAX_LOG_LEN
- #define MAX_LOG_LEN 512
- #endif
- #ifndef RVC_TRANSATCION_RECORD_SUFFIX
- #define RVC_TRANSATCION_RECORD_SUFFIX "B_"
- #endif // !RVC_TRANSATCION_RECORD_SUFFIX
- void RecordServiceSession::Handle_StartTransactionRecord(SpReqAnsContext<RecorderSerVice_StartTransactionRecord_Req, RecorderSerVice_StartTransactionRecord_Ans>::Pointer ctx)
- {
- if (m_pEntity->GetStartFlag()) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Current is recording, stop it.");
- m_pEntity->StopRecord();
- }
- char strVideoName[MAX_PATH] = { 0 };
- snprintf(strVideoName, MAX_PATH, "%s%s",RVC_TRANSATCION_RECORD_SUFFIX, ctx->Req.VideoName.GetData());
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("StartRecord and video name is %s.", strVideoName);
- m_pEntity->StartRecord(strVideoName);
- ctx->Answer(Error_Succeed);
- }
- void RecordServiceSession::Handle_StopTransactionRecord(SpReqAnsContext<RecorderSerVice_StopTransactionRecord_Req, RecorderSerVice_StopTransactionRecord_Ans>::Pointer ctx)
- {
- m_pEntity->StopRecord();
- ctx->Answer(Error_Succeed);
- }
- void CRecorderEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnStart(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- void CRecorderEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnClose(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- ErrorCodeEnum CRecorderEntity::__OnStart(ErrorCodeEnum preOperationError)
- {
- ErrorCodeEnum Error = Error_Succeed;
- m_eDeviceType = RvcGetDeviceType();
- if (preOperationError != Error_Succeed) {
- return preOperationError;
- }
-
- m_iActiveCamera = CAMERA_TYPE_ENV;
- m_iCameraState = 'N';
- int iRecordMode = 0;
- CSmartPointer<IConfigInfo> spConfig;
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- if (spFunction->OpenConfig(Config_CenterSetting, spConfig) == Error_Succeed) {
- spConfig->ReadConfigValueInt("InteractiveControl", "RecordMode", iRecordMode);
- }
- if (1 == iRecordMode) {
- m_iRecordMode = 1;
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Record Mode is %d.", m_iRecordMode);
- InitRecorder();
-
- int i = 0;
- m_arrListener.Init(5);
- GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_BEGIN_RECORD, NULL, false);
- GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_END_RECORD, NULL, false);
- GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, NULL, false);
- GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_SECTION_FINISHED, NULL, false);
- GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_WHOLE_FINISHED, 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'){
- m_iActiveCamera = CAMERA_TYPE_OPT;
- }
- else if (strValue[0] == 'O'){
- m_iActiveCamera = CAMERA_TYPE_ENV;
- }
- else if(strValue[0] == 'B'){
- m_iActiveCamera = CAMERA_TYPE_ERROR;
- }
- else if (strValue[0] == 'N'){
- m_iActiveCamera = CAMERA_TYPE_ENV;
- }
-
- Error = GetFunction()->RegistSysVarEvent("SessionID", this);
- if (Error != Error_Succeed) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "SessionID");
- }
- Error = GetFunction()->GetPath("Temp", m_TempDir);
- if (Error != Error_Succeed) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("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) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record save path failed!");
- }
- if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
- m_RecordSaveDir += SPLIT_SLASH_STR;
- }
- return Error;
- }
- ErrorCodeEnum CRecorderEntity::__OnClose(ErrorCodeEnum preOperationError)
- {
- if (preOperationError != Error_Succeed) {
- return preOperationError;
- }
-
- for (int i = 0; i < m_arrListener.GetCount(); ++i) {
- GetFunction()->UnsubscribeLog(m_arrListener[i]);
- }
- GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
- GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
- StopRecord();
- return Error_Succeed;
- }
- bool CRecorderEntity::InitRecorder()
- {
- bool bRet = false;
- if ((ePadtype == m_eDeviceType) || (eMobilePadType == m_eDeviceType) || (eDesk2SType == m_eDeviceType)) {
- //pad 版增加远端视频队列
- m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
- REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, NULL);
- }
- else {
- // == 2
- m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
- REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
- }
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("init libvideorecord success!");
- return bRet;
- }
- bool CRecorderEntity::ReleaseRecorder()
- {
- if (m_pRecorder) {
- delete m_pRecorder;
- m_pRecorder = NULL;
- }
- }
- CServerSessionBase* CRecorderEntity::OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
- {
- DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("%s connected class = %s!", pszRemoteEntityName, pszClass);
- return new RecordServiceSession(this);
- }
- void CRecorderEntity::Debug(record_loglevel elevel, const char *fmt, ...)
- {
- if (RECORD_LOG_INFO <= elevel) {
- va_list arg;
- va_start(arg, fmt);
- int n = vsnprintf(NULL, 0, fmt, arg);
- if (n >= MAX_LOG_LEN) {
- char* buf = (char*)malloc((size_t)(n + 1));
- vsnprintf(buf, n + 1, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
- free(buf);
- }
- else{
- char strlog[MAX_LOG_LEN] = {0};
- vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
- }
- va_end(arg);
- }
- }
- void CRecorderEntity::vDebug(record_loglevel elevel, const char* str, va_list list)
- {
- if (RECORD_LOG_INFO <= elevel) {
- int n = vsnprintf(NULL, 0, str, list);
- if (n >= MAX_LOG_LEN) {
- char* buf = (char*)malloc((size_t)(n + 1));
- vsnprintf(buf, n + 1, str, list);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
- free(buf);
- }
- else {
- char strlog[MAX_LOG_LEN] = { 0 };
- vsnprintf(strlog, MAX_LOG_LEN, str, list);
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
- }
- }
- }
- int CRecorderEntity::GetActiveCamera()
- {
- return m_iActiveCamera;
- }
- int CRecorderEntity::GetCameraState()
- {
- return m_iCameraState;
- }
- void CRecorderEntity::OnRecordFailed(eRvcRecordFailedCase eCase, const char *pszMessage, bool bRecordDevFault)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnRecordFailed!");
- if (!bRecordDevFault){
- LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"0");
- char strmsg[MAX_PATH] = {0};
- snprintf(strmsg, MAX_PATH, "{%s} 本地录音录像失败,已停止!", pszMessage ? pszMessage : " ");
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDING_FAILED, strmsg);
- }
- else{
- LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"1");
- char strinfo[MAX_PATH] = {0};
- snprintf(strinfo, MAX_PATH, "{%s} 本地录音录像设备故障,尝试恢复中(预计10s内),请稍等", pszMessage ? pszMessage : " ");
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDING_FAILED, strinfo);
- }
- }
- void CRecorderEntity::OnRecordEntityExcption()
- {
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_ENTITY_EXCEPTION, "OnRecordEntityExcption!");
- }
- void CRecorderEntity::OnRecordFinished()
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnRecordFinished!");
- }
- static unsigned long GetFileSize(const char* filename)
- {
- struct stat statbuf;
- stat(filename, &statbuf);
- return statbuf.st_size;
- }
- static void LogVideoSizeInfo(const char* pszMessage)
- {
- unsigned long ufilesize = GetFileSize(pszMessage);
- char strmsg[MAX_PATH] = { 0 };
- snprintf(strmsg, MAX_PATH, "%s file size is %u byte.", pszMessage, ufilesize);
- LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, strmsg);
- }
- void CRecorderEntity::OnASectionFinished(const char* pszMessage, int iSerialNum, bool bfinished)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("file %s finished.(SerialNum %d)", pszMessage, iSerialNum);
- if (false == bfinished) {
- LogEvent(Severity_Middle, LOG_EVT_RECORDER_SECTION_FINISHED, pszMessage);
- }
- else {
- LogEvent(Severity_Middle, LOG_EVT_RECORDER_WHOLE_FINISHED, pszMessage);
- }
- }
- void CRecorderEntity::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, const linkContext& pLinkInfo)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("user_code = 0x%08x.", dwUserCode);
- switch (dwUserCode)
- {
- case EVENT_MOD_BEGIN_RECORD:
- if (0 == m_iRecordMode) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start record!");
- StartRecord(pszMessage);
- }
- break;
- case EVENT_MOD_END_RECORD:
- if (0 == m_iRecordMode) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("stop record!");
- StopRecord();
- }
- break;
- case LOG_EVT_UI_RETURNMENU:
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("exit to main!");
- if (0 == m_iRecordMode) {
- //本地录像,退回到首页关闭当前文件,重新开始录制
- if (m_bStarted) {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("return menu,close video file!");
- m_pRecorder->CloseVideoFile();
- }
- }
- break;
- case LOG_EVT_RECORDER_SECTION_FINISHED:
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recorder section finished, and message is %s.", pszMessage);
- if (m_iRecordMode) {
- LogVideoSizeInfo(pszMessage);
- }
- }
- break;
- case LOG_EVT_RECORDER_WHOLE_FINISHED:
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recorder whole finished, and message is %s.", pszMessage);
- if (m_iRecordMode) {
- LogVideoSizeInfo(pszMessage);
- }
- }
- break;
- default:
- break;
- }
- }
-
- void CRecorderEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("camera state from : %c to %c", pszOldValue[0], pszValue[0]);
- m_iCameraState = pszValue[0];
- if (pszValue[0] == 'E'){
- m_iActiveCamera = CAMERA_TYPE_OPT;
- }
- else if (pszValue[0] == 'O'){
- m_iActiveCamera = CAMERA_TYPE_ENV;
- }
- else if(pszValue[0] == 'B'){
- m_iActiveCamera = CAMERA_TYPE_ERROR;
- }
- else if (pszValue[0] == 'N'){
- m_iActiveCamera = CAMERA_TYPE_ENV;
- }
- }
- else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
- {
- if (m_iCameraState == 'N'){
- if (pszValue[0] == 'E'){
- m_iActiveCamera = CAMERA_TYPE_ENV;
- }
- else if (pszValue[0] == 'O'){
- m_iActiveCamera = CAMERA_TYPE_OPT;
- }
- }
- }
- else if(_stricmp(pszKey,"SessionID")==0)
- {
- CSimpleStringA strSessionID;
- GetFunction()->GetSysVar("SessionID",strSessionID);
- //如果sessionid改变且不为空,切换录像文件
- if(_stricmp(strSessionID,"N")!=0){
- if (m_bStarted && (0 == m_iRecordMode)){
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Session Id change to %s, close record and start new video file.",strSessionID.GetData());
- m_pRecorder->ReNameVideoFile(strSessionID);
- }
- }
- }
- }
- void CRecorderEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- if (Test_ShakeHand == eTestType){
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- }
- void CRecorderEntity::StartRecord(const char *videofilename)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("videofilename = %s", videofilename);
- 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;
- tAudioParams.iAudioChannels = 1;
- if (m_pRecorder->StartVideoRecord(fps, 75, m_eRecordType, &tAudioParams, NULL, FALSE, TRUE, (LPCSTR)m_RecordSaveDir, m_RecordSaveDir.GetLength(), videofilename, strlen(videofilename)))
- {
- m_bStarted = TRUE;
- }
- }
- void CRecorderEntity::StopRecord()
- {
- if (m_bStarted) {
- m_pRecorder->StopVideoRecord();
- m_bStarted = false;
- }
- }
- DeviceTypeEnum CRecorderEntity::RvcGetDeviceType()
- {
- DeviceTypeEnum eType = eStand2sType;
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSystemStaticInfo stStaticinfo;
- spFunction->GetSystemStaticInfo(stStaticinfo);
- if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
- eType = eStand1SPlusType;
- }
- else 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 if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
- eType = eCardStore;
- }
- 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;
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CRecorderEntity)
- SP_END_ENTITY_MAP()
|