MemoryClient.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #include "stdafx.h"
  2. #include "MemoryClient.h"
  3. #include "resource.h"
  4. #include <Shlwapi.h>
  5. #include "Global.h"
  6. #include "MainFrm.h"
  7. #if _USE_DLGVIEW_
  8. CUB530View* CMemoryClient::m_pView = NULL;
  9. #else
  10. CSATHelperView* CMemoryClient::m_pView = NULL;
  11. #endif
  12. HANDLE CMemoryClient::m_hRecordFile = NULL;
  13. HANDLE CMemoryClient::m_hThreadAudio = NULL;
  14. BOOL CMemoryClient::m_bCapture = FALSE;
  15. BOOL CMemoryClient::m_bIsConnect = FALSE;
  16. CMemoryClient::CMemoryClient(void)
  17. {
  18. m_hThread = NULL;
  19. m_hEvent = NULL;
  20. }
  21. CMemoryClient::~CMemoryClient(void)
  22. {
  23. EndOfThread();
  24. }
  25. BOOL CMemoryClient::StartThread()
  26. {
  27. // 创建事件对象;
  28. if ( m_hEvent == NULL )
  29. m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  30. if ( m_hEvent == NULL )
  31. return FALSE;
  32. // 创建线程;
  33. if ( m_hThread == NULL )
  34. {
  35. m_hThread = CreateThread(NULL, 0, ThreadProc, this, 0, NULL);
  36. if ( m_hThread == NULL )
  37. {
  38. CloseHandle(m_hEvent);
  39. m_hEvent = NULL;
  40. return FALSE;
  41. }
  42. }
  43. return TRUE;
  44. }
  45. void CMemoryClient::EndOfThread()
  46. {
  47. if ( m_hEvent )
  48. SetEvent(m_hEvent);
  49. if ( m_hThread )
  50. WaitForSingleObject(m_hThread, INFINITE);
  51. if (m_hThread) CloseHandle(m_hThread);
  52. m_hThread = NULL;
  53. if (m_hEvent) CloseHandle(m_hEvent);
  54. m_hEvent = NULL;
  55. }
  56. DWORD CMemoryClient::ThreadProc(LPVOID lpVoid)
  57. {
  58. CMemoryClient *pThis = (CMemoryClient*)lpVoid;
  59. if ( pThis )
  60. {
  61. CommandHead cmdHead;
  62. do
  63. {
  64. // 读取内存信息;
  65. pThis->Lock(INFINITE);
  66. // 解析命令;
  67. memcpy(&cmdHead, pThis->m_pMemory, sizeof(CommandHead));
  68. // 获取设置命令;
  69. if ( cmdHead.cmdFlag == 0x7F && cmdHead.cmdUser == TRUE )
  70. {
  71. switch( cmdHead.cmdType )
  72. {
  73. // 显示窗口;
  74. case SHOW_APP:
  75. {
  76. CMainFrame* pFrameWnd = (CMainFrame*)m_pView->GetParentFrame();
  77. pFrameWnd->ShowWindow(SW_SHOWNORMAL);
  78. pFrameWnd->m_bNoticeTray = TRUE;
  79. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  80. }
  81. break;
  82. // 隐藏窗口;
  83. case HIDE_APP:
  84. {
  85. CMainFrame* pFrameWnd = (CMainFrame*)m_pView->GetParentFrame();
  86. pFrameWnd->ShowWindow(SW_HIDE);
  87. pFrameWnd->m_bNoticeTray = FALSE;
  88. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  89. }
  90. break;
  91. // 连接设备;
  92. case CONNECT_DEVICE:
  93. {
  94. CMD_Result result;
  95. unsigned short nIndex = 0;
  96. memcpy(&nIndex, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(unsigned short));
  97. result.bResult = m_pView->HwInitialize();
  98. // 返回结果;
  99. result.cmdHead = cmdHead;
  100. result.cmdHead.cmdUser = FALSE;
  101. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  102. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  103. }
  104. break;
  105. // 断开设备;
  106. case DIS_CONNECT_DEVICE:
  107. {
  108. CMD_Result result;
  109. unsigned short nIndex = 0;
  110. memcpy(&nIndex, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(unsigned short));
  111. result.bResult = m_pView->HwUninitialize();
  112. m_pView->Invalidate();
  113. // 返回结果;
  114. result.cmdHead = cmdHead;
  115. result.cmdHead.cmdUser = FALSE;
  116. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  117. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  118. }
  119. break;
  120. // 开始、停止流操作;
  121. case START_STREAMING:
  122. case STOP_STREAMING:
  123. {
  124. CMD_Result result;
  125. BOOL bStartStreaming = TRUE;
  126. memcpy(&bStartStreaming, ((BYTE*)pThis->m_pMemory) + sizeof(CommandHead), sizeof(BOOL));
  127. if (bStartStreaming)
  128. result.bResult = Startstreaming();
  129. else
  130. result.bResult = Stoptstreaming();
  131. // 返回结果;
  132. result.cmdHead = cmdHead;
  133. result.cmdHead.cmdUser = FALSE;
  134. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  135. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  136. }
  137. break;
  138. // 截图;
  139. case CAPTURE_IMAGE_COUNT:
  140. case CAPTURE_IMAGE_TIME:
  141. case CAPTURE_IMAGE_SINGLE:
  142. {
  143. CMD_Result result;
  144. CMD_CaputerImage cmd;
  145. memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerImage));
  146. result.bResult = CaptureImage(cmd, cmd.bSingle = (cmdHead.cmdType == CAPTURE_IMAGE_SINGLE));
  147. // 返回结果;
  148. result.cmdHead = cmdHead;
  149. result.cmdHead.cmdUser = FALSE;
  150. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  151. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  152. }
  153. break;
  154. // 停止截图;
  155. case STOP_CAPTUREIMAGE:
  156. {// 停止截图;
  157. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  158. }
  159. break;
  160. // 同步录频;
  161. case SYN_CAPTURE_AUDIO:
  162. {
  163. CMD_Result result;
  164. CMD_CaputerAudio cmd;
  165. memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio));
  166. result.bResult = SynCaptureAudio(cmd);
  167. // 返回结果;
  168. result.cmdHead = cmdHead;
  169. result.cmdHead.cmdUser = FALSE;
  170. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  171. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  172. }
  173. break;
  174. // 异步录频;
  175. case ASY_CAPTURE_AUDIO:
  176. {
  177. CMD_Result result;
  178. CMD_CaputerAudio cmd;
  179. memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio));
  180. result.bResult = AsyCaptureAudio(cmd);
  181. // 返回结果;
  182. result.cmdHead = cmdHead;
  183. result.cmdHead.cmdUser = FALSE;
  184. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  185. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  186. }
  187. break;
  188. // 停止录屏;
  189. case STOP_CAPTUREAUDIO:
  190. if (m_hRecordFile != NULL)
  191. {
  192. CMD_Result result;
  193. CMD_CaputerAudio cmd;
  194. memcpy(&cmd, pThis->m_pMemory, sizeof(CMD_CaputerAudio));
  195. result.bResult = StopCaptureAudio();
  196. // 返回结果;
  197. result.cmdHead = cmdHead;
  198. result.cmdHead.cmdUser = FALSE;
  199. memset(pThis->m_pMemory, 0, sizeof(MEMERY_SIZE));
  200. memcpy(pThis->m_pMemory, &result, sizeof(CMD_Result));
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. pThis->Unlock();
  208. } while ( WaitForSingleObject(pThis->m_hEvent, 20) == WAIT_TIMEOUT);
  209. #ifdef _DEBUG
  210. OutputDebugString(_T("退出内存通信\n"));
  211. #endif
  212. }
  213. return 0L;
  214. }
  215. // 异步线程;
  216. DWORD CMemoryClient::ThreadAsyAudio(LPVOID lpVoid)
  217. {
  218. DWORD dwDuration = *(DWORD*)lpVoid;
  219. return 0L;
  220. }
  221. BOOL CMemoryClient::Startstreaming()
  222. {
  223. return TRUE;
  224. }
  225. BOOL CMemoryClient::Stoptstreaming()
  226. {
  227. return TRUE;
  228. }
  229. BOOL CMemoryClient::CaptureImage(const CMD_CaputerImage& cmd, BOOL bSingle)
  230. {
  231. BOOL bIsJPG = FALSE;
  232. TString strFilePath = cmd.szSaveDir;
  233. if ( cmd.dwImageType == 2 )
  234. bIsJPG = TRUE;
  235. if ( !bSingle )
  236. {// 不是单张;
  237. m_bCapture = TRUE;
  238. if (!PathFileExists(cmd.szSaveDir))
  239. {// 目录不存在,创建;
  240. Global::MKDIR(cmd.szSaveDir);
  241. if ( !PathFileExists(cmd.szSaveDir) )
  242. {
  243. // 创建目录失败,返回结果;
  244. m_bCapture = FALSE;
  245. return FALSE;
  246. }
  247. }
  248. // 内部线程截图;
  249. m_pView->CaptureMultiImageEx(cmd.szSaveDir, cmd.szPrefix, bIsJPG, cmd.nKeepTime);
  250. }
  251. else
  252. {// 单张;
  253. TString strFile = cmd.szSaveDir;
  254. int nIndex = strFile.find_last_of(_T('\\'));
  255. if ( TString::npos != nIndex )
  256. {
  257. Global::MKDIR(strFile.substr(0, nIndex).c_str());
  258. if (!PathFileExists(strFile.substr(0, nIndex).c_str()))
  259. return FALSE;
  260. }
  261. else
  262. {
  263. return FALSE;
  264. }
  265. if (cmd.IsAutoName)
  266. {
  267. nIndex = strFile.find_last_of(_T('.'));
  268. if (nIndex != TString::npos)
  269. {
  270. nIndex = strFile.find_last_of(_T('\\'));
  271. if (nIndex != TString::npos)
  272. strFile = strFile.substr(0, nIndex + 1);
  273. else
  274. strFile.append(_T("\\"));
  275. }
  276. else
  277. {
  278. if (strFile.at(strFile.length() - 1) != _T('\\'))
  279. strFile.append(_T("\\"));
  280. }
  281. std::string strImg = m_pView->CaptureSingleImageAutoNameEx(strFile.c_str(), bIsJPG);
  282. if ( !PathFileExists(strImg.c_str()) )
  283. {
  284. Global::WriteTextLog(_T("自动命名截图失败=%s"), strImg.c_str());
  285. return FALSE;
  286. }
  287. }
  288. else
  289. {
  290. TCHAR szFile[MAX_PATH] = { 0 };
  291. _stprintf_s(szFile, _T("%s"), cmd.szSaveDir);
  292. m_pView->CaptureSingleImageEx(szFile, bIsJPG);
  293. if (!PathFileExists(szFile))
  294. {
  295. Global::WriteTextLog(_T("截图失败=%s"), szFile);
  296. return FALSE;
  297. }
  298. }
  299. }
  300. return TRUE;
  301. }
  302. BOOL CMemoryClient::SynCaptureAudio(const CMD_CaputerAudio& cmd)
  303. {
  304. return TRUE;
  305. }
  306. BOOL CMemoryClient::AsyCaptureAudio(const CMD_CaputerAudio& cmd)
  307. {
  308. m_pView->StartRecord(cmd.dwDuration, cmd.szSaveDir);
  309. return TRUE;
  310. }
  311. BOOL CMemoryClient::StopCaptureAudio()
  312. {
  313. m_pView->StopRecord();
  314. return TRUE;
  315. }