#include "stdafx.h" #include "MemoryClient.h" #include "resource.h" #include #include "Global.h" #include "MainFrm.h" CVideoCaptureView* CMemoryClient::m_pView = NULL; HANDLE CMemoryClient::m_hRecordFile = NULL; HANDLE CMemoryClient::m_hThreadAudio = NULL; BOOL CMemoryClient::m_bCapture = FALSE; BOOL CMemoryClient::m_bIsConnect = FALSE; CMemoryClient::CMemoryClient(void) { m_hThread = NULL; m_hEvent = NULL; } CMemoryClient::~CMemoryClient(void) { EndOfThread(); } BOOL CMemoryClient::StartThread() { // 创建事件对象; if ( m_hEvent == NULL ) m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if ( m_hEvent == NULL ) return FALSE; // 创建线程; if ( m_hThread == NULL ) { m_hThread = CreateThread(NULL, 0, ThreadProc, this, 0, NULL); if ( m_hThread == NULL ) { CloseHandle(m_hEvent); m_hEvent = NULL; return FALSE; } } return TRUE; } void CMemoryClient::EndOfThread() { if ( m_hEvent ) SetEvent(m_hEvent); if ( m_hThread ) WaitForSingleObject(m_hThread, INFINITE); if (m_hThread) CloseHandle(m_hThread); m_hThread = NULL; if (m_hEvent) CloseHandle(m_hEvent); m_hEvent = NULL; } DWORD CMemoryClient::ThreadProc(LPVOID lpVoid) { CMemoryClient *pThis = (CMemoryClient*)lpVoid; if ( pThis ) { CommandHead cmdHead; do { // 读取内存信息; pThis->Lock(INFINITE); // 解析命令; memcpy(&cmdHead, pThis->m_pMemory, sizeof(CommandHead)); // 获取设置命令; if ( cmdHead.cmdFlag == 0x7F && cmdHead.cmdUser == TRUE ) { switch( cmdHead.cmdType ) { // 显示窗口; case SHOW_APP: { CMainFrame* pFrameWnd = (CMainFrame*)m_pView->GetParentFrame(); pFrameWnd->ShowWindow(SW_SHOWNORMAL); pFrameWnd->m_bNoticeTray = TRUE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); } break; // 隐藏窗口; case HIDE_APP: { CMainFrame* pFrameWnd = (CMainFrame*)m_pView->GetParentFrame(); pFrameWnd->ShowWindow(SW_HIDE); pFrameWnd->m_bNoticeTray = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); } break; // 连接设备; case CONNECT_DEVICE: { CMD_Result result; unsigned short nIndex = 0; memcpy(&nIndex, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(unsigned short)); result.bResult = m_pView->HwInitialize(); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 断开设备; case DIS_CONNECT_DEVICE: { CMD_Result result; unsigned short nIndex = 0; memcpy(&nIndex, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(unsigned short)); result.bResult = m_pView->HwUninitialize(); m_pView->Invalidate(); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 开始、停止流操作; case START_STREAMING: case STOP_STREAMING: { CMD_Result result; BOOL bStartStreaming = TRUE; memcpy(&bStartStreaming, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(BOOL)); if (bStartStreaming) result.bResult = Startstreaming(); else result.bResult = Stoptstreaming(); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 截图; case CAPTURE_IMAGE_COUNT: case CAPTURE_IMAGE_TIME: case CAPTURE_IMAGE_SINGLE: { CMD_Result result; CMD_CaputerImage cmd; memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerImage)); result.bResult = CaptureImage(cmd, cmd.bSingle = (cmdHead.cmdType == CAPTURE_IMAGE_SINGLE)); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 停止截图; case STOP_CAPTUREIMAGE: {// 停止截图; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); } break; // 同步录频; case SYN_CAPTURE_AUDIO: { CMD_Result result; CMD_CaputerAudio cmd; memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio)); result.bResult = SynCaptureAudio(cmd); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 异步录频; case ASY_CAPTURE_AUDIO: { CMD_Result result; CMD_CaputerAudio cmd; memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio)); result.bResult = AsyCaptureAudio(cmd); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; // 停止录屏; case STOP_CAPTUREAUDIO: if (m_hRecordFile != NULL) { CMD_Result result; CMD_CaputerAudio cmd; memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio)); result.bResult = StopCaptureAudio(); // 返回结果; result.cmdHead = cmdHead; result.cmdHead.cmdUser = FALSE; memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE)); memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result)); } break; default: break; } } pThis->Unlock(); } while ( WaitForSingleObject(pThis->m_hEvent, 10) == WAIT_TIMEOUT); } return 0L; } // 异步线程; DWORD CMemoryClient::ThreadAsyAudio(LPVOID lpVoid) { DWORD dwDuration = *(DWORD*)lpVoid; return 0L; } BOOL CMemoryClient::Startstreaming() { return TRUE; } BOOL CMemoryClient::Stoptstreaming() { return TRUE; } BOOL CMemoryClient::CaptureImage(const CMD_CaputerImage& cmd, BOOL bSingle) { BOOL bIsJPG = FALSE; TString strFilePath = cmd.szSaveDir; if ( cmd.dwImageType == 2 ) bIsJPG = TRUE; if ( !bSingle ) {// 不是单张; m_bCapture = TRUE; if (!PathFileExists(cmd.szSaveDir)) {// 目录不存在,创建; Global::MKDIR(cmd.szSaveDir); if ( !PathFileExists(cmd.szSaveDir) ) { // 创建目录失败,返回结果; m_bCapture = FALSE; return FALSE; } } m_pView->CaptureMultiImageEx(cmd.szSaveDir, cmd.szPrefix, bIsJPG, cmd.nKeepTime); } else {// 单张; TString strFile = cmd.szSaveDir; int nIndex = strFile.find_last_of(_T('\\')); if ( TString::npos != nIndex ) { Global::MKDIR(strFile.substr(0, nIndex).c_str()); if (!PathFileExists(strFile.substr(0, nIndex).c_str())) return FALSE; } else { return FALSE; } if (cmd.IsAutoName) { nIndex = strFile.find_last_of(_T('.')); if (nIndex != TString::npos) { nIndex = strFile.find_last_of(_T('\\')); if (nIndex != TString::npos) strFile = strFile.substr(0, nIndex + 1); else strFile.append(_T("\\")); } else { if (strFile.at(strFile.length() - 1) != _T('\\')) strFile.append(_T("\\")); } m_pView->CaptureSingleImageAutoNameEx(strFile.c_str(), bIsJPG); } else { TCHAR szFile[MAX_PATH] = { 0 }; _stprintf_s(szFile, _T("%s"), cmd.szSaveDir); m_pView->CaptureSingleImageEx(szFile, bIsJPG); } // 等待磁盘完成写入; //Sleep(bIsJPG ? 200 : 200); } return TRUE; } BOOL CMemoryClient::SynCaptureAudio(const CMD_CaputerAudio& cmd) { return TRUE; } BOOL CMemoryClient::AsyCaptureAudio(const CMD_CaputerAudio& cmd) { m_pView->StartRecord(cmd.dwDuration, cmd.szSaveDir); return TRUE; } BOOL CMemoryClient::StopCaptureAudio() { m_pView->StopRecord(); return TRUE; }