123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpIni.h"
- #include "../mod_assistantchannel/AssistantChannel_client_g.h"
- using namespace AssistantChannel;
- #include "../mod_assistantchannel/chan_protocol.h"
- #include "rvc_media_common.h"
- #include "libvideoqueue.h"
- #include "jpeg2k.h"
- #include "libfacecapture.h"
- #include "y2k_time.h"
- #include "sysvar.h"
- #include "Event.h"
- #include "fileutil.h"
- #include "EventCode.h"
- #include "../mod_mediacontroller/Event.h"
- #ifndef MAX_LOG_LEN
- #define MAX_LOG_LEN 512
- #endif
- #ifndef rvc_snprintf
- #ifdef RVC_OS_WIN
- #define rvc_snprintf _snprintf
- #else
- #define rvc_snprintf snprintf
- #endif // RVC_OS_WIN
- #endif // rvc_snprintf
- // 脸部跟踪&拍照 devel: 0x302
- static struct {
- int monitor_id;
- int evt_code;
- char* evt_msg;
- } monitor_id_evtmsg[] = {
- {0, EVENT_MOD_CUSTOMER_CLOSE, "客户接近 1.5m"},
- {1, EVENT_MOD_CUSTOMER_LEAVE, "客户消失或退出到接近距离1.5M之外"},
- {2, EVENT_MOD_CUSTOMER_ENTEROPERATE, "客户进入操作距离0.5m"},
- {3, EVENT_MOD_CUSTOMER_BACKTOCLOSE, "客户退回到接近距离"},
- {4, EVENT_MOD_CUSTOMER_APPEAR, "有人出现"},
- {5, EVENT_MOD_CUSTOMER_CHANGE, "客户已经换人"},
- {6, EVENT_MOD_CUSTOMER_CAPTUREFACE, "捕获脸部事件"},
- {7, EVENT_MOD_CUSTOMER_LOSEFACE, "失去脸部事件"},
- {8, EVENT_MOD_FACE_BREAKDOWN,"the opencv library breakdown"}
- };
- class CFaceTrackingEntity;
- class ChannelClient : public ChannelService_ClientBase
- {
- public:
- ChannelClient(CFaceTrackingEntity *pEntity);
- virtual void OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData);
- };
- class CFaceTrackingEntity : public CEntityBase, public CHostApi , public CVideoMonitorEvent, public ISysVarListener, public ITimerListener,public ILogListener
- {
- public:
- CFaceTrackingEntity() : m_facecapture(NULL), bIsSessionChange(false),strCustomerID(false),strSessionID(false),bIsCustomerChange(false), m_bSingleCamera(false)
- {
- // note: this object initialize at DllMain, so it suggests keep your code simple here.
- // 1) do simple initializing here
- // 2) dont do complex operation, complex operation such as create new process(thread) and so on
- }
- virtual ~CFaceTrackingEntity() {}
- virtual const char *GetEntityName() const { return "FaceTracking"; }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- ErrorCodeEnum Error;
- bool bRet = false;
- m_facecapture = new Clibfacecapture(&bRet, this, this, REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
- if (!bRet)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("load libface capture failed!");
- pTransactionContext->SendAnswer(Error_Resource);
- return;
- }
- Error = GetFunction()->RegistSysVarEvent("SessionID", this);
- if (Error != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "SessionID");
- }
- Error = GetFunction()->RegistSysVarEvent("CustomerID", this);
- if (Error != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", "CustomerID");
- }
- Error = GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE, this);
- if (Error != Error_Succeed)
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("register sysvar %s failed!", SYSVAR_CAMERASTATE);
- }
- CSimpleStringA strValue;
- GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
- if (strValue[0] == 'N')
- {
- m_facecapture->SetCameraState(0);
- }
- else if (strValue[0] == 'E')
- {
- m_facecapture->SetCameraState(1);
- }
- else if (strValue[0] == 'O')
- {
- m_facecapture->SetCameraState(2);
- }
- else if (strValue[0] == 'B')
- {
- m_facecapture->SetCameraState(3);
- }
- //is Pad Version
- CSystemStaticInfo stStaticinfo;
- spFunction->GetSystemStaticInfo(stStaticinfo);
- if (stricmp(stStaticinfo.strMachineType,"RVC.Stand1SPlus")==0){
- m_bSingleCamera = true;
- }
- spFunction->SubscribeLog(m_UUid1, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_HEADLIGHT_GREEN_OFF,NULL,false);
- spFunction->SubscribeLog(m_UUid2, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED,NULL,false);
- spFunction->SubscribeLog(m_UUid3, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED,NULL,false);
- spFunction->SubscribeLog(m_UUid4, this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_RESET_ACTIVECAMERA, NULL, false);
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnStarted()
- {
- LogEvent(Severity_Middle, LOG_EVT_MOD_FACETRACKING_STARTED_SUCCESS, "facetracking entity started successfully.");
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- delete m_facecapture;
- m_facecapture = NULL;
- spFunction->UnsubscribeLog(m_UUid1);
- spFunction->UnsubscribeLog(m_UUid2);
- spFunction->UnsubscribeLog(m_UUid3);
- spFunction->UnsubscribeLog(m_UUid4);
- spFunction->UnregistSysVarEvent("SessionID");
- spFunction->UnregistSysVarEvent("CustomerID");
- spFunction->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- virtual void OnPaused()
- {
- Capture(0);
- }
- int TransCameraStateToInt(char cData)
- {
- int iRet = 0;
- if ('E' == cData){
- iRet = 1;
- }
- else if('O' == cData){
- iRet = 2;
- }
- else if('B' == cData){
- iRet = 3;
- }
- return iRet;
- }
- 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, const linkContext &pLinkInfo)
- {
- switch (dwUserCode)
- {
- case LOG_EVT_HEADLIGHT_GREEN_OFF:
- m_facecapture->SetLightChange();
- break;
- case LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED:
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED event");
- CSimpleStringA strValue;
- GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
- #if defined(RVC_OS_WIN)
- int iState = TransCameraStateToInt(strValue[0]);
- if (3 != iState) {
- m_facecapture->SetCameraState(iState);
- m_facecapture->StartFaceCapture();
- }
- else {
- m_facecapture->SetCameraState(3);
- }
- #else
- if (strValue[0] == 'N') {
- m_facecapture->SetCameraState(0);
- m_facecapture->StartFaceCapture();
- }
- else if (strValue[0] == 'E')
- {
- m_facecapture->SetCameraState(1);
- m_facecapture->StartFaceCapture();
- }
- else if (strValue[0] == 'O')
- {
- m_facecapture->SetCameraState(2);
- m_facecapture->StartFaceCapture();
- }
- else if (strValue[0] == 'B')
- {
- m_facecapture->SetCameraState(3);
- }
- #endif //RVC_OS_WIN
- if (!m_bSingleCamera) {
- GetFunction()->SetTimer(1, this, 1000);
- }
- }
- break;
- case LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED:
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED event");
- m_facecapture->StopFaceCapture();
- CSimpleStringA strValue;
- GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
- int iState = TransCameraStateToInt(strValue[0]);
- m_facecapture->SetCameraState(iState);
- if (!m_bSingleCamera) {
- GetFunction()->KillTimer(1);
- }
- GetFunction()->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
- }
- break;
- case LOG_EVT_MEDIACONTROLLER_RESET_ACTIVECAMERA:
- {
- CSimpleStringA strValue;
- GetFunction()->GetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, strValue); // E or O
- if ('O' == strValue[0]) {
- GetFunction()->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
- }
- }
- break;
- default:
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unknown event.");
- break;
- }
- }
- void Capture(int id)
- {
- #if 0
- CImageFrame frm;
- frm.data = new unsigned char[REC_COMMON_VIDEO_SNAPSHOT_WIDTH*REC_COMMON_VIDEO_SNAPSHOT_HEIGHT*4];
- frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
- frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
- frm.framesize = frm.width * frm.height * 3;
- if (m_facecapture->SnapShot(frm)) {
- int rc;
- jpeg2k_raw_image raw_image;
- jpeg2k_coded_image codec_image = {0};
- raw_image.data = frm.data;
- raw_image.width = frm.width;
- raw_image.height = frm.height;
- raw_image.len = raw_image.width * raw_image.height * 3;
- rc = jpeg2k_encode(&raw_image, &codec_image, 10);
- if (rc == 0) {
- ChannelService_Send_Info Info;
- Info.compress = false;
- Info.encrypt = false;
- Info.type = ACM_TYPE_PHT;
- Info.id = id;
- Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
- Info.data.m_pData = codec_image.data;
- Info.data.m_iLength = codec_image.len;
- (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->Send(Info);
- }
- jpeg2k_encode_free(&codec_image);
- } else {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get video frm failed!");
- }
- delete frm.data;
- #endif
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // ITimerListener implementation
-
- virtual void OnTimeout(DWORD dwTimerID)
- {
- uint64_t uid;
- CCustomerStatus status;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("before GetMainCustomerStatus");
- if (m_facecapture->GetMainCustomerStatus(uid, status))
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSimpleStringA strValue;
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnTimeout, check value, eCamera = %d", status.stCustomerPos.eCamera);
- spFunction->GetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, strValue); // E or O
- if (status.stCustomerPos.eCamera == EnvironCamera)
- {
- if (strValue[0] == 'O')
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ActiveTrackingCamera change from Opt->Env!");
- spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_ENV); // from Operation -> Environment
- }
- }
- else
- {
- if (strValue[0] == 'E')
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("ActiveTrackingCamera change from Env->Opt!");
- spFunction->SetSysVar(SYSVAR_ACTIVETRACKINGCAMERA, ACTIVETRACKINGCAMERA_OPT); // from Environment -> Operation
- }
- }
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("GetMainCustomerStatus failed!");
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////
- // CHostApi implementation
- virtual uint64_t GenerateUUID()
- {
- m_lastUUID = CUUID::Create(m_lastUUID);
- return m_lastUUID;
- }
- virtual bool IsCustomerChange()
- {
- bool bChange = bIsSessionChange||bIsCustomerChange;
- return bChange;
- }
- virtual void GetFaceImgName(char* FaceName, uint32_t uLen)
- {
- CSimpleStringA strPath;
- ErrorCodeEnum Error = GetFunction()->GetPath("UploadPhoto", strPath);
- if (Error == Error_Succeed)
- {
- if (((const char*)strCustomerID == NULL)||(_stricmp(strCustomerID,"N") == 0))
- {
- rvc_snprintf(FaceName, uLen,"%s%s%s_0", strPath.GetData(), SPLIT_SLASH_STR, strSessionID.GetData());
- }
- else
- {
- rvc_snprintf(FaceName, uLen, "%s%s%s_%s", strPath.GetData(), SPLIT_SLASH_STR, strSessionID.GetData(), strCustomerID.GetData());
- }
- bIsSessionChange = false;
- bIsCustomerChange = false;
- }
- else
- {
- DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("getpath uploadphoto failed!");
- }
- }
- virtual bool LoadConfig(CFaceCaptureConfig &config)
- {
- CSmartPointer<IEntityFunction> spFunction = GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- ErrorCodeEnum Error(Error_Succeed);
- /*
- PrimCamera=1
- UpCameraEdgeLimit=3
- DownCameraEdgeLimit=2
- ServersType=0
- ContourMinAera=14400
- FaceDataDirPath=facedata
- OperateFaceSize=4.2
- CloseFaceSize=6.6
- FarFaceSize=8.2
- SearchFaceSize=8.2
- DetectFaceSize=6.6
- FaceSizeOffset=18
- SleepLong=800
- SleepMiddle=600
- SleepShort=500
- ThresholdNum=10
- */
- CSimpleStringA strPath;
- config.nServersType = 0;
- GetFunction()->GetPath("cfg", strPath);
- strcpy(config.strFaceDataDirPath, strPath);
- strcat(config.strFaceDataDirPath, SPLIT_SLASH_STR "facedata");
- config.nPrimCamera = 1;
- config.nContourMinAera = 14400;
- config.nUpCameraEdgeLimit = 3;
- config.nDownCameraEdgeLimit = 2;
- config.fOperateFaceSize = 4.2f;
- config.fCloseFaceSize = 6.6f;
- config.fFarFaceSize = 8.2f;
- config.fSearchFaceSize = 8.2f;
- config.fDetectFaceSize = 6.6f;
- config.nFaceSizeOffset = 18;
- config.nSleepLong = 800;
- config.nSleepMiddle = 600;
- config.nSleepShort = 500;
- config.nThresholdNum = 10;
- Error = Error_Succeed;
- return Error == Error_Succeed;
- }
- virtual void Debug(facecap_loglevel elevel, const char *fmt, ...)
- {
- 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_E)elevel, LOG_TYPE_SYSTEM)("%s", buf);
- free(buf);
- }
- else{
- char strlog[MAX_LOG_LEN] = {0};
- vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
- DbgWithLink((LOG_LEVEL_E)elevel, LOG_TYPE_SYSTEM)("%s", strlog);
- }
- va_end(arg);
- }
- ////////////////////////////////////////////////////////////////////////////////////
- // CVideoMonitorEvent
- void GenerateMonitorEvent(video_monitor_event_type eType)
- {
- LogEvent(Severity_Middle, monitor_id_evtmsg[eType].evt_code, monitor_id_evtmsg[eType].evt_msg);
- }
- private:
- virtual void OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (_stricmp(pszKey,"SessionID") == 0)
- {
- GetFunction()->GetSysVar("SessionID",strSessionID);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("sessionID change to %s",strSessionID);
- bIsSessionChange = true;
- }
- if (_stricmp(pszKey,"CustomerID") == 0)
- {
- GetFunction()->GetSysVar("CustomerID",strCustomerID);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("customerID change to %s",strCustomerID);
- bIsCustomerChange = true;
- }
- if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
- {
- CSimpleStringA str;
- GetFunction()->GetSysVar(SYSVAR_CAMERASTATE,str);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("customerID change to %s",strCustomerID);
- if (str == 'N')
- {
- m_facecapture->SetCameraState(0);
- }
- else if (str == 'E')
- {
- m_facecapture->SetCameraState(1);
- }
- else if (str == 'O')
- {
- m_facecapture->SetCameraState(2);
- }
- else if (str == 'B')
- {
- m_facecapture->SetCameraState(3);
- //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("set Camera State to 3");
- }
- }
- }
- private:
- CUUID m_UUid1,m_UUid2,m_UUid3,m_UUid4;
- bool bIsSessionChange;
- bool bIsCustomerChange;
- CSimpleStringA strCustomerID;
- CSimpleStringA strSessionID;
- CUUID m_lastUUID;
- ChannelClient *m_pChannelClient;
- Clibfacecapture *m_facecapture;
- bool m_bSingleCamera;
- };
- void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
- {
- if (Error == Error_Succeed)
- {
- CFaceTrackingEntity *pEntity = static_cast<CFaceTrackingEntity*>(m_pEntityBase);
- int cat = ACM_PHT_CAT(Msg.sub_type);
- if (cat == ACM_PHT_REQ)
- {
- //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);
- pEntity->Capture(Msg.id);
- //SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
- } else
- {
- _ASSERT(0);
- }
- }
- }
- ChannelClient::ChannelClient( CFaceTrackingEntity *pEntity ) : ChannelService_ClientBase(pEntity)
- {
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CFaceTrackingEntity)
- SP_END_ENTITY_MAP()
|