123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #include "stdafx.h"
- #include "MemoryClient.h"
- #include "resource.h"
- #include <Shlwapi.h>
- #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;
- }
|