HKVisionDlg.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. // HKVisionDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "HKVision.h"
  5. #include "HKVisionDlg.h"
  6. #include "SelectDSP.h"
  7. #include "settingsDlg.h"
  8. #include "hikserver.h"
  9. #include "hikvisionsdk.h"
  10. #include "Global.h"
  11. #include "mdlProject.h"
  12. #include <fcntl.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <io.h>
  16. #include <stdio.h>
  17. #include <mmsystem.h>
  18. #include ".\hkvisiondlg.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. #define WM_MYCOMMAND (WM_USER+1000) //
  25. #define DIALTYPE 0 //dialing
  26. #define NORMATYPE 1 //Wan or Lan
  27. #define UPDATE_COUNTERS __T("Update Frame Counters")
  28. #define DATA_READY __T("Data ready")
  29. BOOL bCapture = FALSE;
  30. BOOL g_bOverlayMode=FALSE;//如果函数SetPreviewOverlayMode的返回值为0,则bOverlayMode赋值为TRUE,表示启用Overlay方式预览
  31. unsigned char StreamBuf[1000000];
  32. int __cdecl StreamReadCallback(ULONG ChannelNum, void *Context);
  33. typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
  34. int __cdecl StreamDirectReadCallback(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
  35. unsigned char FileHeader[64][200];
  36. int FileHeaderLen = 0;
  37. int gFileHandleQcif[MAX_CHANNELS];
  38. int icount = 0;;
  39. int KeyFrameInterva[10] = {25,67,76,100,100,100,100,121,211,211};
  40. int FrameRate[10] = {25,25,25,25,20,10,5,4,3,2};
  41. int IQ[10] = {10,10,12,15,18,20,20,22,24,24};
  42. int PQ[10] = {10,10,12,15,18,20,20,22,24,24};
  43. int BQ[10] = {10,15,15,20,21,23,23,25,27,27};
  44. int nowstate[16];
  45. int servertype;
  46. UINT usframe = 0;
  47. BOOL chantype = FALSE;
  48. DWORD dcurrentwin=0;
  49. int g_nTotalChannel;
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CAboutDlg dialog used for App About
  52. int SplitRects(int width, int height, int cols, int rows, RECT *rectList);
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CHKVisionDlg dialog
  55. bool g_DiskVelumnIsEnough( int iDiskVolume,CString sPath )
  56. {
  57. bool bRet = false;
  58. double dVelume=0.0,dRemainVelume=0.0;
  59. switch( iDiskVolume )
  60. {
  61. case 0: dVelume = 500; break;
  62. case 1: dVelume = 1024*1; break;
  63. case 2: dVelume = 1024*3; break;
  64. case 3: dVelume = 1024*5; break;
  65. }
  66. ULARGE_INTEGER lpuse;
  67. ULARGE_INTEGER lptotal;
  68. ULARGE_INTEGER lpfree;
  69. GetDiskFreeSpaceEx(sPath,&lpuse,&lptotal,&lpfree);
  70. CString s;
  71. s.Format("%4.4fGB\n",lpfree.QuadPart/1024.0/1024.0);
  72. dRemainVelume = lpfree.QuadPart/1024.0/1024.0;
  73. if( dRemainVelume>=dVelume )
  74. bRet = true;
  75. return bRet;
  76. }
  77. int g_VideoMaxTime( int iType )
  78. {
  79. int nRet=0;
  80. switch( iType )
  81. {
  82. case 0: nRet = 3*60; break;
  83. case 1: nRet = 5*60; break;
  84. case 2: nRet = 10*60; break;
  85. case 3: nRet = 20*60; break;
  86. case 4: nRet = 30*60; break;
  87. case 5: nRet = 60*60; break;
  88. }
  89. return nRet;
  90. }
  91. int g_NoMotionTime( int iType )
  92. {
  93. int nRet=0;
  94. switch( iType )
  95. {
  96. case 0: nRet = 5; break;
  97. case 1: nRet = 10; break;
  98. case 2: nRet = 30; break;
  99. case 3: nRet = 60; break;
  100. case 4: nRet = 3*60; break;
  101. }
  102. return nRet;
  103. }
  104. void CALLBACK DrawFun(long nport, HDC hDc,LONG nUser)
  105. {
  106. CHKVisionDlg *dlg = g_pDlgMain->m_pMonitor;
  107. //if (nport == 0 ) //&& DisplayMode != 1)
  108. {
  109. //if (DisplayMode != 1)
  110. //{
  111. // int weight,height;
  112. // weight = rectList[iLastSelect].right - rectList[iLastSelect].left;
  113. // height = rectList[iLastSelect].bottom- rectList[iLastSelect].top;
  114. // HGDIOBJ pOldPen = ::SelectObject(hDc,&penWhite);
  115. // ::MoveToEx(hDc, 1, 1, NULL);
  116. // ::LineTo(hDc, weight -1, 1);
  117. // ::LineTo(hDc, weight -1,height-1);
  118. // ::LineTo(hDc, 1,height -1);
  119. // ::LineTo(hDc, 1,1);
  120. // ::SelectObject(hDc,pOldPen);
  121. //}
  122. CString str;
  123. if( dlg->m_bCapture[nport] )
  124. str.Format("录像中");
  125. SetTextColor(hDc, RGB(255,0,0));
  126. SetBkMode(hDc,TRANSPARENT);
  127. TextOut(hDc,5,5,str,str.GetLength());
  128. //if(dlg->m_bMoving[nport])
  129. {
  130. //dlg->m_VideoWin.DrawVect(nport,(char *)motionData[nport],hDc,pOwner->chstandard[nport]);
  131. //dlg->m_bMoving[nport] = FALSE;
  132. }
  133. }
  134. //else
  135. // dlg->StopDrawFun(nport);
  136. return;
  137. }
  138. CHKVisionDlg::CHKVisionDlg(CWnd* pParent /*=NULL*/)
  139. : CDialog(CHKVisionDlg::IDD, pParent)
  140. {
  141. //{{AFX_DATA_INIT(CHKVisionDlg)
  142. m_brightness = 0;
  143. m_contrast = 0;
  144. m_hue = 0;
  145. m_saturation = 0;
  146. m_iframes = 0;
  147. //m_pbframes = 0;
  148. m_lostframes = 0;
  149. //m_audioframes = 0;
  150. m_ip = _T("");
  151. m_sframe = FALSE;
  152. //}}AFX_DATA_INIT
  153. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  154. //m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  155. memset( m_bHaveSignal,0,64 );
  156. memset( m_bCapture,0,64 );
  157. memset( m_dwVideoStartTime,0,sizeof(DWORD)*64 );
  158. memset( m_dwNoMotionStartTime,0,sizeof(DWORD)*64 );
  159. }
  160. void CHKVisionDlg::DoDataExchange(CDataExchange* pDX)
  161. {
  162. CDialog::DoDataExchange(pDX);
  163. //{{AFX_DATA_MAP(CHKVisionDlg)
  164. DDX_Slider(pDX, IDC_SLIDER_BRIGHT, m_brightness);
  165. DDX_Slider(pDX, IDC_SLIDER_CONTRAST, m_contrast);
  166. DDX_Slider(pDX, IDC_SLIDER_HUE, m_hue);
  167. DDX_Slider(pDX, IDC_SLIDER_SAT, m_saturation);
  168. //DDX_Text(pDX, IDC_IFRAMES, m_iframes);
  169. //DDX_Text(pDX, IDC_PBFRAMES, m_pbframes);
  170. DDX_Text(pDX, IDC_LOST_FRAMES, m_lostframes);
  171. //DDX_Text(pDX, IDC_AUDIOFRAMES, m_audioframes);
  172. DDX_Text(pDX, IDC_IP, m_ip);
  173. DDX_Check(pDX, IDC_CAPTURESFRAME, m_sframe);
  174. //}}AFX_DATA_MAP
  175. }
  176. static const UINT MsgUpdateCounters = ::RegisterWindowMessage(UPDATE_COUNTERS);
  177. static const UINT MsgDataReady = ::RegisterWindowMessage(DATA_READY);
  178. BEGIN_MESSAGE_MAP(CHKVisionDlg, CDialog)
  179. //chn ON_MESSAGE(WM_MYCOMMAND,OnMyCommand)
  180. //{{AFX_MSG_MAP(CHKVisionDlg)
  181. ON_WM_SYSCOMMAND()
  182. ON_WM_PAINT()
  183. ON_WM_QUERYDRAGICON()
  184. ON_BN_CLICKED(IDC_START, OnStart)
  185. ON_WM_CLOSE()
  186. ON_BN_CLICKED(IDC_STOPCAP, OnStop)
  187. ON_BN_CLICKED(IDC_PREVIEW, OnPreview)
  188. ON_WM_CTLCOLOR()
  189. ON_WM_SHOWWINDOW()
  190. ON_WM_MOVE()
  191. ON_WM_HSCROLL()
  192. ON_WM_VSCROLL()
  193. ON_WM_LBUTTONDOWN()
  194. ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
  195. ON_WM_TIMER()
  196. ON_BN_CLICKED(IDC_CAPIMAGE, OnCapimage)
  197. ON_WM_DESTROY()
  198. ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
  199. ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
  200. ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
  201. ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  202. ON_BN_CLICKED(IDC_CAPTURESFRAME, OnCapturesframe)
  203. ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
  204. ON_BN_CLICKED(IDC_SUBSTART, OnSubstart)
  205. //chn ON_REGISTERED_MESSAGE(MsgUpdateCounters, OnUpdateCounters)
  206. //chn ON_REGISTERED_MESSAGE(MsgDataReady, OnDataReady)
  207. ON_BN_CLICKED(IDC_STOPSUBCAP, OnStopsubcap)
  208. //}}AFX_MSG_MAP
  209. END_MESSAGE_MAP()
  210. /////////////////////////////////////////////////////////////////////////////
  211. // CHKVisionDlg message handlers
  212. int LogWrite(const char *Format, ...);
  213. HANDLE ChannelHandle[MAX_CHANNELS];
  214. //int logFile;
  215. int gFileHandle[MAX_CHANNELS];
  216. BOOLEAN dlgInited = FALSE;
  217. HWND gMainWnd;
  218. RECT rectList[MAX_CHANNELS];
  219. BOOL bDdrawMode = FALSE;
  220. ULONG gChannelFrames[MAX_CHANNELS];
  221. ULONG gChannelTotalLength[MAX_CHANNELS];
  222. ULONG gChannelFramesLost[MAX_CHANNELS];
  223. ULONG gChannelOverflow[MAX_CHANNELS];
  224. ULONG gCurrentFileLen[MAX_CHANNELS];
  225. ULONG gCaptureStartedNum = 0;
  226. UINT gTimer;
  227. UINT gCapImages;
  228. ULONG gFileSize = 100; // 100 mb
  229. int gFileNum = 0;
  230. COLORREF gBackgroundColor = RGB(10, 10, 10);
  231. extern int iLastSelect;
  232. ULONG StartTime;
  233. ULONG LastTime;
  234. ///add ver3.4 by h
  235. //int gFileHandleQcif[MAX_CHANNELS]; //子通道同时编码时qcif录象文件句柄
  236. unsigned char FileHeaderQcif[MAX_CHANNELS][200]; //QCIF Header,when
  237. BOOL bEncodeCifAndQcif[MAX_CHANNELS]; //子通道同时编码时通道标志
  238. void CALLBACK StartCap(int port)
  239. {
  240. StartVideoCapture(ChannelHandle[port]);
  241. }
  242. void CALLBACK StopCap(int port)
  243. {
  244. StopVideoCapture(ChannelHandle[port]);
  245. }
  246. int CALLBACK CheckIP(DWORD nChannel,char* nIP)
  247. {
  248. /*
  249. CString ctemp;
  250. ctemp.Format("%s",nIP);
  251. if (ctemp == "192.0.0.215")
  252. return -1;
  253. */
  254. return 0;
  255. }
  256. int CALLBACK checkpassword(char *username,WORD namelen,char *password,WORD passlen)
  257. {
  258. /*
  259. if ((username[0] == '1') && (username[1] == '2') && (username[2] == '3') && (password[0] == 'w'))
  260. return 0;
  261. else
  262. return -1;
  263. */
  264. return 0;
  265. }
  266. void CALLBACK SetIBP(int port,int framerat)
  267. {
  268. SetIBPMode(ChannelHandle[port],211,2,2,framerat);
  269. }
  270. void CALLBACK MakeIFrame(int port)
  271. {
  272. CaptureIFrame(ChannelHandle[port]);
  273. }
  274. BOOL CHKVisionDlg::OnInitDialog()
  275. {
  276. CDialog::OnInitDialog();
  277. int i;
  278. servertype = NORMATYPE;
  279. // Add "About..." menu item to system menu.
  280. m_brush1.CreateSolidBrush(gBackgroundColor);
  281. m_brush2.CreateSolidBrush(RGB(0,0,0));
  282. m_bLog = FALSE;
  283. // Set the icon for this dialog. The framework does this automatically
  284. // when the application's main window is not a dialog
  285. SetIcon(m_hIcon, TRUE); // Set big icon
  286. SetIcon(m_hIcon, FALSE); // Set small icon
  287. // TODO: Add extra initialization here
  288. m_brightness = 50;
  289. m_contrast = 50;
  290. m_hue = 0;
  291. m_saturation = 50;
  292. m_iframes = 0;
  293. //m_pbframes = 0;
  294. //m_audioframes = 0;
  295. m_lostframes = 0;
  296. if(InitDSPs() < 0)
  297. {
  298. //afxDump<<"error:can not init DSPs\n";
  299. AfxMessageBox("can not init DSPs\n");
  300. return FALSE;
  301. }
  302. int iReturn=-1;
  303. //根据函数SetPreviewOverlayMode的返回值来判断当前的显卡是否支持Overlay方式预览,
  304. //0表示支持,其它值表示不支持
  305. iReturn=SetPreviewOverlayMode(TRUE);
  306. TRACE("iReturn=%04x\n",iReturn);
  307. if(iReturn==0)
  308. {
  309. g_bOverlayMode=TRUE;
  310. }
  311. g_nTotalChannel = GetTotalChannels();
  312. int nTemp = 0;
  313. for(i = 0; i < g_nTotalChannel; i++)//判斷是否有信號
  314. {
  315. if( 0 == GetVideoSignal( (HANDLE)i ) )
  316. {
  317. m_bHaveSignal[i] = true;
  318. nTemp = i;
  319. }
  320. }
  321. switch( nTemp )
  322. {
  323. case 1: g_nTotalChannel = 1; break;
  324. case 2:
  325. case 3:
  326. case 4: g_nTotalChannel = 4; break;
  327. case 5:
  328. case 6:
  329. case 7:
  330. case 8: g_nTotalChannel = 8; break;
  331. }
  332. for(i = 0; i < g_nTotalChannel; i++)
  333. {
  334. m_bDspPreset[i] = TRUE;
  335. bEncodeCifAndQcif[i] = FALSE;
  336. gFileHandle[i] = -1;
  337. }
  338. m_sframe = FALSE;
  339. UpdateData(FALSE);
  340. CString title;
  341. CString dsps;
  342. dsps.Format(" %d channels Found", g_nTotalChannel);
  343. GetWindowText(title);
  344. title += dsps;
  345. SetWindowText(title);
  346. dlgInited = TRUE;
  347. m_VideoWin.SubclassDlgItem(IDC_VIDEOWIN, this);
  348. for(i = 0; i < g_nTotalChannel; i++)
  349. {
  350. ChannelHandle[i] = ChannelOpen(i);
  351. if (ChannelHandle[i]<0)
  352. {
  353. AfxMessageBox("channel open error > 0");
  354. }
  355. else if (ChannelHandle[i] ==(HANDLE) 0xffff)
  356. {
  357. AfxMessageBox("channel open error 0xffff");
  358. }
  359. if( m_bHaveSignal[i] )
  360. {
  361. RegisterDrawFun(i, DrawFun ,(DWORD)this);//注册画的回调
  362. VideoStandard_t videostandard;
  363. int bright,constrast,sat,hue;
  364. int bHeight;
  365. GetVideoPara(ChannelHandle[i],&videostandard,&bright,&constrast,&sat,&hue);
  366. AdjustMotionDetectPrecision(ChannelHandle[i], 0x80000000|6, 6, 0);//设置移动侦测灵敏度
  367. if(videostandard==1)
  368. {
  369. bHeight=480;
  370. }
  371. else
  372. {
  373. bHeight=576;
  374. }
  375. RECT areaList[4];
  376. areaList[0].top = 0; areaList[0].left = 0;
  377. areaList[0].right = 704/2; areaList[0].bottom = bHeight/2;
  378. areaList[1].top = 0; areaList[1].left = 704/2;
  379. areaList[1].right = 703; areaList[1].bottom = bHeight/2;
  380. areaList[2].top = bHeight/2; areaList[2].left = 0;
  381. areaList[2].right = 704/2; areaList[2].bottom = bHeight-1;
  382. areaList[3].top = bHeight/2; areaList[3].left = 704/2;
  383. areaList[3].right = 703; areaList[3].bottom = bHeight-1;
  384. SetupMotionDetection(ChannelHandle[i], areaList, 4);
  385. StartMotionDetection(ChannelHandle[i]);
  386. }
  387. gChannelTotalLength[i] = 0;
  388. nowstate[i]=0;
  389. if (servertype == DIALTYPE)
  390. {
  391. SetIBPMode(ChannelHandle[i],211,2,1,8);
  392. SetDefaultQuant(ChannelHandle[i],18,18,23);
  393. SetStreamType(ChannelHandle[i],STREAM_TYPE_VIDEO);
  394. }
  395. else
  396. {
  397. SetIBPMode(ChannelHandle[i],100,2,1,25);
  398. SetDefaultQuant(ChannelHandle[i],15,15,20);
  399. }
  400. }
  401. if (servertype == DIALTYPE)
  402. {
  403. for(i = 0; i < g_nTotalChannel; i++)
  404. SetEncoderPictureFormat(ChannelHandle[i], ENC_QCIF_FORMAT);
  405. }
  406. else
  407. {
  408. for(i = 0; i < g_nTotalChannel; i++)
  409. {
  410. if ( i==0 )
  411. {
  412. //when initiated,set the first channel as 4CIF encode,others as CIF
  413. SetEncoderPictureFormat(ChannelHandle[0], ENC_4CIF_FORMAT);
  414. bEncodeCifAndQcif[0] = FALSE;
  415. }
  416. else
  417. {
  418. SetEncoderPictureFormat(ChannelHandle[i], ENC_CIF_FORMAT);
  419. }
  420. }
  421. }
  422. // int id = IDC_CHECK2;
  423. // for(i = 0; i < MAX_CHANNELS; i++){
  424. // GetDlgItem(id + i)->EnableWindow(FALSE);
  425. // }
  426. RegisterStreamDirectReadCallback(::StreamDirectReadCallback,this);
  427. RegisterMessageNotifyHandle(m_hWnd, MsgDataReady);
  428. MP4_ServerSetMessage(WM_MYCOMMAND,this->m_hWnd);
  429. gCapImages = 0;
  430. SetOverlayColorKey(gBackgroundColor);
  431. gTimer = SetTimer(1, 1000, 0);
  432. SetTimer(2,2000,0);
  433. SetTimer(3,1000,0);//是否到没移动的最长时间
  434. SetTimer(5,5000,0);
  435. for (i=0;i<MAX_CHANNELS;i++)
  436. gCurrentFileLen[i] = 0;
  437. SERVER_VIDEOINFO videoinfo;
  438. for( i=0 ; i < g_nTotalChannel; i++ )
  439. {
  440. if(i == 0)
  441. {
  442. MP4_ServerSetBufNum(i,90);
  443. }
  444. else
  445. {
  446. MP4_ServerSetBufNum(i,80);
  447. }
  448. if (servertype == DIALTYPE)
  449. videoinfo.m_datatype[i] = DIALING;
  450. else
  451. videoinfo.m_datatype[i] = NORMAL;
  452. }
  453. videoinfo.m_datatype[0] = SMALLPIC;
  454. videoinfo.m_channum = g_nTotalChannel;
  455. videoinfo.m_waittime = 2;
  456. MP4_ServerSetStart(StartCap);
  457. MP4_ServerSetStop(StopCap);
  458. MP4_ServerSetIBPMode(SetIBP);
  459. MP4_ServerSetCapIFrame(MakeIFrame);
  460. MP4_ServerSetTTL(64);
  461. MP4_ServerSetNetPort(5050,6050);
  462. MP4_ServerCheckIP(CheckIP);
  463. MP4_ServerCheckPassword(checkpassword);
  464. //set the max connector of channel 0
  465. MP4_ServerMaxUser(0,24);
  466. //如果想不使用缺省方式进行多播,
  467. //可以调用下面的函数设置自己的多播信息
  468. //详细信息请参考SDK文档
  469. // MP4_ServerCastGroup(TRUE,0,"228.0.0.132",9988);
  470. //if (!MP4_ServerStart(&videoinfo))
  471. //{
  472. // MessageBox("error","error",MB_OK);
  473. //}
  474. StartTime = timeGetTime();
  475. return TRUE; // return TRUE unless you set the focus to a control
  476. }
  477. void CHKVisionDlg::SetCtrlPos( CRect rect )
  478. {
  479. CRect rc;
  480. rc.left = 0;
  481. rc.top = 0;
  482. rc.right = rc.left+rect.Width();
  483. rc.bottom = rc.top+rect.Height();
  484. this->MoveWindow( rc );
  485. GetDlgItem(IDC_VIDEOWIN)->MoveWindow( rc );
  486. }
  487. void CHKVisionDlg::OnSysCommand(UINT nID, LPARAM lParam)
  488. {
  489. //if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  490. //{
  491. // CAboutDlg dlgAbout;
  492. // dlgAbout.DoModal();
  493. //}
  494. //else
  495. {
  496. CDialog::OnSysCommand(nID, lParam);
  497. }
  498. }
  499. // If you add a minimize button to your dialog, you will need the code below
  500. // to draw the icon. For MFC applications using the document/view model,
  501. // this is automatically done for you by the framework.
  502. void CHKVisionDlg::OnPaint()
  503. {
  504. if (IsIconic())
  505. {
  506. CPaintDC dc(this); // device context for painting
  507. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  508. // Center icon in client rectangle
  509. int cxIcon = GetSystemMetrics(SM_CXICON);
  510. int cyIcon = GetSystemMetrics(SM_CYICON);
  511. CRect rect;
  512. GetClientRect(&rect);
  513. int x = (rect.Width() - cxIcon + 1) / 2;
  514. int y = (rect.Height() - cyIcon + 1) / 2;
  515. // Draw the icon
  516. dc.DrawIcon(x, y, m_hIcon);
  517. }
  518. else
  519. {
  520. CDialog::OnPaint();
  521. }
  522. }
  523. // The system calls this to obtain the cursor to display while the user drags
  524. // the minimized window.
  525. HCURSOR CHKVisionDlg::OnQueryDragIcon()
  526. {
  527. return (HCURSOR) m_hIcon;
  528. }
  529. int LogWrite(const char *Format, ...)
  530. {
  531. return 1;
  532. CHAR Buffer0[1024];
  533. CHAR Buffer1[512];
  534. DWORD ItemsWritten;
  535. va_list ArgumentList;
  536. // if(logFile == -1)return -1;
  537. SYSTEMTIME sysTime;
  538. GetLocalTime(&sysTime);
  539. sprintf(Buffer0, "%02d%02d:%02d:%02d:%02d:", sysTime.wMonth, sysTime.wDay, sysTime.wHour,
  540. sysTime.wMinute, sysTime.wSecond);
  541. va_start ( ArgumentList, Format );
  542. ItemsWritten = vsprintf ((char *)Buffer1, Format, ArgumentList );
  543. va_end ( ArgumentList );
  544. strcat(Buffer0, Buffer1);
  545. // return _write ( logFile, Buffer0, strlen((const char *)Buffer0 ));
  546. }
  547. void CHKVisionDlg::OnStart()
  548. {
  549. StartCaptureVideo( 2 );
  550. GetDlgItem(IDC_STOPCAP)->EnableWindow(TRUE);
  551. return;
  552. // TODO: Add your control notification handler code here
  553. CSelectDSP selDialog;
  554. selDialog.DSPPreset(m_bDspPreset, g_nTotalChannel);
  555. // if(selDialog.DoModal() == IDOK){
  556. {
  557. // TRACE("SelectDSP:OK button clicked\n");
  558. selDialog.GetDSPsChoiced(m_bDspPreset);
  559. CString str;
  560. str.Format("%d", 0);
  561. GetDlgItem(IDC_LOST_FRAMES)->SetWindowText((LPCTSTR)str);
  562. GetDlgItem(IDC_TOTALFRAMES)->SetWindowText((LPCTSTR)str);
  563. GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
  564. // GetDlgItem(IDC_FPS)->SetWindowText((LPCTSTR)str);
  565. LogWrite("start capture\n");
  566. for(int i = 0; i < g_nTotalChannel; i++)
  567. {
  568. m_bDspPreset[i]=TRUE;
  569. if(m_bDspPreset[i])
  570. {
  571. char fileName[256];
  572. sprintf(fileName, "d:\\stream%d_%d.264", i, gFileNum++/g_nTotalChannel);
  573. gFileHandle[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
  574. if(gFileHandle[i] == -1)
  575. {
  576. TRACE("channel %d file open error\n,i");
  577. return;
  578. }
  579. gChannelFrames[i] = 0;
  580. gChannelTotalLength[i] = 0;
  581. gChannelFramesLost[i] = 0;
  582. gChannelOverflow[i] = 0;
  583. gCurrentFileLen[i] = 0;
  584. _write(gFileHandle[i], FileHeader[i], FileHeaderLen);
  585. // could not be start again untill stopped first
  586. //m_bDspPreset[i] = FALSE;
  587. gCaptureStartedNum++;
  588. // let the threads have chance to run
  589. //Sleep(500);
  590. }
  591. else
  592. gFileHandle[i] = -1;
  593. }
  594. StartTime = timeGetTime();
  595. LastTime = timeGetTime();
  596. if(gCaptureStartedNum)
  597. {
  598. GetDlgItem(IDC_STOPCAP)->EnableWindow(TRUE);
  599. GetDlgItem(IDC_START)->EnableWindow(FALSE);
  600. GetDlgItem(IDC_SETTINGS)->EnableWindow(FALSE);
  601. }
  602. }
  603. /*
  604. StartVideoCapture(ChannelHandle[0]);
  605. bCapture = TRUE;
  606. */
  607. }
  608. void CHKVisionDlg::LogMessage(const unsigned char *buf, void *context)
  609. {
  610. if(!IsWindow(this->m_hWnd))return;
  611. int len = m_Logwin.GetWindowTextLength();
  612. CString str;
  613. if (len >= 16384)
  614. {
  615. m_Logwin.GetWindowText(str);
  616. m_Logwin.SetWindowText(str.GetBuffer(len)+16384);
  617. len -= 16384;
  618. }
  619. str = buf;
  620. m_Logwin.SetSel(len, len);
  621. m_Logwin.ReplaceSel(str);
  622. }
  623. void CHKVisionDlg::OnClose()
  624. {
  625. // TODO: Add your message handler code here and/or call default
  626. Close();
  627. // _close(logFile);
  628. CDialog::OnClose();
  629. }
  630. void CHKVisionDlg::Close()
  631. {
  632. bCapture = FALSE;
  633. MP4_ServerStop();
  634. m_bLog = FALSE;
  635. KillTimer(gTimer);
  636. if(gCaptureStartedNum)
  637. OnStop();
  638. gCaptureStartedNum = 0;
  639. DeInitDSPs();
  640. }
  641. void CHKVisionDlg::OnStop()
  642. {
  643. // TODO: Add your control notification handler code here
  644. StopCaptureVideo( 2 );
  645. return;
  646. CSelectDSP selDialog;
  647. selDialog.DSPPreset(m_bDspPreset, g_nTotalChannel);
  648. // bCapture = FALSE;
  649. // if(selDialog.DoModal() == IDOK){
  650. {
  651. LogWrite("Stop capture\n");
  652. // TRACE("SelectDSP:OK button clicked\n");
  653. selDialog.GetDSPsChoiced(m_bDspPreset);
  654. for(int i = 0; i < g_nTotalChannel; i++){
  655. if(m_bDspPreset[i]){
  656. ASSERT(gFileHandle[i] != -1);
  657. // StopVideoCapture(ChannelHandle[i]);
  658. //lseek(gFileHandle[i], 0, SEEK_SET);
  659. //FRAMES_STATISTICS fs;
  660. //GetFramesStatistics(ChannelHandle[i], &fs);
  661. //ULONG frames = fs.AudioFrames + fs.VideoFrames;
  662. //TRACE("channel %i has %x frames written\n", i, frames);
  663. #define END_CODE 0x00000002
  664. ULONG endCode = END_CODE;
  665. _write(gFileHandle[i], &endCode, sizeof(ULONG));
  666. _close(gFileHandle[i]);
  667. ///add v34
  668. if (bEncodeCifAndQcif[i])
  669. _close(gFileHandleQcif[i]);
  670. gCaptureStartedNum--;
  671. }
  672. }
  673. if(!gCaptureStartedNum){
  674. GetDlgItem(IDC_START)->EnableWindow(TRUE);
  675. GetDlgItem(IDC_SETTINGS)->EnableWindow(TRUE);
  676. GetDlgItem(IDC_STOPCAP)->EnableWindow(FALSE);
  677. }
  678. }
  679. }
  680. void CHKVisionDlg::StartVideoPreview()
  681. {
  682. }
  683. void CHKVisionDlg::StopVideoPreview()
  684. {
  685. // TRACE("StopVideoPreview\n");
  686. for(int i = 0; i < g_nTotalChannel; i++){
  687. ::StopVideoPreview(ChannelHandle[i]);
  688. }
  689. }
  690. void CHKVisionDlg::OnPreview()
  691. {
  692. /*
  693. // TODO: Add your control notification handler code here
  694. static BOOLEAN bVideoStarted = FALSE;
  695. if(bVideoStarted){
  696. StopVideoPreview();
  697. bVideoStarted = FALSE;
  698. }else{
  699. StartVideoPreview();
  700. bVideoStarted = TRUE;
  701. }
  702. */
  703. //////以下为演示程序
  704. char ctemp[256];
  705. sprintf(ctemp,"192.0.0.214");
  706. MP4_ServerStringToClient((LPCTSTR)ctemp,"receive",7);
  707. }
  708. HBRUSH CHKVisionDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
  709. {
  710. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  711. // TODO: Change any attributes of the DC here
  712. if((nCtlColor== CTLCOLOR_EDIT) )//&& (pWnd->GetDlgCtrlID() == IDC_EDIT1))
  713. {
  714. pDC->SetTextColor (RGB (255, 0 , 0));
  715. pDC->SetBkColor(gBackgroundColor);
  716. hbr = (HBRUSH) m_brush1;
  717. }
  718. if((nCtlColor== CTLCOLOR_EDIT) )//&& (pWnd->GetDlgCtrlID() == IDC_LOGWIN))
  719. {
  720. pDC->SetTextColor (RGB (255, 0 , 0));
  721. pDC->SetBkColor(RGB(0, 0, 0));
  722. // pDC->SetBkColor(RGB(255, 0, 255));
  723. hbr = (HBRUSH) m_brush2;
  724. }
  725. // TODO: Return a different brush if the default is not desired
  726. return hbr;
  727. }
  728. void CHKVisionDlg::OnShowWindow(BOOL bShow, UINT nStatus)
  729. {
  730. CDialog::OnShowWindow(bShow, nStatus);
  731. // TODO: Add your message handler code here
  732. // TRACE("Onshowwindow m_hwnd is %x\n", m_hWnd);
  733. m_bLog = TRUE;
  734. }
  735. void CHKVisionDlg::OnMove(int x, int y)
  736. {
  737. CDialog::OnMove(x, y);
  738. if(!dlgInited)return;
  739. // TODO: Add your message handler code here
  740. //::SendMessage(GetDlgItem(IDC_VIDEOWIN)->m_hWnd, WM_PAINT, 0, 0);
  741. }
  742. void CHKVisionDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  743. {
  744. // TODO: Add your message handler code here and/or call default
  745. CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
  746. }
  747. void CHKVisionDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
  748. {
  749. /* // TODO: Add your message handler code here and/or call default
  750. int bright, contrast, sat, hue, standard;
  751. if((GetDlgItem(IDC_SLIDER_BRIGHT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
  752. for(int i = 0; i < g_nTotalChannel; i++){
  753. GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
  754. bright = (nPos * 255) / 100;
  755. / TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
  756. SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
  757. }
  758. }
  759. if((GetDlgItem(IDC_SLIDER_CONTRAST) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
  760. for(int i = 0; i < g_nTotalChannel; i++){
  761. GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
  762. contrast = (nPos * 127) / 100;
  763. TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
  764. SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
  765. }
  766. }
  767. if((GetDlgItem(IDC_SLIDER_SAT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
  768. for(int i = 0; i < g_nTotalChannel; i++){
  769. GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
  770. sat = (nPos * 127) / 100;
  771. TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
  772. SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
  773. }
  774. }
  775. if((GetDlgItem(IDC_SLIDER_HUE) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
  776. for(int i = 0; i < g_nTotalChannel; i++){
  777. GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
  778. hue = (nPos * 255) / 100;
  779. TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
  780. SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
  781. }
  782. }
  783. */
  784. CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
  785. }
  786. int CHKVisionDlg::OnUpdateCounters(UINT wparam, INT lparam)
  787. {
  788. CString str;
  789. str.Format("%i", wparam);
  790. //GetDlgItem(IDC_IFRAMES)->SetWindowText((LPCTSTR)str);
  791. return 0;
  792. }
  793. void CHKVisionDlg::OnLButtonDown(UINT nFlags, CPoint point)
  794. {
  795. // TODO: Add your message handler code here and/or call default
  796. CDialog::OnLButtonDown(nFlags, point);
  797. }
  798. int CHKVisionDlg::OnDataReady(UINT channelNum, INT lparam)
  799. {
  800. ULONG length = 1000000;
  801. int frameType;
  802. int status,i;
  803. BOOL breakable;
  804. CString ctip;
  805. int nframetype =0;
  806. // if cap images we need clean the queue here
  807. // if (!bCapture)
  808. // return 0;
  809. status = ReadStreamData(ChannelHandle[channelNum], StreamBuf, &length, &frameType);
  810. // error occured
  811. if(status < 0){
  812. TRACE("error:ReadStreamData %x\n", status);
  813. return 0;
  814. }
  815. ///add v34
  816. // nframetype value: 1 cif head; 2 qcif head; 3 cif frame; 4 qcif frame; 5 audio frame; 0 others
  817. //在CIF,QICF同时编码时,要对子通道的数据流分别处理
  818. // CIF的数据存入CIF文件中,QCIF的数据存入QCIF文件中,音频数据要分别存储
  819. // if (bEncodeCifAndQcif[channelNum])
  820. // nframetype = GetSubChannelStreamType(&StreamBuf,frameType);
  821. breakable = status;
  822. // no errors
  823. if(frameType > 0) {
  824. if(frameType == PktSysHeader){
  825. if (bEncodeCifAndQcif[channelNum])
  826. {
  827. if (nframetype == 1) //get cif file header
  828. {
  829. memcpy(FileHeader[channelNum],StreamBuf,length);
  830. FileHeaderLen = length;
  831. }
  832. else if (nframetype ==2) //get qcif or qqcif file header
  833. {
  834. memcpy(FileHeaderQcif[channelNum], StreamBuf, length);
  835. TRACE("Ch%02d get Qcifhead or QQcifhead!\n",channelNum);
  836. FileHeaderLen = length;
  837. }
  838. }
  839. else{
  840. TRACE("channel %d get the file header !\n",channelNum);
  841. // store the file header
  842. memcpy(FileHeader[channelNum], StreamBuf, length);
  843. FileHeaderLen = length;
  844. }
  845. }
  846. if(frameType == PktMotionDetection)
  847. {
  848. m_VideoWin.DrawVect(channelNum, (char *)StreamBuf, length);
  849. return 0;
  850. }
  851. if(frameType == PktOrigImage){
  852. return 0;
  853. }
  854. }
  855. if(length == 0){
  856. TRACE("no data ?\n");
  857. return 0;
  858. }
  859. if(frameType == PktIFrames){
  860. int iii=1;
  861. }
  862. ULONG currentTime = timeGetTime();
  863. gChannelTotalLength[channelNum] += length;
  864. gCurrentFileLen[channelNum] += length;
  865. if(currentTime > StartTime+1000){
  866. CString str,str2;
  867. str.Format("%d", (gChannelTotalLength[dcurrentwin] *8/(currentTime - StartTime)));
  868. for(i=0;i<g_nTotalChannel;i++)
  869. gChannelTotalLength[i] = 0;
  870. StartTime= currentTime;
  871. GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
  872. }
  873. /*
  874. if((gCurrentFileLen[wparam] > gFileSize * 1000 * 1000) && breakable){
  875. TRACE("switch file:frametype=%x\n", frameType);
  876. _close(gFileHandle[wparam]);
  877. // create new file
  878. char fileName[256];
  879. sprintf(fileName, "stream%d_%d.mp4", wparam, gFileNum++/g_nTotalChannel);
  880. gFileHandle[wparam] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
  881. // write the header first
  882. _write(gFileHandle[wparam], FileHeader, FileHeaderLen);
  883. gCurrentFileLen[wparam] -= gFileSize * 1000 * 1000;
  884. }
  885. */
  886. if(frameType ==PktAudioFrames)
  887. {
  888. _write(gFileHandleQcif[channelNum],StreamBuf,length);
  889. MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,1);
  890. _write(gFileHandle[channelNum], StreamBuf, length);
  891. MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,0);
  892. }else if (frameType ==PktSubIFrames || frameType ==PktSubPFrames || frameType == PktSubBBPFrames || frameType == PktSubSysHeader)
  893. {
  894. _write(gFileHandleQcif[channelNum],StreamBuf,length);
  895. MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,1);
  896. }else
  897. {
  898. _write(gFileHandle[channelNum], StreamBuf, length);
  899. MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,0);
  900. }
  901. return 0;
  902. }
  903. void CHKVisionDlg::OnSettings()
  904. {
  905. // TODO: Add your control notification handler code here
  906. int i;
  907. CSettingsDlg dlg;
  908. int nResponse = dlg.DoModal();
  909. if (nResponse == IDOK){
  910. // TRACE("quant = %d file size = %d streamType = %d\n",
  911. // dlg.m_Quant, dlg.m_FileSize , dlg.m_StreamType);
  912. // setup quant value
  913. USHORT Format1[40] = {48, 16, 'H','I','K','V', 'I','S','I','O','N', '\0'};
  914. USHORT Format2[40] = {24, 50, _OSD_YEAR4, '-',_OSD_MONTH2,'-',_OSD_DAY,'-', _OSD_HOUR24, ':', _OSD_MINUTE,':', _OSD_SECOND, '\0'};
  915. PictureFormat_t encodeType ;
  916. for( i = 0; i < g_nTotalChannel; i++){
  917. StopVideoCapture(ChannelHandle[i]);
  918. }
  919. switch(dlg.m_iEncodeType)
  920. {
  921. case 0:
  922. encodeType = ENC_4CIF_FORMAT;
  923. break;
  924. case 1:
  925. encodeType = ENC_2CIF_FORMAT;
  926. break;
  927. case 2:
  928. encodeType = ENC_DCIF_FORMAT;
  929. break;
  930. case 3:
  931. encodeType = ENC_CIF_FORMAT;
  932. break;
  933. case 4:
  934. encodeType = ENC_QCIF_FORMAT;
  935. break;
  936. default:
  937. encodeType = ENC_CIF_FORMAT;
  938. break;
  939. }
  940. for(i = 0; i < g_nTotalChannel; i++)
  941. {
  942. if(encodeType != ENC_4CIF_FORMAT)
  943. {
  944. SetEncoderPictureFormat(ChannelHandle[i],encodeType);
  945. }
  946. else if ((i%4==0)||(i%4==1))
  947. {
  948. SetEncoderPictureFormat(ChannelHandle[i],encodeType);
  949. }
  950. }
  951. for(i = 0; i < g_nTotalChannel; i++){
  952. SetBitrateControlMode(ChannelHandle[i], dlg.m_bCbr ?brCBR : brVBR );
  953. }
  954. for( i = 0; i < g_nTotalChannel; i++)
  955. {
  956. if (!dlg.m_bCbr)
  957. {
  958. SetDefaultQuant(ChannelHandle[i], dlg.m_iQuant, dlg.m_pQuant, dlg.m_bQuant);
  959. SetupBitrateControl(ChannelHandle[i],dlg.m_dwbitrate);
  960. }
  961. else
  962. SetupBitrateControl(ChannelHandle[i],dlg.m_dwbitrate);
  963. }
  964. if(dlg.m_OsdEnable){
  965. for(i = 0; i < g_nTotalChannel; i++){
  966. SetOsdDisplayMode(ChannelHandle[i], 255, TRUE, 0, Format1, Format2);
  967. SetOsd(ChannelHandle[i], TRUE);
  968. }
  969. }else {
  970. for(i = 0; i < g_nTotalChannel; i++){
  971. SetOsd(ChannelHandle[i], FALSE);
  972. }
  973. }
  974. if(dlg.m_IFramesIntervals < 12){
  975. AfxMessageBox("Invalid Key Frames Invervals\n");
  976. return;
  977. }else{
  978. if((dlg.m_FrameRate > 25) || (dlg.m_FrameRate <=0))
  979. dlg.m_FrameRate = 25;
  980. for( i = 0; i < g_nTotalChannel; i++){
  981. SetIBPMode(ChannelHandle[i], dlg.m_IFramesIntervals, 2, 1, dlg.m_FrameRate);
  982. }
  983. }
  984. if(dlg.m_MotionDetect){
  985. for( i = 0; i < g_nTotalChannel; i++){
  986. StartMotionDetection(ChannelHandle[i]);
  987. }
  988. }else{
  989. for( i = 0; i < g_nTotalChannel; i++){
  990. StopMotionDetection(ChannelHandle[i]);
  991. }
  992. }
  993. USHORT streamType = STREAM_TYPE_AVSYNC;
  994. switch(dlg.m_StreamType){
  995. case 0:
  996. streamType = STREAM_TYPE_VIDEO;
  997. break;
  998. case 1:
  999. streamType = STREAM_TYPE_AUDIO;
  1000. break;
  1001. case 2:
  1002. streamType = STREAM_TYPE_AVSYNC;
  1003. break;
  1004. }
  1005. for(i = 0; i < g_nTotalChannel; i++){
  1006. SetStreamType(ChannelHandle[i], streamType);
  1007. }
  1008. if(dlg.m_logoEnable){
  1009. int w,h;
  1010. unsigned char *bitsBuffer = (unsigned char *)malloc(128 * 128 * 2);
  1011. if(bitsBuffer == NULL)
  1012. return;
  1013. LoadYUVFromBmpFile("logo.bmp", bitsBuffer, 128 * 128 * 2, &w, &h);
  1014. for( i = 0; i < g_nTotalChannel; i++){
  1015. SetLogo(ChannelHandle[i], 0, 110, 128, 128, (UCHAR *)bitsBuffer);
  1016. SetLogoDisplayMode(ChannelHandle[i], RGB(0,0,0),TRUE, 0);
  1017. }
  1018. free(bitsBuffer);
  1019. }else{
  1020. for( i = 0; i < g_nTotalChannel; i++){
  1021. StopLogo(ChannelHandle[i]);
  1022. }
  1023. }
  1024. gFileSize = dlg.m_FileSize;
  1025. for( i = 0; i < g_nTotalChannel; i++){
  1026. if(encodeType != ENC_4CIF_FORMAT)
  1027. {
  1028. StartVideoCapture(ChannelHandle[i]);
  1029. }
  1030. else if ((i%4==0)||(i%4==1))
  1031. {
  1032. StartVideoCapture(ChannelHandle[i]);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. void CHKVisionDlg::OnTimer(UINT nIDEvent)
  1038. {
  1039. // TODO: Add your message handler code here and/or call default
  1040. if (nIDEvent == 1)
  1041. {
  1042. for(int i = 0; i < g_nTotalChannel; i++){
  1043. if(!GetVideoSignal(ChannelHandle[i])){
  1044. CheckDlgButton(IDC_CHECK2 + i, BST_CHECKED);
  1045. }else{
  1046. // TRACE("Channel%d Video lost !\n", i);
  1047. CheckDlgButton(IDC_CHECK2 + i, BST_UNCHECKED);
  1048. }
  1049. }
  1050. }
  1051. else if (nIDEvent == 2)
  1052. {
  1053. CString str;
  1054. WORD num;
  1055. MP4_ServerGetState(&num);
  1056. str.Format("%d",num);
  1057. GetDlgItem(IDC_FPS)->SetWindowText((LPCTSTR)str);
  1058. }
  1059. else if (nIDEvent == 3)//判断没移动多久了
  1060. {
  1061. DWORD dwTicks = GetTickCount();
  1062. for( int i=0;i<64;i++ )
  1063. {
  1064. int a = ( dwTicks-m_dwNoMotionStartTime[i])/1000;
  1065. if( m_bCapture[i] && a > g_NoMotionTime( g_nNoMotionTime ) )
  1066. {
  1067. StopCaptureVideo( i );
  1068. }
  1069. }
  1070. }
  1071. else if (nIDEvent == 10)
  1072. {
  1073. CaptureIFrame(ChannelHandle[0]);
  1074. }
  1075. //CPLAYER_API STATISTIC* __stdcall MP4_ServerGetStatistic(DWORD nChannel,int *returnnum);
  1076. CDialog::OnTimer(nIDEvent);
  1077. }
  1078. void CHKVisionDlg::OnCapimage()
  1079. {
  1080. // TODO: Add your control notification handler code here
  1081. }
  1082. void CHKVisionDlg::OnDestroy()
  1083. {
  1084. if (usframe)
  1085. KillTimer(usframe);
  1086. CDialog::OnDestroy();
  1087. }
  1088. void CHKVisionDlg::OnMyCommand(WPARAM wParam, LPARAM lParam)
  1089. {
  1090. if ((BYTE)wParam == GETSTRING)
  1091. {
  1092. char ts[1000];
  1093. char ips[30];
  1094. WORD tlen;
  1095. memset(ts,0,1000);
  1096. MP4_ServerReadLastMessage(ips,ts,&tlen);
  1097. sprintf(&(ts[tlen]),"\r\n");
  1098. AfxMessageBox(ts);
  1099. // TRACE(ts);
  1100. }
  1101. }
  1102. void CHKVisionDlg::OnButton5()
  1103. {
  1104. MP4_ServerCommandToClient(20,0);
  1105. }
  1106. void CHKVisionDlg::OnButton6()
  1107. {
  1108. char sendcont[500];
  1109. CString firstco;
  1110. firstco = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
  1111. int testnum=0;
  1112. MP4_ServerSetWait(3000,5);
  1113. UpdateData(TRUE);
  1114. // for(int i=0;i<100;i++)
  1115. {
  1116. memset(sendcont,0,500);
  1117. UpdateData(TRUE);
  1118. sprintf(sendcont,"%s send content %d",firstco,testnum);
  1119. testnum++;
  1120. if (testnum > 200)
  1121. testnum=0;
  1122. MP4_ServerStringToClient(m_ip,sendcont,400);
  1123. Sleep(100);
  1124. }
  1125. }
  1126. void CHKVisionDlg::OnButton7()
  1127. {
  1128. MP4_ServerResetChannel(1);
  1129. }
  1130. void CHKVisionDlg::OnButton8()
  1131. {
  1132. MP4_ServerStringToClient_other(0,"123456789",10);
  1133. }
  1134. void CHKVisionDlg::OnCapturesframe()
  1135. {
  1136. UpdateData(TRUE);
  1137. if (m_sframe)
  1138. {
  1139. usframe = SetTimer(10,1000,NULL);
  1140. }
  1141. else
  1142. {
  1143. if (usframe)
  1144. KillTimer(usframe);
  1145. }
  1146. }
  1147. void CHKVisionDlg::OnButton9()
  1148. {
  1149. if (chantype)
  1150. {
  1151. for(int i = 0; i < g_nTotalChannel; i++)
  1152. {
  1153. StopVideoCapture(ChannelHandle[i]);
  1154. MP4_ServerChangeChanType(i,NORMAL);
  1155. SetIBPMode(ChannelHandle[i],211,2,2,25);
  1156. SetDefaultQuant(ChannelHandle[i],15,15,20);
  1157. SetEncoderPictureFormat(ChannelHandle[i], ENC_CIF_FORMAT);
  1158. StartVideoCapture(ChannelHandle[i]);
  1159. }
  1160. chantype = FALSE;
  1161. }
  1162. else
  1163. {
  1164. for(int i = 0; i < g_nTotalChannel; i++)
  1165. {
  1166. StopVideoCapture(ChannelHandle[i]);
  1167. MP4_ServerChangeChanType(i,DIALING);
  1168. SetIBPMode(ChannelHandle[i],211,2,2,8);
  1169. SetDefaultQuant(ChannelHandle[i],18,18,23);
  1170. SetStreamType(ChannelHandle[i],STREAM_TYPE_VIDEO);
  1171. SetEncoderPictureFormat(ChannelHandle[i], ENC_QCIF_FORMAT);
  1172. StartVideoCapture(ChannelHandle[i]);
  1173. }
  1174. chantype = TRUE;
  1175. }
  1176. }
  1177. //typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
  1178. int __cdecl StreamDirectReadCallback(ULONG channelNum,void *DataBuf,DWORD Length,int frameType,void *context)
  1179. {
  1180. //判断硬盘空间
  1181. CHKVisionDlg* pdlg = g_pDlgMain->m_pMonitor;
  1182. if( !g_DiskVelumnIsEnough( g_nDiskVolume,g_strVideoPath ) )
  1183. {
  1184. if( g_nOperateType==1 )
  1185. {
  1186. for( int i=0;i<64;i++ )
  1187. {
  1188. if( pdlg->m_bCapture[i] )
  1189. {
  1190. pdlg->StopCaptureVideo( i );
  1191. }
  1192. }
  1193. return false;
  1194. }
  1195. else if( g_nOperateType==0 )
  1196. {
  1197. CString str;
  1198. for( int i=0;i<5;i++ )
  1199. {
  1200. if( g_strAllVideoFile.GetCount()>0 )
  1201. {
  1202. str = g_strAllVideoFile[0];
  1203. DeleteFile( str );
  1204. g_strAllVideoFile.RemoveAt( 0 );
  1205. }
  1206. }
  1207. }
  1208. }
  1209. //判断视频时间
  1210. DWORD dwTicks = GetTickCount();
  1211. int nTime = (dwTicks-pdlg->m_dwVideoStartTime[channelNum])/1000;
  1212. if( pdlg->m_bCapture[channelNum] && nTime > g_VideoMaxTime( g_nVideoMaxTime ) )
  1213. {
  1214. pdlg->StopCaptureVideo( channelNum );
  1215. }
  1216. int status=0;
  1217. CString ctip;
  1218. int nframetype =0;
  1219. // no errors
  1220. if(frameType > 0)
  1221. {
  1222. if(frameType == PktSysHeader)
  1223. {
  1224. // store the file header
  1225. memcpy(FileHeader[channelNum], DataBuf, Length);
  1226. FileHeaderLen = Length;
  1227. TRACE("channel %d get the file header !\n",channelNum);
  1228. }
  1229. if(frameType == PktIFrames || frameType ==PktSubIFrames)
  1230. {
  1231. status = 1;
  1232. }
  1233. else
  1234. {
  1235. status = 0;
  1236. }
  1237. if(frameType == PktMotionDetection)
  1238. {
  1239. int result[4];
  1240. //if(g_bOverlayMode)
  1241. //{
  1242. // pdlg->m_VideoWin.DrawVectEx(channelNum, (char *)DataBuf);
  1243. //}
  1244. //else
  1245. //{
  1246. // pdlg->m_bMoving[channelNum] = TRUE;
  1247. // pdlg(motionData[channelNum],(char*)DataBuf,Length);
  1248. //}
  1249. MotionAnalyzer(ChannelHandle[channelNum],(char*)DataBuf,15,result);
  1250. if((result[0] + result[1] + result[2] + result[3]))
  1251. {
  1252. if( !pdlg->m_bCapture[channelNum] )
  1253. pdlg->StartCaptureVideo( channelNum );
  1254. pdlg->m_dwNoMotionStartTime[channelNum] = GetTickCount();
  1255. //if (channelNum ==0)
  1256. // TRACE("!!!!!!!!!!!!!!!!CH=%d motion detect result is %d %d %d %d\n",channelNum,result[0],\
  1257. // result[1],result[2],result[3]);
  1258. //// Trigger函数的作用是把报警前的数据按setup里面的设置,把相应的警前帧写入文件
  1259. //if(!pdlg->baftermotion[channelNum])
  1260. // //alarmFile[channelNum].Trigger(gFileHandle[channelNum]);
  1261. ////每次有移动侦测帧上来,自动把ualreadywriteframes[channelNum]清为0 ,为写入警后帧作准备
  1262. //pdlg->ualreadywriteframes[channelNum]=0;
  1263. //pdlg->baftermotion[channelNum]=TRUE;
  1264. }
  1265. return 0;
  1266. }
  1267. if(frameType == PktOrigImage)
  1268. {
  1269. return 0;
  1270. }
  1271. }
  1272. if(Length == 0)
  1273. {
  1274. TRACE("no data ?\n");
  1275. return 0;
  1276. }
  1277. ULONG currentTime = timeGetTime();
  1278. gChannelTotalLength[channelNum] += Length;
  1279. gCurrentFileLen[channelNum] += Length;
  1280. if(currentTime > StartTime+1000)
  1281. {
  1282. CString str,str2;
  1283. str.Format("%d", (gChannelTotalLength[dcurrentwin] *8/(currentTime - StartTime)));
  1284. for(int i=0;i<g_nTotalChannel;i++)
  1285. gChannelTotalLength[i] = 0;
  1286. StartTime= currentTime;
  1287. }
  1288. if(frameType ==PktAudioFrames)
  1289. {
  1290. _write(gFileHandleQcif[channelNum],DataBuf,Length);
  1291. MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,1);
  1292. _write(gFileHandle[channelNum], DataBuf, Length);
  1293. MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,0);
  1294. }
  1295. else if (frameType ==PktSubIFrames || frameType ==PktSubPFrames || frameType == PktSubBBPFrames || frameType == PktSubSysHeader)
  1296. {
  1297. _write(gFileHandleQcif[channelNum],DataBuf,Length);
  1298. MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,1);
  1299. }
  1300. else
  1301. {
  1302. _write(gFileHandle[channelNum], DataBuf, Length);
  1303. MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,0);
  1304. }
  1305. return 0;
  1306. }
  1307. void CHKVisionDlg::OnSubstart()
  1308. {
  1309. // TODO: Add your control notification handler code here
  1310. for(int i=0; i < GetTotalChannels(); i++)
  1311. {
  1312. char fileName[256];
  1313. sprintf(fileName,"d:\\streamqcif%d_%d.264",i,gFileNum++);
  1314. gFileHandleQcif[i]= _open(fileName,_O_CREAT|_O_BINARY|_O_WRONLY|_O_TRUNC,_S_IREAD|_S_IWRITE);
  1315. if(gFileHandleQcif[i] ==-1)
  1316. {
  1317. TRACE("file Open error!\n");
  1318. AfxMessageBox("can not open file!\n");
  1319. return ;
  1320. }
  1321. SetSubStreamType(ChannelHandle[i],STREAM_TYPE_AVSYNC);
  1322. SetSubEncoderPictureFormat(ChannelHandle[i],ENC_CIF_FORMAT);
  1323. SetupSubChannel(ChannelHandle[i],1);// switch to sub channel for setting
  1324. SetBitrateControlMode(ChannelHandle[i], brVBR );
  1325. SetupBitrateControl(ChannelHandle[i],2000000);
  1326. SetDefaultQuant(ChannelHandle[i], 16, 16, 18);
  1327. SetIBPMode(ChannelHandle[i],100,2,2,25);
  1328. StartSubVideoCapture(ChannelHandle[i]);
  1329. SetupSubChannel(ChannelHandle[i],0);//switch to main channel
  1330. }
  1331. GetDlgItem(IDC_SUBSTART)->EnableWindow(FALSE);
  1332. GetDlgItem(IDC_STOPSUBCAP)->EnableWindow(TRUE);
  1333. }
  1334. void CHKVisionDlg::OnStopsubcap()
  1335. {
  1336. // TODO: Add your control notification handler code here
  1337. for(int i=0; i < GetTotalChannels(); i++)
  1338. {
  1339. StopSubVideoCapture(ChannelHandle[i]);
  1340. }
  1341. GetDlgItem(IDC_SUBSTART)->EnableWindow(TRUE);
  1342. GetDlgItem(IDC_STOPSUBCAP)->EnableWindow(FALSE);
  1343. }
  1344. bool CHKVisionDlg::StartCaptureVideo( int iChannel )
  1345. {
  1346. if( 0 != GetVideoSignal( (HANDLE)iChannel ) )
  1347. {
  1348. return false;
  1349. }
  1350. if( !g_DiskVelumnIsEnough( g_nDiskVolume,g_strVideoPath ) )
  1351. {
  1352. if( g_nOperateType==1 )
  1353. return false;
  1354. else if( g_nOperateType==0 )
  1355. {
  1356. CString str;
  1357. for( int i=0;i<5;i++ )
  1358. {
  1359. if( g_strAllVideoFile.GetCount()>0 )
  1360. {
  1361. str = g_strAllVideoFile[0];
  1362. DeleteFile( str );
  1363. g_strAllVideoFile.RemoveAt( 0 );
  1364. }
  1365. }
  1366. }
  1367. }
  1368. bool bRet=true;
  1369. int i = iChannel;
  1370. if( !m_bCapture[i] )
  1371. {
  1372. char fileName[256];
  1373. SYSTEMTIME st;
  1374. GetLocalTime( &st );
  1375. sprintf(fileName, "%s%d_%d-%02d-%02d-%02d%02d%02d.264", g_strVideoPath,i+1,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond );
  1376. gFileHandle[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
  1377. if(gFileHandle[i] == -1)
  1378. {
  1379. TRACE("channel %d file open error\n,i");
  1380. return false;
  1381. }
  1382. gChannelFrames[i] = 0;
  1383. gChannelTotalLength[i] = 0;
  1384. gChannelFramesLost[i] = 0;
  1385. gChannelOverflow[i] = 0;
  1386. gCurrentFileLen[i] = 0;
  1387. _write(gFileHandle[i], FileHeader[i], FileHeaderLen);
  1388. m_bCapture[i] = true;
  1389. m_dwVideoStartTime[i] = GetTickCount();
  1390. StartVideoCapture(ChannelHandle[i]);
  1391. gCaptureStartedNum++;
  1392. }
  1393. return bRet;
  1394. }
  1395. bool CHKVisionDlg::StopCaptureVideo( int iChannel )
  1396. {
  1397. bool bRet=true;
  1398. int i = iChannel;
  1399. if( m_bCapture[i])
  1400. {
  1401. ASSERT(gFileHandle[i] != -1);
  1402. m_bCapture[i] = false;
  1403. m_dwVideoStartTime[i] = GetTickCount();
  1404. m_dwNoMotionStartTime[i] = GetTickCount();
  1405. StopVideoCapture(ChannelHandle[i]);
  1406. #define END_CODE 0x00000002
  1407. ULONG endCode = END_CODE;
  1408. _write(gFileHandle[i], &endCode, sizeof(ULONG));
  1409. _close(gFileHandle[i]);
  1410. ///add v34
  1411. if (bEncodeCifAndQcif[i])
  1412. _close(gFileHandleQcif[i]);
  1413. gCaptureStartedNum--;
  1414. }
  1415. return bRet;
  1416. }
  1417. void CHKVisionDlg::GetAllVideoFile()
  1418. {
  1419. DWORD returnValue;
  1420. double dBegin=0.0,dEnd=0.0,dNow=0.0,dTemp=0.0;
  1421. g_strAllVideoFile.RemoveAll();
  1422. CString sPath,str;
  1423. if( SetCurrentDirectory(g_strVideoPath) == TRUE)
  1424. {
  1425. CFileFind finder;
  1426. BOOL bWorking = finder.FindFile("*.264");
  1427. while (bWorking)
  1428. {
  1429. bWorking = finder.FindNextFile();//1_2010-10-17-101010.264
  1430. sPath = finder.GetFileName();
  1431. g_strAllVideoFile.Add( sPath );
  1432. }
  1433. CString str1,str2,s1,s2,sTemp;
  1434. for( int i=0;i<g_strAllVideoFile.GetCount();i++ )
  1435. {
  1436. str1 = g_strAllVideoFile[i];
  1437. s1 = str1.Mid( 2,17 );
  1438. s1.Delete(10);
  1439. s1.Delete(7);
  1440. s1.Delete(4);
  1441. dBegin = atof(s1);
  1442. for( int j=i+1;j<g_strAllVideoFile.GetCount();j++ )
  1443. {
  1444. str2 = g_strAllVideoFile[j];
  1445. s2 = str2.Mid( 2,17 );
  1446. s2.Delete(10);
  1447. s2.Delete(7);
  1448. s2.Delete(4);
  1449. dEnd = atof(s2);
  1450. if( dEnd<dBegin )
  1451. {
  1452. dTemp = dBegin;
  1453. dBegin = dEnd;
  1454. dEnd = dTemp;
  1455. sTemp = str1;
  1456. str1 = str2;
  1457. str2 = sTemp;
  1458. g_strAllVideoFile[i] = str1;
  1459. g_strAllVideoFile[j] = str2;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. else
  1465. {
  1466. returnValue = GetLastError();
  1467. }
  1468. }
  1469. LRESULT CHKVisionDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  1470. {
  1471. // TODO: 在此添加专用代码和/或调用基类
  1472. switch( message )
  1473. {
  1474. case WM_SHOW:
  1475. RegisterDrawFun(int(lParam), DrawFun ,(DWORD)this);//注册画的回调
  1476. break;
  1477. }
  1478. return CDialog::WindowProc(message, wParam, lParam);
  1479. }
  1480. void CHKVisionDlg::StopDrawFun(DWORD nport)
  1481. {
  1482. StopRegisterDrawFun(nport);
  1483. }