#include "mod_salesaudiotrans.h" #define LOG_EVT_UI_STARTRECORD 0x30B00001 //开始录像 #define LOG_EVT_UI_STOPRECORD 0x30B00002 //停止录像 #define LOG_EVT_SALESRECORD_FINISHED 0x31500003 //销售双录结束 #define LOG_EVT_UI_RECORDFAILED 0x31500001 //双录失败 #define LOG_EVT_SALESRECORD_ENTITY_EXCEPTION 0x31500002 //销售双录实体异常 using namespace SalesAudioTrans; void SalesAudioTransServiceSession::Handle_StartAudioTrans(SpReqAnsContext::Pointer ctx) { LOG_FUNCTION(); ErrorCodeEnum rc = Error_Succeed; rc = m_pEntity->StartAudioTrans(ctx->Req.VideoName, ctx->Req.Context, ctx->Req.ContextLen); ctx->Answer(rc); } ErrorCodeEnum CSalesAudioTransEntity::Load_RestFul_Functions() { ErrorCodeEnum err = Error_NotImpl; CSimpleStringA szDllName = "libaudiotransmission.dll"; if (NULL == m_fcreateobj) { m_hInst = LoadLibraryA(szDllName); if (m_hInst) { m_fcreateobj = (cpprestful_create)GetProcAddress(m_hInst, "CreateIAudioTransObj"); if (NULL == m_fcreateobj) { return err; } m_fdestoryobj = (cpprestful_destory)GetProcAddress(m_hInst, "DestoryIAudioTransObj"); if (NULL == m_fdestoryobj) { return err; } err = Error_Succeed; } else{ DWORD tmpError = GetLastError(); Dbg("LoadLibraryA [%s] failed with error %u.", szDllName.GetData(), tmpError); } } return err; } void CSalesAudioTransEntity::OnPreStart(CAutoArray strArgs,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnStart(Error_Succeed); pTransactionContext->SendAnswer(Error) ; } ErrorCodeEnum CSalesAudioTransEntity::__OnStart( ErrorCodeEnum preOperationError ) { LOG_FUNCTION(); ErrorCodeEnum Error = Error_Succeed; //MessageBoxA(0,0,0,0); if (Error_Succeed != Load_RestFul_Functions()){ Dbg("load restful functions failed!"); return Error; } else{ Dbg("load restful functions success!"); } Error = LoadConfig(); int i = 0; m_arrListener.Init(5); GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_SALESRECORD_FINISHED, NULL, false); GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STARTRECORD, NULL, false); GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STOPRECORD, NULL, false); GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_RECORDFAILED, NULL, false); GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_SALESRECORD_ENTITY_EXCEPTION, NULL, false); return Error; } ErrorCodeEnum CSalesAudioTransEntity::LoadConfig() { CSmartPointer spFunction = GetFunction(); CSmartPointer spConfig; ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig); if (Error == Error_Succeed) { CSimpleStringA str_main_server; Error = spConfig->ReadConfigValue("SalesAudioTrans", "ASRServer", str_main_server); if (Error_Succeed == Error){ if (str_main_server.GetLength() > MIN_URL_LEN){ m_main_server = str_main_server; } } Dbg("audio recognize main server[%s]", m_main_server.GetData()); if (Error_Succeed != Error){ CSimpleStringA str_backup_server; Error = spConfig->ReadConfigValue("SalesAudioTrans", "ASRServer_Backup", str_backup_server); if (Error_Succeed == Error){ if (str_backup_server.GetLength() > MIN_URL_LEN){ m_backup_server = str_backup_server; } } } Dbg("audio recognize backup server[%s]", m_backup_server.GetData()); int iTimeOut = DEFAULT_CONNECT_TIMEOUT; int iSize = DEFAULT_MAX_AUDIO_SEND_SIZE; Error = spConfig->ReadConfigValueInt("SalesAudioTrans", "ConnectTimeOut", iTimeOut); if (iTimeOut > 0 && iTimeOut < 5*DEFAULT_CONNECT_TIMEOUT){ m_iconntimeout = iTimeOut; } Error = spConfig->ReadConfigValueInt("SalesAudioTrans", "MaxAudioSize", iSize); m_imaxaudiosendsize = SetMaxAudioSendSize(iSize); Dbg("connect timeout is %d, max audio transmission size is %d.",m_iconntimeout, m_imaxaudiosendsize); } return Error; } void CSalesAudioTransEntity::OnStarted() { asr_server_info_t tServerInfo; tServerInfo.fCreateObj = m_fcreateobj; tServerInfo.fDestoryObj = m_fdestoryobj; tServerInfo.iMaxAudioSize = m_imaxaudiosendsize; tServerInfo.iTimeOut = m_iconntimeout; sprintf_s(tServerInfo.strMainServerURL, MAX_PATH, "%s", m_main_server.GetData()); sprintf_s(tServerInfo.strBackupServerURL, MAX_PATH, "%s", m_backup_server.GetData()); m_pAudioTrans = new Caudiotransmission(&tServerInfo); if (NULL != m_pAudioTrans){ Dbg("create Caudiotransmission object success, and m_pAudioTrans address is %0x.", m_pAudioTrans); } } ErrorCodeEnum CSalesAudioTransEntity::StartAudioTrans(CSimpleStringW strVideoName, CBlob cBuffer, size_t uLen) { LOG_FUNCTION(); ErrorCodeEnum Error = Error_Succeed; if (NULL != m_pAudioTrans){ char* result[16] = {0}; auto arr1 = strVideoName.Split('@'); auto arr2 = CAutoArray(arr1.GetCount()); for (int i = 0; i < arr1.GetCount() && i <= 4; ++i) { arr2[i] = CSimpleStringW2A(arr1[i]); result[i] = const_cast(arr2[i].GetData()); } char strName[MAX_PATH] = {0}; sprintf_s(strName, MAX_PATH, "%s", result[4]);//录像名:录像类型标志_录像流水号(如S_C13213EF) Dbg("record number is %s.", strName); if (0 == m_pAudioTrans->StartAudioTransmission(strName, cBuffer.m_pData, uLen)){ m_bStartTrans = true; } } else{ Error = Error_NotImpl; } return Error; } ErrorCodeEnum CSalesAudioTransEntity::StopAudioTrans() { LOG_FUNCTION(); ErrorCodeEnum Error = Error_NotImpl; if (m_bStartTrans){ if (NULL != m_pAudioTrans){ if (0 == m_pAudioTrans->StopAudioTransmission()){ Error = Error_Succeed; m_bStartTrans = false; Dbg("stop audio transmission success!"); } else{ Dbg("stop audio transmission failed!"); } } else{ Dbg("audio trans object is invalid!"); } } else{ Dbg("audio transmission has stopped."); } return Error; } int CSalesAudioTransEntity::SetMaxAudioSendSize(int iSize) { int iRet = DEFAULT_MAX_AUDIO_SEND_SIZE; if (iSize >= SINGLE_AUDIO_FRAME_SIZE && iSize < DEFAULT_MAX_AUDIO_SEND_SIZE) { iRet = (iSize/SINGLE_AUDIO_FRAME_SIZE)*SINGLE_AUDIO_FRAME_SIZE; } return iRet; } void CSalesAudioTransEntity::OnLog(const CAutoArray &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage) { LOG_TRACE("user_code = %08x", dwUserCode); switch (dwUserCode) { //case LOG_EVT_UI_STOPRECORD: case LOG_EVT_SALESRECORD_FINISHED: case LOG_EVT_SALESRECORD_ENTITY_EXCEPTION: case LOG_EVT_UI_RECORDFAILED: StopAudioTrans(); break; default: break; } } void CSalesAudioTransEntity::OnTimeout(DWORD dwTimerID) { } void CSalesAudioTransEntity::OnSysVarEvent(const char *pszKey,const char *pszValue,const char *pszOldValue,const char *pszEntityName) { } void CSalesAudioTransEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer pTransactionContext) { ErrorCodeEnum Error = __OnClose(Error_Succeed); pTransactionContext->SendAnswer(Error); } ErrorCodeEnum CSalesAudioTransEntity::__OnClose(ErrorCodeEnum preOperationError) { LOG_FUNCTION(); if (NULL != m_pAudioTrans){ delete m_pAudioTrans; m_pAudioTrans = NULL; } SAFE_FREE_LIBRARY(m_hInst); m_hInst = NULL; CSmartPointer spFunction = GetFunction(); for (int i = 0; i < m_arrListener.GetCount(); ++i) { spFunction->UnsubscribeLog(m_arrListener[i]); } return Error_Succeed; } void CSalesAudioTransEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer pTransactionContext) { pTransactionContext->SendAnswer(Error_Succeed); } CServerSessionBase* CSalesAudioTransEntity::OnNewSession(const char *pszRemoteEntityName, const char * pszParam) { LOG_FUNCTION(); Dbg("%s connected class = %s!", pszRemoteEntityName, pszParam); return new SalesAudioTransServiceSession(this); } SP_BEGIN_ENTITY_MAP() SP_ENTITY(CSalesAudioTransEntity) SP_END_ENTITY_MAP()