MemoryClient.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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, 10) == WAIT_TIMEOUT);
  209. }
  210. return 0L;
  211. }
  212. // 异步线程;
  213. DWORD CMemoryClient::ThreadAsyAudio(LPVOID lpVoid)
  214. {
  215. DWORD dwDuration = *(DWORD*)lpVoid;
  216. return 0L;
  217. }
  218. BOOL CMemoryClient::Startstreaming()
  219. {
  220. return TRUE;
  221. }
  222. BOOL CMemoryClient::Stoptstreaming()
  223. {
  224. return TRUE;
  225. }
  226. BOOL CMemoryClient::CaptureImage(const CMD_CaputerImage& cmd, BOOL bSingle)
  227. {
  228. BOOL bIsJPG = FALSE;
  229. TString strFilePath = cmd.szSaveDir;
  230. if ( cmd.dwImageType == 2 )
  231. bIsJPG = TRUE;
  232. if ( !bSingle )
  233. {// 不是单张;
  234. m_bCapture = TRUE;
  235. if (!PathFileExists(cmd.szSaveDir))
  236. {// 目录不存在,创建;
  237. Global::MKDIR(cmd.szSaveDir);
  238. if ( !PathFileExists(cmd.szSaveDir) )
  239. {
  240. // 创建目录失败,返回结果;
  241. m_bCapture = FALSE;
  242. return FALSE;
  243. }
  244. }
  245. // 内部线程截图;
  246. m_pView->CaptureMultiImageEx(cmd.szSaveDir, cmd.szPrefix, bIsJPG, cmd.nKeepTime);
  247. }
  248. else
  249. {// 单张;
  250. TString strFile = cmd.szSaveDir;
  251. int nIndex = strFile.find_last_of(_T('\\'));
  252. if ( TString::npos != nIndex )
  253. {
  254. Global::MKDIR(strFile.substr(0, nIndex).c_str());
  255. if (!PathFileExists(strFile.substr(0, nIndex).c_str()))
  256. return FALSE;
  257. }
  258. else
  259. {
  260. return FALSE;
  261. }
  262. if (cmd.IsAutoName)
  263. {
  264. nIndex = strFile.find_last_of(_T('.'));
  265. if (nIndex != TString::npos)
  266. {
  267. nIndex = strFile.find_last_of(_T('\\'));
  268. if (nIndex != TString::npos)
  269. strFile = strFile.substr(0, nIndex + 1);
  270. else
  271. strFile.append(_T("\\"));
  272. }
  273. else
  274. {
  275. if (strFile.at(strFile.length() - 1) != _T('\\'))
  276. strFile.append(_T("\\"));
  277. }
  278. std::string strImg = m_pView->CaptureSingleImageAutoNameEx(strFile.c_str(), bIsJPG);
  279. if ( !PathFileExists(strImg.c_str()) )
  280. {
  281. Global::WriteTextLog(_T("自动命名截图失败=%s"), strImg.c_str());
  282. return FALSE;
  283. }
  284. }
  285. else
  286. {
  287. TCHAR szFile[MAX_PATH] = { 0 };
  288. _stprintf_s(szFile, _T("%s"), cmd.szSaveDir);
  289. m_pView->CaptureSingleImageEx(szFile, bIsJPG);
  290. if (!PathFileExists(szFile))
  291. {
  292. Global::WriteTextLog(_T("截图失败=%s"), szFile);
  293. return FALSE;
  294. }
  295. }
  296. }
  297. return TRUE;
  298. }
  299. BOOL CMemoryClient::SynCaptureAudio(const CMD_CaputerAudio& cmd)
  300. {
  301. return TRUE;
  302. }
  303. BOOL CMemoryClient::AsyCaptureAudio(const CMD_CaputerAudio& cmd)
  304. {
  305. m_pView->StartRecord(cmd.dwDuration, cmd.szSaveDir);
  306. return TRUE;
  307. }
  308. BOOL CMemoryClient::StopCaptureAudio()
  309. {
  310. m_pView->StopRecord();
  311. return TRUE;
  312. }