1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786 |
- // HKVisionDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "HKVision.h"
- #include "HKVisionDlg.h"
- #include "SelectDSP.h"
- #include "settingsDlg.h"
- #include "hikserver.h"
- #include "hikvisionsdk.h"
- #include "Global.h"
- #include "mdlProject.h"
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <io.h>
- #include <stdio.h>
- #include <mmsystem.h>
- #include ".\hkvisiondlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WM_MYCOMMAND (WM_USER+1000) //
- #define DIALTYPE 0 //dialing
- #define NORMATYPE 1 //Wan or Lan
- #define UPDATE_COUNTERS __T("Update Frame Counters")
- #define DATA_READY __T("Data ready")
- BOOL bCapture = FALSE;
- BOOL g_bOverlayMode=FALSE;//如果函数SetPreviewOverlayMode的返回值为0,则bOverlayMode赋值为TRUE,表示启用Overlay方式预览
- unsigned char StreamBuf[1000000];
- int __cdecl StreamReadCallback(ULONG ChannelNum, void *Context);
- typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
- int __cdecl StreamDirectReadCallback(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
- unsigned char FileHeader[64][200];
- int FileHeaderLen = 0;
- int gFileHandleQcif[MAX_CHANNELS];
- int icount = 0;;
- int KeyFrameInterva[10] = {25,67,76,100,100,100,100,121,211,211};
- int FrameRate[10] = {25,25,25,25,20,10,5,4,3,2};
- int IQ[10] = {10,10,12,15,18,20,20,22,24,24};
- int PQ[10] = {10,10,12,15,18,20,20,22,24,24};
- int BQ[10] = {10,15,15,20,21,23,23,25,27,27};
- int nowstate[16];
- int servertype;
- UINT usframe = 0;
- BOOL chantype = FALSE;
- DWORD dcurrentwin=0;
- int g_nTotalChannel;
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- int SplitRects(int width, int height, int cols, int rows, RECT *rectList);
- /////////////////////////////////////////////////////////////////////////////
- // CHKVisionDlg dialog
- bool g_DiskVelumnIsEnough( int iDiskVolume,CString sPath )
- {
- bool bRet = false;
- double dVelume=0.0,dRemainVelume=0.0;
- switch( iDiskVolume )
- {
- case 0: dVelume = 500; break;
- case 1: dVelume = 1024*1; break;
- case 2: dVelume = 1024*3; break;
- case 3: dVelume = 1024*5; break;
- }
- ULARGE_INTEGER lpuse;
- ULARGE_INTEGER lptotal;
- ULARGE_INTEGER lpfree;
- GetDiskFreeSpaceEx(sPath,&lpuse,&lptotal,&lpfree);
- CString s;
- s.Format("%4.4fGB\n",lpfree.QuadPart/1024.0/1024.0);
- dRemainVelume = lpfree.QuadPart/1024.0/1024.0;
- if( dRemainVelume>=dVelume )
- bRet = true;
-
- return bRet;
- }
- int g_VideoMaxTime( int iType )
- {
- int nRet=0;
- switch( iType )
- {
- case 0: nRet = 3*60; break;
- case 1: nRet = 5*60; break;
- case 2: nRet = 10*60; break;
- case 3: nRet = 20*60; break;
- case 4: nRet = 30*60; break;
- case 5: nRet = 60*60; break;
- }
- return nRet;
- }
- int g_NoMotionTime( int iType )
- {
- int nRet=0;
- switch( iType )
- {
- case 0: nRet = 5; break;
- case 1: nRet = 10; break;
- case 2: nRet = 30; break;
- case 3: nRet = 60; break;
- case 4: nRet = 3*60; break;
- }
- return nRet;
- }
- void CALLBACK DrawFun(long nport, HDC hDc,LONG nUser)
- {
- CHKVisionDlg *dlg = g_pDlgMain->m_pMonitor;
- //if (nport == 0 ) //&& DisplayMode != 1)
- {
- //if (DisplayMode != 1)
- //{
- // int weight,height;
- // weight = rectList[iLastSelect].right - rectList[iLastSelect].left;
- // height = rectList[iLastSelect].bottom- rectList[iLastSelect].top;
- // HGDIOBJ pOldPen = ::SelectObject(hDc,&penWhite);
- // ::MoveToEx(hDc, 1, 1, NULL);
- // ::LineTo(hDc, weight -1, 1);
- // ::LineTo(hDc, weight -1,height-1);
- // ::LineTo(hDc, 1,height -1);
- // ::LineTo(hDc, 1,1);
- // ::SelectObject(hDc,pOldPen);
- //}
- CString str;
- if( dlg->m_bCapture[nport] )
- str.Format("录像中");
- SetTextColor(hDc, RGB(255,0,0));
- SetBkMode(hDc,TRANSPARENT);
- TextOut(hDc,5,5,str,str.GetLength());
- //if(dlg->m_bMoving[nport])
- {
- //dlg->m_VideoWin.DrawVect(nport,(char *)motionData[nport],hDc,pOwner->chstandard[nport]);
- //dlg->m_bMoving[nport] = FALSE;
- }
- }
- //else
- // dlg->StopDrawFun(nport);
- return;
- }
- CHKVisionDlg::CHKVisionDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CHKVisionDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CHKVisionDlg)
- m_brightness = 0;
- m_contrast = 0;
- m_hue = 0;
- m_saturation = 0;
- m_iframes = 0;
- //m_pbframes = 0;
- m_lostframes = 0;
- //m_audioframes = 0;
- m_ip = _T("");
- m_sframe = FALSE;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- //m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- memset( m_bHaveSignal,0,64 );
- memset( m_bCapture,0,64 );
- memset( m_dwVideoStartTime,0,sizeof(DWORD)*64 );
- memset( m_dwNoMotionStartTime,0,sizeof(DWORD)*64 );
- }
- void CHKVisionDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CHKVisionDlg)
- DDX_Slider(pDX, IDC_SLIDER_BRIGHT, m_brightness);
- DDX_Slider(pDX, IDC_SLIDER_CONTRAST, m_contrast);
- DDX_Slider(pDX, IDC_SLIDER_HUE, m_hue);
- DDX_Slider(pDX, IDC_SLIDER_SAT, m_saturation);
- //DDX_Text(pDX, IDC_IFRAMES, m_iframes);
- //DDX_Text(pDX, IDC_PBFRAMES, m_pbframes);
- DDX_Text(pDX, IDC_LOST_FRAMES, m_lostframes);
- //DDX_Text(pDX, IDC_AUDIOFRAMES, m_audioframes);
- DDX_Text(pDX, IDC_IP, m_ip);
- DDX_Check(pDX, IDC_CAPTURESFRAME, m_sframe);
- //}}AFX_DATA_MAP
- }
- static const UINT MsgUpdateCounters = ::RegisterWindowMessage(UPDATE_COUNTERS);
- static const UINT MsgDataReady = ::RegisterWindowMessage(DATA_READY);
- BEGIN_MESSAGE_MAP(CHKVisionDlg, CDialog)
- //chn ON_MESSAGE(WM_MYCOMMAND,OnMyCommand)
- //{{AFX_MSG_MAP(CHKVisionDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_START, OnStart)
- ON_WM_CLOSE()
- ON_BN_CLICKED(IDC_STOPCAP, OnStop)
- ON_BN_CLICKED(IDC_PREVIEW, OnPreview)
- ON_WM_CTLCOLOR()
- ON_WM_SHOWWINDOW()
- ON_WM_MOVE()
- ON_WM_HSCROLL()
- ON_WM_VSCROLL()
- ON_WM_LBUTTONDOWN()
- ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_CAPIMAGE, OnCapimage)
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
- ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
- ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
- ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
- ON_BN_CLICKED(IDC_CAPTURESFRAME, OnCapturesframe)
- ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
- ON_BN_CLICKED(IDC_SUBSTART, OnSubstart)
- //chn ON_REGISTERED_MESSAGE(MsgUpdateCounters, OnUpdateCounters)
- //chn ON_REGISTERED_MESSAGE(MsgDataReady, OnDataReady)
- ON_BN_CLICKED(IDC_STOPSUBCAP, OnStopsubcap)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CHKVisionDlg message handlers
- int LogWrite(const char *Format, ...);
- HANDLE ChannelHandle[MAX_CHANNELS];
- //int logFile;
- int gFileHandle[MAX_CHANNELS];
- BOOLEAN dlgInited = FALSE;
- HWND gMainWnd;
- RECT rectList[MAX_CHANNELS];
- BOOL bDdrawMode = FALSE;
- ULONG gChannelFrames[MAX_CHANNELS];
- ULONG gChannelTotalLength[MAX_CHANNELS];
- ULONG gChannelFramesLost[MAX_CHANNELS];
- ULONG gChannelOverflow[MAX_CHANNELS];
- ULONG gCurrentFileLen[MAX_CHANNELS];
- ULONG gCaptureStartedNum = 0;
- UINT gTimer;
- UINT gCapImages;
- ULONG gFileSize = 100; // 100 mb
- int gFileNum = 0;
- COLORREF gBackgroundColor = RGB(10, 10, 10);
- extern int iLastSelect;
- ULONG StartTime;
- ULONG LastTime;
- ///add ver3.4 by h
- //int gFileHandleQcif[MAX_CHANNELS]; //子通道同时编码时qcif录象文件句柄
- unsigned char FileHeaderQcif[MAX_CHANNELS][200]; //QCIF Header,when
- BOOL bEncodeCifAndQcif[MAX_CHANNELS]; //子通道同时编码时通道标志
- void CALLBACK StartCap(int port)
- {
- StartVideoCapture(ChannelHandle[port]);
- }
- void CALLBACK StopCap(int port)
- {
- StopVideoCapture(ChannelHandle[port]);
- }
- int CALLBACK CheckIP(DWORD nChannel,char* nIP)
- {
- /*
- CString ctemp;
- ctemp.Format("%s",nIP);
- if (ctemp == "192.0.0.215")
- return -1;
- */
- return 0;
- }
- int CALLBACK checkpassword(char *username,WORD namelen,char *password,WORD passlen)
- {
- /*
- if ((username[0] == '1') && (username[1] == '2') && (username[2] == '3') && (password[0] == 'w'))
- return 0;
- else
- return -1;
- */
- return 0;
- }
- void CALLBACK SetIBP(int port,int framerat)
- {
- SetIBPMode(ChannelHandle[port],211,2,2,framerat);
- }
- void CALLBACK MakeIFrame(int port)
- {
- CaptureIFrame(ChannelHandle[port]);
- }
- BOOL CHKVisionDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- int i;
- servertype = NORMATYPE;
- // Add "About..." menu item to system menu.
- m_brush1.CreateSolidBrush(gBackgroundColor);
- m_brush2.CreateSolidBrush(RGB(0,0,0));
- m_bLog = FALSE;
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- m_brightness = 50;
- m_contrast = 50;
- m_hue = 0;
- m_saturation = 50;
- m_iframes = 0;
- //m_pbframes = 0;
- //m_audioframes = 0;
- m_lostframes = 0;
- if(InitDSPs() < 0)
- {
- //afxDump<<"error:can not init DSPs\n";
- AfxMessageBox("can not init DSPs\n");
- return FALSE;
- }
- int iReturn=-1;
- //根据函数SetPreviewOverlayMode的返回值来判断当前的显卡是否支持Overlay方式预览,
- //0表示支持,其它值表示不支持
- iReturn=SetPreviewOverlayMode(TRUE);
- TRACE("iReturn=%04x\n",iReturn);
- if(iReturn==0)
- {
- g_bOverlayMode=TRUE;
- }
- g_nTotalChannel = GetTotalChannels();
- int nTemp = 0;
- for(i = 0; i < g_nTotalChannel; i++)//判斷是否有信號
- {
- if( 0 == GetVideoSignal( (HANDLE)i ) )
- {
- m_bHaveSignal[i] = true;
- nTemp = i;
- }
- }
- switch( nTemp )
- {
- case 1: g_nTotalChannel = 1; break;
- case 2:
- case 3:
- case 4: g_nTotalChannel = 4; break;
- case 5:
- case 6:
- case 7:
- case 8: g_nTotalChannel = 8; break;
- }
-
- for(i = 0; i < g_nTotalChannel; i++)
- {
- m_bDspPreset[i] = TRUE;
- bEncodeCifAndQcif[i] = FALSE;
- gFileHandle[i] = -1;
- }
- m_sframe = FALSE;
- UpdateData(FALSE);
- CString title;
- CString dsps;
- dsps.Format(" %d channels Found", g_nTotalChannel);
- GetWindowText(title);
- title += dsps;
- SetWindowText(title);
- dlgInited = TRUE;
- m_VideoWin.SubclassDlgItem(IDC_VIDEOWIN, this);
- for(i = 0; i < g_nTotalChannel; i++)
- {
- ChannelHandle[i] = ChannelOpen(i);
- if (ChannelHandle[i]<0)
- {
- AfxMessageBox("channel open error > 0");
- }
- else if (ChannelHandle[i] ==(HANDLE) 0xffff)
- {
- AfxMessageBox("channel open error 0xffff");
- }
- if( m_bHaveSignal[i] )
- {
- RegisterDrawFun(i, DrawFun ,(DWORD)this);//注册画的回调
- VideoStandard_t videostandard;
- int bright,constrast,sat,hue;
- int bHeight;
- GetVideoPara(ChannelHandle[i],&videostandard,&bright,&constrast,&sat,&hue);
- AdjustMotionDetectPrecision(ChannelHandle[i], 0x80000000|6, 6, 0);//设置移动侦测灵敏度
- if(videostandard==1)
- {
- bHeight=480;
- }
- else
- {
- bHeight=576;
- }
- RECT areaList[4];
- areaList[0].top = 0; areaList[0].left = 0;
- areaList[0].right = 704/2; areaList[0].bottom = bHeight/2;
-
- areaList[1].top = 0; areaList[1].left = 704/2;
- areaList[1].right = 703; areaList[1].bottom = bHeight/2;
-
- areaList[2].top = bHeight/2; areaList[2].left = 0;
- areaList[2].right = 704/2; areaList[2].bottom = bHeight-1;
-
- areaList[3].top = bHeight/2; areaList[3].left = 704/2;
- areaList[3].right = 703; areaList[3].bottom = bHeight-1;
- SetupMotionDetection(ChannelHandle[i], areaList, 4);
- StartMotionDetection(ChannelHandle[i]);
- }
- gChannelTotalLength[i] = 0;
- nowstate[i]=0;
- if (servertype == DIALTYPE)
- {
- SetIBPMode(ChannelHandle[i],211,2,1,8);
- SetDefaultQuant(ChannelHandle[i],18,18,23);
- SetStreamType(ChannelHandle[i],STREAM_TYPE_VIDEO);
- }
- else
- {
- SetIBPMode(ChannelHandle[i],100,2,1,25);
- SetDefaultQuant(ChannelHandle[i],15,15,20);
- }
- }
- if (servertype == DIALTYPE)
- {
- for(i = 0; i < g_nTotalChannel; i++)
- SetEncoderPictureFormat(ChannelHandle[i], ENC_QCIF_FORMAT);
- }
- else
- {
- for(i = 0; i < g_nTotalChannel; i++)
- {
- if ( i==0 )
- {
- //when initiated,set the first channel as 4CIF encode,others as CIF
- SetEncoderPictureFormat(ChannelHandle[0], ENC_4CIF_FORMAT);
- bEncodeCifAndQcif[0] = FALSE;
- }
- else
- {
- SetEncoderPictureFormat(ChannelHandle[i], ENC_CIF_FORMAT);
- }
- }
- }
- // int id = IDC_CHECK2;
- // for(i = 0; i < MAX_CHANNELS; i++){
- // GetDlgItem(id + i)->EnableWindow(FALSE);
- // }
- RegisterStreamDirectReadCallback(::StreamDirectReadCallback,this);
- RegisterMessageNotifyHandle(m_hWnd, MsgDataReady);
- MP4_ServerSetMessage(WM_MYCOMMAND,this->m_hWnd);
- gCapImages = 0;
- SetOverlayColorKey(gBackgroundColor);
- gTimer = SetTimer(1, 1000, 0);
- SetTimer(2,2000,0);
- SetTimer(3,1000,0);//是否到没移动的最长时间
- SetTimer(5,5000,0);
- for (i=0;i<MAX_CHANNELS;i++)
- gCurrentFileLen[i] = 0;
- SERVER_VIDEOINFO videoinfo;
-
- for( i=0 ; i < g_nTotalChannel; i++ )
- {
- if(i == 0)
- {
- MP4_ServerSetBufNum(i,90);
- }
- else
- {
- MP4_ServerSetBufNum(i,80);
- }
-
- if (servertype == DIALTYPE)
- videoinfo.m_datatype[i] = DIALING;
- else
- videoinfo.m_datatype[i] = NORMAL;
- }
- videoinfo.m_datatype[0] = SMALLPIC;
- videoinfo.m_channum = g_nTotalChannel;
- videoinfo.m_waittime = 2;
- MP4_ServerSetStart(StartCap);
- MP4_ServerSetStop(StopCap);
- MP4_ServerSetIBPMode(SetIBP);
- MP4_ServerSetCapIFrame(MakeIFrame);
- MP4_ServerSetTTL(64);
- MP4_ServerSetNetPort(5050,6050);
- MP4_ServerCheckIP(CheckIP);
- MP4_ServerCheckPassword(checkpassword);
- //set the max connector of channel 0
- MP4_ServerMaxUser(0,24);
- //如果想不使用缺省方式进行多播,
- //可以调用下面的函数设置自己的多播信息
- //详细信息请参考SDK文档
- // MP4_ServerCastGroup(TRUE,0,"228.0.0.132",9988);
- //if (!MP4_ServerStart(&videoinfo))
- //{
- // MessageBox("error","error",MB_OK);
- //}
- StartTime = timeGetTime();
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CHKVisionDlg::SetCtrlPos( CRect rect )
- {
- CRect rc;
- rc.left = 0;
- rc.top = 0;
- rc.right = rc.left+rect.Width();
- rc.bottom = rc.top+rect.Height();
- this->MoveWindow( rc );
- GetDlgItem(IDC_VIDEOWIN)->MoveWindow( rc );
- }
- void CHKVisionDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- //if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- //{
- // CAboutDlg dlgAbout;
- // dlgAbout.DoModal();
- //}
- //else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CHKVisionDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CHKVisionDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- int LogWrite(const char *Format, ...)
- {
- return 1;
- CHAR Buffer0[1024];
- CHAR Buffer1[512];
- DWORD ItemsWritten;
- va_list ArgumentList;
- // if(logFile == -1)return -1;
- SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- sprintf(Buffer0, "%02d%02d:%02d:%02d:%02d:", sysTime.wMonth, sysTime.wDay, sysTime.wHour,
- sysTime.wMinute, sysTime.wSecond);
- va_start ( ArgumentList, Format );
- ItemsWritten = vsprintf ((char *)Buffer1, Format, ArgumentList );
- va_end ( ArgumentList );
- strcat(Buffer0, Buffer1);
- // return _write ( logFile, Buffer0, strlen((const char *)Buffer0 ));
- }
- void CHKVisionDlg::OnStart()
- {
- StartCaptureVideo( 2 );
- GetDlgItem(IDC_STOPCAP)->EnableWindow(TRUE);
- return;
- // TODO: Add your control notification handler code here
- CSelectDSP selDialog;
- selDialog.DSPPreset(m_bDspPreset, g_nTotalChannel);
- // if(selDialog.DoModal() == IDOK){
- {
- // TRACE("SelectDSP:OK button clicked\n");
- selDialog.GetDSPsChoiced(m_bDspPreset);
- CString str;
- str.Format("%d", 0);
- GetDlgItem(IDC_LOST_FRAMES)->SetWindowText((LPCTSTR)str);
- GetDlgItem(IDC_TOTALFRAMES)->SetWindowText((LPCTSTR)str);
- GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
- // GetDlgItem(IDC_FPS)->SetWindowText((LPCTSTR)str);
- LogWrite("start capture\n");
- for(int i = 0; i < g_nTotalChannel; i++)
- {
- m_bDspPreset[i]=TRUE;
- if(m_bDspPreset[i])
- {
- char fileName[256];
- sprintf(fileName, "d:\\stream%d_%d.264", i, gFileNum++/g_nTotalChannel);
- gFileHandle[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
- if(gFileHandle[i] == -1)
- {
- TRACE("channel %d file open error\n,i");
- return;
- }
- gChannelFrames[i] = 0;
- gChannelTotalLength[i] = 0;
- gChannelFramesLost[i] = 0;
- gChannelOverflow[i] = 0;
- gCurrentFileLen[i] = 0;
- _write(gFileHandle[i], FileHeader[i], FileHeaderLen);
- // could not be start again untill stopped first
- //m_bDspPreset[i] = FALSE;
- gCaptureStartedNum++;
- // let the threads have chance to run
- //Sleep(500);
- }
- else
- gFileHandle[i] = -1;
- }
- StartTime = timeGetTime();
- LastTime = timeGetTime();
- if(gCaptureStartedNum)
- {
- GetDlgItem(IDC_STOPCAP)->EnableWindow(TRUE);
- GetDlgItem(IDC_START)->EnableWindow(FALSE);
- GetDlgItem(IDC_SETTINGS)->EnableWindow(FALSE);
- }
- }
- /*
- StartVideoCapture(ChannelHandle[0]);
- bCapture = TRUE;
- */
- }
- void CHKVisionDlg::LogMessage(const unsigned char *buf, void *context)
- {
- if(!IsWindow(this->m_hWnd))return;
- int len = m_Logwin.GetWindowTextLength();
- CString str;
-
- if (len >= 16384)
- {
- m_Logwin.GetWindowText(str);
- m_Logwin.SetWindowText(str.GetBuffer(len)+16384);
- len -= 16384;
- }
- str = buf;
- m_Logwin.SetSel(len, len);
- m_Logwin.ReplaceSel(str);
- }
- void CHKVisionDlg::OnClose()
- {
- // TODO: Add your message handler code here and/or call default
- Close();
- // _close(logFile);
- CDialog::OnClose();
- }
- void CHKVisionDlg::Close()
- {
- bCapture = FALSE;
- MP4_ServerStop();
- m_bLog = FALSE;
- KillTimer(gTimer);
- if(gCaptureStartedNum)
- OnStop();
- gCaptureStartedNum = 0;
- DeInitDSPs();
- }
- void CHKVisionDlg::OnStop()
- {
- // TODO: Add your control notification handler code here
- StopCaptureVideo( 2 );
- return;
- CSelectDSP selDialog;
- selDialog.DSPPreset(m_bDspPreset, g_nTotalChannel);
- // bCapture = FALSE;
- // if(selDialog.DoModal() == IDOK){
- {
- LogWrite("Stop capture\n");
- // TRACE("SelectDSP:OK button clicked\n");
- selDialog.GetDSPsChoiced(m_bDspPreset);
- for(int i = 0; i < g_nTotalChannel; i++){
- if(m_bDspPreset[i]){
- ASSERT(gFileHandle[i] != -1);
- // StopVideoCapture(ChannelHandle[i]);
- //lseek(gFileHandle[i], 0, SEEK_SET);
- //FRAMES_STATISTICS fs;
- //GetFramesStatistics(ChannelHandle[i], &fs);
- //ULONG frames = fs.AudioFrames + fs.VideoFrames;
- //TRACE("channel %i has %x frames written\n", i, frames);
- #define END_CODE 0x00000002
- ULONG endCode = END_CODE;
- _write(gFileHandle[i], &endCode, sizeof(ULONG));
- _close(gFileHandle[i]);
- ///add v34
- if (bEncodeCifAndQcif[i])
- _close(gFileHandleQcif[i]);
- gCaptureStartedNum--;
- }
- }
- if(!gCaptureStartedNum){
- GetDlgItem(IDC_START)->EnableWindow(TRUE);
- GetDlgItem(IDC_SETTINGS)->EnableWindow(TRUE);
- GetDlgItem(IDC_STOPCAP)->EnableWindow(FALSE);
- }
- }
- }
- void CHKVisionDlg::StartVideoPreview()
- {
- }
- void CHKVisionDlg::StopVideoPreview()
- {
- // TRACE("StopVideoPreview\n");
- for(int i = 0; i < g_nTotalChannel; i++){
- ::StopVideoPreview(ChannelHandle[i]);
- }
- }
- void CHKVisionDlg::OnPreview()
- {
- /*
- // TODO: Add your control notification handler code here
- static BOOLEAN bVideoStarted = FALSE;
- if(bVideoStarted){
- StopVideoPreview();
- bVideoStarted = FALSE;
- }else{
- StartVideoPreview();
- bVideoStarted = TRUE;
- }
- */
- //////以下为演示程序
- char ctemp[256];
- sprintf(ctemp,"192.0.0.214");
- MP4_ServerStringToClient((LPCTSTR)ctemp,"receive",7);
- }
- HBRUSH CHKVisionDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
-
- // TODO: Change any attributes of the DC here
- if((nCtlColor== CTLCOLOR_EDIT) )//&& (pWnd->GetDlgCtrlID() == IDC_EDIT1))
- {
- pDC->SetTextColor (RGB (255, 0 , 0));
- pDC->SetBkColor(gBackgroundColor);
- hbr = (HBRUSH) m_brush1;
- }
- if((nCtlColor== CTLCOLOR_EDIT) )//&& (pWnd->GetDlgCtrlID() == IDC_LOGWIN))
- {
- pDC->SetTextColor (RGB (255, 0 , 0));
- pDC->SetBkColor(RGB(0, 0, 0));
- // pDC->SetBkColor(RGB(255, 0, 255));
- hbr = (HBRUSH) m_brush2;
- }
-
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- void CHKVisionDlg::OnShowWindow(BOOL bShow, UINT nStatus)
- {
- CDialog::OnShowWindow(bShow, nStatus);
-
- // TODO: Add your message handler code here
- // TRACE("Onshowwindow m_hwnd is %x\n", m_hWnd);
- m_bLog = TRUE;
- }
- void CHKVisionDlg::OnMove(int x, int y)
- {
- CDialog::OnMove(x, y);
- if(!dlgInited)return;
-
- // TODO: Add your message handler code here
- //::SendMessage(GetDlgItem(IDC_VIDEOWIN)->m_hWnd, WM_PAINT, 0, 0);
-
- }
- void CHKVisionDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- // TODO: Add your message handler code here and/or call default
-
- CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
- }
- void CHKVisionDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- /* // TODO: Add your message handler code here and/or call default
- int bright, contrast, sat, hue, standard;
-
- if((GetDlgItem(IDC_SLIDER_BRIGHT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
- for(int i = 0; i < g_nTotalChannel; i++){
- GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
- bright = (nPos * 255) / 100;
- / TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
- SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
- }
- }
- if((GetDlgItem(IDC_SLIDER_CONTRAST) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
- for(int i = 0; i < g_nTotalChannel; i++){
- GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
- contrast = (nPos * 127) / 100;
- TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
- SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
- }
- }
- if((GetDlgItem(IDC_SLIDER_SAT) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
- for(int i = 0; i < g_nTotalChannel; i++){
- GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
- sat = (nPos * 127) / 100;
- TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
- SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
- }
- }
- if((GetDlgItem(IDC_SLIDER_HUE) == pScrollBar) && (nSBCode != SB_ENDSCROLL)){
- for(int i = 0; i < g_nTotalChannel; i++){
- GetVideoPara(ChannelHandle[i], &standard, &bright, &contrast, &sat, &hue);
- hue = (nPos * 255) / 100;
- TRACE("b=%i c=%i s=%i h=%i\n", bright, contrast, sat, hue);
- SetVideoPara(ChannelHandle[i], bright, contrast, sat, hue);
- }
- }
- */
- CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
- }
- int CHKVisionDlg::OnUpdateCounters(UINT wparam, INT lparam)
- {
- CString str;
- str.Format("%i", wparam);
- //GetDlgItem(IDC_IFRAMES)->SetWindowText((LPCTSTR)str);
-
- return 0;
- }
- void CHKVisionDlg::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CDialog::OnLButtonDown(nFlags, point);
- }
- int CHKVisionDlg::OnDataReady(UINT channelNum, INT lparam)
- {
- ULONG length = 1000000;
- int frameType;
- int status,i;
- BOOL breakable;
- CString ctip;
- int nframetype =0;
- // if cap images we need clean the queue here
- // if (!bCapture)
- // return 0;
-
- status = ReadStreamData(ChannelHandle[channelNum], StreamBuf, &length, &frameType);
- // error occured
- if(status < 0){
- TRACE("error:ReadStreamData %x\n", status);
- return 0;
- }
- ///add v34
- // nframetype value: 1 cif head; 2 qcif head; 3 cif frame; 4 qcif frame; 5 audio frame; 0 others
- //在CIF,QICF同时编码时,要对子通道的数据流分别处理
- // CIF的数据存入CIF文件中,QCIF的数据存入QCIF文件中,音频数据要分别存储
- // if (bEncodeCifAndQcif[channelNum])
- // nframetype = GetSubChannelStreamType(&StreamBuf,frameType);
- breakable = status;
- // no errors
- if(frameType > 0) {
- if(frameType == PktSysHeader){
- if (bEncodeCifAndQcif[channelNum])
- {
- if (nframetype == 1) //get cif file header
- {
- memcpy(FileHeader[channelNum],StreamBuf,length);
- FileHeaderLen = length;
- }
- else if (nframetype ==2) //get qcif or qqcif file header
- {
- memcpy(FileHeaderQcif[channelNum], StreamBuf, length);
- TRACE("Ch%02d get Qcifhead or QQcifhead!\n",channelNum);
- FileHeaderLen = length;
- }
- }
- else{
- TRACE("channel %d get the file header !\n",channelNum);
- // store the file header
- memcpy(FileHeader[channelNum], StreamBuf, length);
- FileHeaderLen = length;
- }
- }
- if(frameType == PktMotionDetection)
- {
- m_VideoWin.DrawVect(channelNum, (char *)StreamBuf, length);
- return 0;
- }
- if(frameType == PktOrigImage){
- return 0;
- }
- }
- if(length == 0){
- TRACE("no data ?\n");
- return 0;
- }
- if(frameType == PktIFrames){
- int iii=1;
- }
- ULONG currentTime = timeGetTime();
- gChannelTotalLength[channelNum] += length;
- gCurrentFileLen[channelNum] += length;
- if(currentTime > StartTime+1000){
- CString str,str2;
- str.Format("%d", (gChannelTotalLength[dcurrentwin] *8/(currentTime - StartTime)));
- for(i=0;i<g_nTotalChannel;i++)
- gChannelTotalLength[i] = 0;
- StartTime= currentTime;
- GetDlgItem(IDC_BPS)->SetWindowText((LPCTSTR)str);
- }
- /*
- if((gCurrentFileLen[wparam] > gFileSize * 1000 * 1000) && breakable){
- TRACE("switch file:frametype=%x\n", frameType);
- _close(gFileHandle[wparam]);
- // create new file
- char fileName[256];
- sprintf(fileName, "stream%d_%d.mp4", wparam, gFileNum++/g_nTotalChannel);
- gFileHandle[wparam] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
- // write the header first
- _write(gFileHandle[wparam], FileHeader, FileHeaderLen);
- gCurrentFileLen[wparam] -= gFileSize * 1000 * 1000;
- }
- */
- if(frameType ==PktAudioFrames)
- {
- _write(gFileHandleQcif[channelNum],StreamBuf,length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,1);
- _write(gFileHandle[channelNum], StreamBuf, length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,0);
-
- }else if (frameType ==PktSubIFrames || frameType ==PktSubPFrames || frameType == PktSubBBPFrames || frameType == PktSubSysHeader)
- {
- _write(gFileHandleQcif[channelNum],StreamBuf,length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,1);
- }else
- {
- _write(gFileHandle[channelNum], StreamBuf, length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)StreamBuf, length,frameType,status,0);
- }
- return 0;
- }
- void CHKVisionDlg::OnSettings()
- {
- // TODO: Add your control notification handler code here
- int i;
- CSettingsDlg dlg;
- int nResponse = dlg.DoModal();
- if (nResponse == IDOK){
- // TRACE("quant = %d file size = %d streamType = %d\n",
- // dlg.m_Quant, dlg.m_FileSize , dlg.m_StreamType);
- // setup quant value
- USHORT Format1[40] = {48, 16, 'H','I','K','V', 'I','S','I','O','N', '\0'};
- USHORT Format2[40] = {24, 50, _OSD_YEAR4, '-',_OSD_MONTH2,'-',_OSD_DAY,'-', _OSD_HOUR24, ':', _OSD_MINUTE,':', _OSD_SECOND, '\0'};
- PictureFormat_t encodeType ;
- for( i = 0; i < g_nTotalChannel; i++){
- StopVideoCapture(ChannelHandle[i]);
- }
-
- switch(dlg.m_iEncodeType)
- {
- case 0:
- encodeType = ENC_4CIF_FORMAT;
- break;
- case 1:
- encodeType = ENC_2CIF_FORMAT;
- break;
- case 2:
- encodeType = ENC_DCIF_FORMAT;
- break;
- case 3:
- encodeType = ENC_CIF_FORMAT;
- break;
- case 4:
- encodeType = ENC_QCIF_FORMAT;
- break;
- default:
- encodeType = ENC_CIF_FORMAT;
- break;
- }
- for(i = 0; i < g_nTotalChannel; i++)
- {
- if(encodeType != ENC_4CIF_FORMAT)
- {
- SetEncoderPictureFormat(ChannelHandle[i],encodeType);
- }
- else if ((i%4==0)||(i%4==1))
- {
- SetEncoderPictureFormat(ChannelHandle[i],encodeType);
- }
- }
- for(i = 0; i < g_nTotalChannel; i++){
- SetBitrateControlMode(ChannelHandle[i], dlg.m_bCbr ?brCBR : brVBR );
- }
- for( i = 0; i < g_nTotalChannel; i++)
- {
- if (!dlg.m_bCbr)
- {
- SetDefaultQuant(ChannelHandle[i], dlg.m_iQuant, dlg.m_pQuant, dlg.m_bQuant);
- SetupBitrateControl(ChannelHandle[i],dlg.m_dwbitrate);
- }
- else
- SetupBitrateControl(ChannelHandle[i],dlg.m_dwbitrate);
- }
- if(dlg.m_OsdEnable){
- for(i = 0; i < g_nTotalChannel; i++){
- SetOsdDisplayMode(ChannelHandle[i], 255, TRUE, 0, Format1, Format2);
- SetOsd(ChannelHandle[i], TRUE);
- }
- }else {
- for(i = 0; i < g_nTotalChannel; i++){
- SetOsd(ChannelHandle[i], FALSE);
- }
- }
- if(dlg.m_IFramesIntervals < 12){
- AfxMessageBox("Invalid Key Frames Invervals\n");
- return;
- }else{
- if((dlg.m_FrameRate > 25) || (dlg.m_FrameRate <=0))
- dlg.m_FrameRate = 25;
- for( i = 0; i < g_nTotalChannel; i++){
- SetIBPMode(ChannelHandle[i], dlg.m_IFramesIntervals, 2, 1, dlg.m_FrameRate);
- }
- }
- if(dlg.m_MotionDetect){
- for( i = 0; i < g_nTotalChannel; i++){
- StartMotionDetection(ChannelHandle[i]);
- }
- }else{
- for( i = 0; i < g_nTotalChannel; i++){
- StopMotionDetection(ChannelHandle[i]);
- }
- }
- USHORT streamType = STREAM_TYPE_AVSYNC;
- switch(dlg.m_StreamType){
- case 0:
- streamType = STREAM_TYPE_VIDEO;
- break;
- case 1:
- streamType = STREAM_TYPE_AUDIO;
- break;
- case 2:
- streamType = STREAM_TYPE_AVSYNC;
- break;
- }
- for(i = 0; i < g_nTotalChannel; i++){
- SetStreamType(ChannelHandle[i], streamType);
- }
- if(dlg.m_logoEnable){
- int w,h;
- unsigned char *bitsBuffer = (unsigned char *)malloc(128 * 128 * 2);
- if(bitsBuffer == NULL)
- return;
- LoadYUVFromBmpFile("logo.bmp", bitsBuffer, 128 * 128 * 2, &w, &h);
- for( i = 0; i < g_nTotalChannel; i++){
- SetLogo(ChannelHandle[i], 0, 110, 128, 128, (UCHAR *)bitsBuffer);
- SetLogoDisplayMode(ChannelHandle[i], RGB(0,0,0),TRUE, 0);
- }
- free(bitsBuffer);
- }else{
- for( i = 0; i < g_nTotalChannel; i++){
- StopLogo(ChannelHandle[i]);
- }
- }
- gFileSize = dlg.m_FileSize;
- for( i = 0; i < g_nTotalChannel; i++){
-
- if(encodeType != ENC_4CIF_FORMAT)
- {
- StartVideoCapture(ChannelHandle[i]);
- }
- else if ((i%4==0)||(i%4==1))
- {
- StartVideoCapture(ChannelHandle[i]);
- }
-
- }
-
- }
- }
- void CHKVisionDlg::OnTimer(UINT nIDEvent)
- {
- // TODO: Add your message handler code here and/or call default
- if (nIDEvent == 1)
- {
- for(int i = 0; i < g_nTotalChannel; i++){
- if(!GetVideoSignal(ChannelHandle[i])){
- CheckDlgButton(IDC_CHECK2 + i, BST_CHECKED);
- }else{
- // TRACE("Channel%d Video lost !\n", i);
- CheckDlgButton(IDC_CHECK2 + i, BST_UNCHECKED);
- }
- }
- }
- else if (nIDEvent == 2)
- {
- CString str;
- WORD num;
- MP4_ServerGetState(&num);
- str.Format("%d",num);
- GetDlgItem(IDC_FPS)->SetWindowText((LPCTSTR)str);
- }
- else if (nIDEvent == 3)//判断没移动多久了
- {
- DWORD dwTicks = GetTickCount();
- for( int i=0;i<64;i++ )
- {
- int a = ( dwTicks-m_dwNoMotionStartTime[i])/1000;
- if( m_bCapture[i] && a > g_NoMotionTime( g_nNoMotionTime ) )
- {
- StopCaptureVideo( i );
- }
- }
- }
- else if (nIDEvent == 10)
- {
- CaptureIFrame(ChannelHandle[0]);
- }
- //CPLAYER_API STATISTIC* __stdcall MP4_ServerGetStatistic(DWORD nChannel,int *returnnum);
- CDialog::OnTimer(nIDEvent);
- }
- void CHKVisionDlg::OnCapimage()
- {
- // TODO: Add your control notification handler code here
- }
- void CHKVisionDlg::OnDestroy()
- {
- if (usframe)
- KillTimer(usframe);
- CDialog::OnDestroy();
- }
- void CHKVisionDlg::OnMyCommand(WPARAM wParam, LPARAM lParam)
- {
- if ((BYTE)wParam == GETSTRING)
- {
- char ts[1000];
- char ips[30];
- WORD tlen;
- memset(ts,0,1000);
- MP4_ServerReadLastMessage(ips,ts,&tlen);
- sprintf(&(ts[tlen]),"\r\n");
- AfxMessageBox(ts);
- // TRACE(ts);
- }
- }
- void CHKVisionDlg::OnButton5()
- {
- MP4_ServerCommandToClient(20,0);
-
- }
- void CHKVisionDlg::OnButton6()
- {
- char sendcont[500];
- CString firstco;
- firstco = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
- int testnum=0;
- MP4_ServerSetWait(3000,5);
- UpdateData(TRUE);
- // for(int i=0;i<100;i++)
- {
- memset(sendcont,0,500);
- UpdateData(TRUE);
- sprintf(sendcont,"%s send content %d",firstco,testnum);
- testnum++;
- if (testnum > 200)
- testnum=0;
- MP4_ServerStringToClient(m_ip,sendcont,400);
- Sleep(100);
- }
-
- }
- void CHKVisionDlg::OnButton7()
- {
- MP4_ServerResetChannel(1);
- }
- void CHKVisionDlg::OnButton8()
- {
- MP4_ServerStringToClient_other(0,"123456789",10);
-
- }
- void CHKVisionDlg::OnCapturesframe()
- {
- UpdateData(TRUE);
- if (m_sframe)
- {
- usframe = SetTimer(10,1000,NULL);
- }
- else
- {
- if (usframe)
- KillTimer(usframe);
- }
-
- }
- void CHKVisionDlg::OnButton9()
- {
- if (chantype)
- {
- for(int i = 0; i < g_nTotalChannel; i++)
- {
- StopVideoCapture(ChannelHandle[i]);
- MP4_ServerChangeChanType(i,NORMAL);
- SetIBPMode(ChannelHandle[i],211,2,2,25);
- SetDefaultQuant(ChannelHandle[i],15,15,20);
- SetEncoderPictureFormat(ChannelHandle[i], ENC_CIF_FORMAT);
- StartVideoCapture(ChannelHandle[i]);
- }
- chantype = FALSE;
- }
- else
- {
- for(int i = 0; i < g_nTotalChannel; i++)
- {
- StopVideoCapture(ChannelHandle[i]);
- MP4_ServerChangeChanType(i,DIALING);
- SetIBPMode(ChannelHandle[i],211,2,2,8);
- SetDefaultQuant(ChannelHandle[i],18,18,23);
- SetStreamType(ChannelHandle[i],STREAM_TYPE_VIDEO);
- SetEncoderPictureFormat(ChannelHandle[i], ENC_QCIF_FORMAT);
- StartVideoCapture(ChannelHandle[i]);
- }
- chantype = TRUE;
- }
- }
- //typedef int (*STREAM_DIRECT_READ_CALLBACK)(ULONG channelNumber,void *DataBuf,DWORD Length,int FrameType,void *context);
- int __cdecl StreamDirectReadCallback(ULONG channelNum,void *DataBuf,DWORD Length,int frameType,void *context)
- {
- //判断硬盘空间
- CHKVisionDlg* pdlg = g_pDlgMain->m_pMonitor;
- if( !g_DiskVelumnIsEnough( g_nDiskVolume,g_strVideoPath ) )
- {
- if( g_nOperateType==1 )
- {
- for( int i=0;i<64;i++ )
- {
- if( pdlg->m_bCapture[i] )
- {
- pdlg->StopCaptureVideo( i );
- }
- }
- return false;
- }
- else if( g_nOperateType==0 )
- {
- CString str;
- for( int i=0;i<5;i++ )
- {
- if( g_strAllVideoFile.GetCount()>0 )
- {
- str = g_strAllVideoFile[0];
- DeleteFile( str );
- g_strAllVideoFile.RemoveAt( 0 );
- }
- }
- }
- }
- //判断视频时间
- DWORD dwTicks = GetTickCount();
- int nTime = (dwTicks-pdlg->m_dwVideoStartTime[channelNum])/1000;
- if( pdlg->m_bCapture[channelNum] && nTime > g_VideoMaxTime( g_nVideoMaxTime ) )
- {
- pdlg->StopCaptureVideo( channelNum );
- }
- int status=0;
- CString ctip;
- int nframetype =0;
- // no errors
- if(frameType > 0)
- {
- if(frameType == PktSysHeader)
- {
- // store the file header
- memcpy(FileHeader[channelNum], DataBuf, Length);
- FileHeaderLen = Length;
- TRACE("channel %d get the file header !\n",channelNum);
- }
- if(frameType == PktIFrames || frameType ==PktSubIFrames)
- {
- status = 1;
- }
- else
- {
- status = 0;
- }
-
- if(frameType == PktMotionDetection)
- {
- int result[4];
- //if(g_bOverlayMode)
- //{
- // pdlg->m_VideoWin.DrawVectEx(channelNum, (char *)DataBuf);
- //}
- //else
- //{
- // pdlg->m_bMoving[channelNum] = TRUE;
- // pdlg(motionData[channelNum],(char*)DataBuf,Length);
- //}
- MotionAnalyzer(ChannelHandle[channelNum],(char*)DataBuf,15,result);
- if((result[0] + result[1] + result[2] + result[3]))
- {
- if( !pdlg->m_bCapture[channelNum] )
- pdlg->StartCaptureVideo( channelNum );
-
- pdlg->m_dwNoMotionStartTime[channelNum] = GetTickCount();
- //if (channelNum ==0)
- // TRACE("!!!!!!!!!!!!!!!!CH=%d motion detect result is %d %d %d %d\n",channelNum,result[0],\
- // result[1],result[2],result[3]);
- //// Trigger函数的作用是把报警前的数据按setup里面的设置,把相应的警前帧写入文件
- //if(!pdlg->baftermotion[channelNum])
- // //alarmFile[channelNum].Trigger(gFileHandle[channelNum]);
- ////每次有移动侦测帧上来,自动把ualreadywriteframes[channelNum]清为0 ,为写入警后帧作准备
- //pdlg->ualreadywriteframes[channelNum]=0;
- //pdlg->baftermotion[channelNum]=TRUE;
- }
- return 0;
- }
- if(frameType == PktOrigImage)
- {
- return 0;
- }
- }
- if(Length == 0)
- {
- TRACE("no data ?\n");
- return 0;
- }
- ULONG currentTime = timeGetTime();
- gChannelTotalLength[channelNum] += Length;
- gCurrentFileLen[channelNum] += Length;
- if(currentTime > StartTime+1000)
- {
- CString str,str2;
- str.Format("%d", (gChannelTotalLength[dcurrentwin] *8/(currentTime - StartTime)));
- for(int i=0;i<g_nTotalChannel;i++)
- gChannelTotalLength[i] = 0;
- StartTime= currentTime;
- }
-
- if(frameType ==PktAudioFrames)
- {
- _write(gFileHandleQcif[channelNum],DataBuf,Length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,1);
- _write(gFileHandle[channelNum], DataBuf, Length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,0);
-
- }
- else if (frameType ==PktSubIFrames || frameType ==PktSubPFrames || frameType == PktSubBBPFrames || frameType == PktSubSysHeader)
- {
-
- _write(gFileHandleQcif[channelNum],DataBuf,Length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,1);
- }
- else
- {
- _write(gFileHandle[channelNum], DataBuf, Length);
- MP4_ServerWriteDataEx(channelNum,(unsigned char *)DataBuf, Length,frameType,status,0);
- }
- return 0;
- }
- void CHKVisionDlg::OnSubstart()
- {
- // TODO: Add your control notification handler code here
- for(int i=0; i < GetTotalChannels(); i++)
- {
- char fileName[256];
- sprintf(fileName,"d:\\streamqcif%d_%d.264",i,gFileNum++);
- gFileHandleQcif[i]= _open(fileName,_O_CREAT|_O_BINARY|_O_WRONLY|_O_TRUNC,_S_IREAD|_S_IWRITE);
- if(gFileHandleQcif[i] ==-1)
- {
- TRACE("file Open error!\n");
- AfxMessageBox("can not open file!\n");
- return ;
- }
- SetSubStreamType(ChannelHandle[i],STREAM_TYPE_AVSYNC);
- SetSubEncoderPictureFormat(ChannelHandle[i],ENC_CIF_FORMAT);
- SetupSubChannel(ChannelHandle[i],1);// switch to sub channel for setting
- SetBitrateControlMode(ChannelHandle[i], brVBR );
- SetupBitrateControl(ChannelHandle[i],2000000);
- SetDefaultQuant(ChannelHandle[i], 16, 16, 18);
- SetIBPMode(ChannelHandle[i],100,2,2,25);
- StartSubVideoCapture(ChannelHandle[i]);
- SetupSubChannel(ChannelHandle[i],0);//switch to main channel
- }
- GetDlgItem(IDC_SUBSTART)->EnableWindow(FALSE);
- GetDlgItem(IDC_STOPSUBCAP)->EnableWindow(TRUE);
- }
- void CHKVisionDlg::OnStopsubcap()
- {
- // TODO: Add your control notification handler code here
- for(int i=0; i < GetTotalChannels(); i++)
- {
- StopSubVideoCapture(ChannelHandle[i]);
- }
- GetDlgItem(IDC_SUBSTART)->EnableWindow(TRUE);
- GetDlgItem(IDC_STOPSUBCAP)->EnableWindow(FALSE);
- }
- bool CHKVisionDlg::StartCaptureVideo( int iChannel )
- {
- if( 0 != GetVideoSignal( (HANDLE)iChannel ) )
- {
- return false;
- }
- if( !g_DiskVelumnIsEnough( g_nDiskVolume,g_strVideoPath ) )
- {
- if( g_nOperateType==1 )
- return false;
- else if( g_nOperateType==0 )
- {
- CString str;
- for( int i=0;i<5;i++ )
- {
- if( g_strAllVideoFile.GetCount()>0 )
- {
- str = g_strAllVideoFile[0];
- DeleteFile( str );
- g_strAllVideoFile.RemoveAt( 0 );
- }
- }
- }
- }
- bool bRet=true;
- int i = iChannel;
- if( !m_bCapture[i] )
- {
- char fileName[256];
- SYSTEMTIME st;
- GetLocalTime( &st );
- 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 );
- gFileHandle[i] = _open(fileName, _O_CREAT | _O_BINARY | _O_WRONLY| _O_TRUNC, _S_IREAD | _S_IWRITE);
- if(gFileHandle[i] == -1)
- {
- TRACE("channel %d file open error\n,i");
- return false;
- }
- gChannelFrames[i] = 0;
- gChannelTotalLength[i] = 0;
- gChannelFramesLost[i] = 0;
- gChannelOverflow[i] = 0;
- gCurrentFileLen[i] = 0;
- _write(gFileHandle[i], FileHeader[i], FileHeaderLen);
- m_bCapture[i] = true;
- m_dwVideoStartTime[i] = GetTickCount();
- StartVideoCapture(ChannelHandle[i]);
-
- gCaptureStartedNum++;
- }
- return bRet;
- }
-
- bool CHKVisionDlg::StopCaptureVideo( int iChannel )
- {
- bool bRet=true;
- int i = iChannel;
- if( m_bCapture[i])
- {
- ASSERT(gFileHandle[i] != -1);
- m_bCapture[i] = false;
- m_dwVideoStartTime[i] = GetTickCount();
- m_dwNoMotionStartTime[i] = GetTickCount();
- StopVideoCapture(ChannelHandle[i]);
- #define END_CODE 0x00000002
- ULONG endCode = END_CODE;
- _write(gFileHandle[i], &endCode, sizeof(ULONG));
- _close(gFileHandle[i]);
- ///add v34
- if (bEncodeCifAndQcif[i])
- _close(gFileHandleQcif[i]);
- gCaptureStartedNum--;
- }
- return bRet;
- }
- void CHKVisionDlg::GetAllVideoFile()
- {
- DWORD returnValue;
- double dBegin=0.0,dEnd=0.0,dNow=0.0,dTemp=0.0;
-
- g_strAllVideoFile.RemoveAll();
- CString sPath,str;
- if( SetCurrentDirectory(g_strVideoPath) == TRUE)
- {
- CFileFind finder;
- BOOL bWorking = finder.FindFile("*.264");
- while (bWorking)
- {
- bWorking = finder.FindNextFile();//1_2010-10-17-101010.264
- sPath = finder.GetFileName();
- g_strAllVideoFile.Add( sPath );
- }
- CString str1,str2,s1,s2,sTemp;
- for( int i=0;i<g_strAllVideoFile.GetCount();i++ )
- {
- str1 = g_strAllVideoFile[i];
- s1 = str1.Mid( 2,17 );
- s1.Delete(10);
- s1.Delete(7);
- s1.Delete(4);
- dBegin = atof(s1);
- for( int j=i+1;j<g_strAllVideoFile.GetCount();j++ )
- {
- str2 = g_strAllVideoFile[j];
- s2 = str2.Mid( 2,17 );
- s2.Delete(10);
- s2.Delete(7);
- s2.Delete(4);
- dEnd = atof(s2);
-
- if( dEnd<dBegin )
- {
- dTemp = dBegin;
- dBegin = dEnd;
- dEnd = dTemp;
- sTemp = str1;
- str1 = str2;
- str2 = sTemp;
- g_strAllVideoFile[i] = str1;
- g_strAllVideoFile[j] = str2;
- }
- }
- }
- }
- else
- {
- returnValue = GetLastError();
- }
- }
- LRESULT CHKVisionDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: 在此添加专用代码和/或调用基类
- switch( message )
- {
- case WM_SHOW:
- RegisterDrawFun(int(lParam), DrawFun ,(DWORD)this);//注册画的回调
- break;
- }
- return CDialog::WindowProc(message, wParam, lParam);
- }
- void CHKVisionDlg::StopDrawFun(DWORD nport)
- {
- StopRegisterDrawFun(nport);
- }
|