MemoryClient.cpp 7.7 KB

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