// DemoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "SingleDisplay.h"
#include "SingleDisplayDlg.h"
#include "RGBConvert.h"
#include "SingleDisplayDlg.h"
#include <sstream>
#include <io.h>
#include <fcntl.h>
#include <afx.h>
#import "msxml3.dll"
using namespace MSXML2;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define DEFAULT_SHOW_RATE (30)
#define FRAME_DISPLAY_INTERVAL30 33333333
using namespace Dahua;
using namespace Dahua::Infra;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CSingleDisplayDlg 对话框
CSingleDisplayDlg::CSingleDisplayDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSingleDisplayDlg::IDD, pParent)
, _connected(false)
, _isGrabbing(false)
, _bRunning(false)
, _frameCnt(0)
, _frameCtrl(false)
, _maxFrameCnt(0)
, _devListDlg(NULL)
, _displayThread(CThreadLite::ThreadProc(&CSingleDisplayDlg::DisplayThreadProc, this), "Display")
// , _streamRetrieveThread(CThreadLite::ThreadProc(&CSingleDisplayDlg::StreamRetrieveThreadPro, this), "StreamRetrieve")
, m_dDisplayInterval(0)
, m_nFirstFrameTime(0)
, m_nLastFrameTime(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
setDisplayFPS(30); // 默认显示30帧
}
void CSingleDisplayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
_render.setHandle(GetDlgItem(IDC_DISPLAY)->GetSafeHwnd());
}
BOOL CSingleDisplayDlg::OpenConsole()
{
AllocConsole();
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle(static_cast<long>(reinterpret_cast<uintptr_t>(handle)), _O_TEXT);
FILE* hf = _fdopen(hCrt, "w");
*stdout = *hf;
return TRUE;
}
BEGIN_MESSAGE_MAP(CSingleDisplayDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BTN_CONNECT, &CSingleDisplayDlg::OnBnClickedConnect)
ON_BN_CLICKED(IDC_BTN_PLAY, &CSingleDisplayDlg::OnBnClickedBtnPlay)
ON_WM_CLOSE()
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_FRAMERATE, &CSingleDisplayDlg::OnNMCustomdrawSliderFramerate)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_EXPOSURETIME, &CSingleDisplayDlg::OnNMCustomdrawSliderExposuretime)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_GAIN, &CSingleDisplayDlg::OnNMCustomdrawSliderGain)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_GAMA, &CSingleDisplayDlg::OnNMCustomdrawSliderGama)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_LIGHT, &CSingleDisplayDlg::OnNMCustomdrawSliderLight)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_SHARPNESS, &CSingleDisplayDlg::OnNMCustomdrawSliderSharpness)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_HUE, &CSingleDisplayDlg::OnNMCustomdrawSliderHue)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_SATURATION, &CSingleDisplayDlg::OnNMCustomdrawSliderSaturation)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_RWHITEBALANCE, &CSingleDisplayDlg::OnNMCustomdrawSliderRwhitebalance)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_GWHITEBALANCE, &CSingleDisplayDlg::OnNMCustomdrawSliderGwhitebalance)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_BWHITEBALANCE, &CSingleDisplayDlg::OnNMCustomdrawSliderBwhitebalance)
ON_CBN_SELCHANGE(IDC_COMBO_FORMAT, &CSingleDisplayDlg::OnCbnSelchangeComboFormat)
ON_CBN_SELCHANGE(IDC_COMBO_AUTOEXPOSURE, &CSingleDisplayDlg::OnCbnSelchangeComboAutoexposure)
ON_CBN_SELCHANGE(IDC_COMBO_EXPOSUREMODE, &CSingleDisplayDlg::OnCbnSelchangeComboExposuremode)
ON_CBN_SELCHANGE(IDC_COMBO_AUTOGAIN, &CSingleDisplayDlg::OnCbnSelchangeComboAutogain)
ON_CBN_SELCHANGE(IDC_COMBO_AUTOWHITEBALANCE, &CSingleDisplayDlg::OnCbnSelchangeComboAutowhitebalance)
ON_CBN_SELCHANGE(IDC_COMBO_PLAYMODE, &CSingleDisplayDlg::OnCbnSelchangeComboPlaymode)
ON_BN_CLICKED(IDC_BTN_HORITRANS, &CSingleDisplayDlg::OnBnClickedBtnHoritrans)
ON_BN_CLICKED(IDC_BTN_VERTTRANS, &CSingleDisplayDlg::OnBnClickedBtnVerttrans)
ON_BN_CLICKED(IDC_BTN_SOFTTRIGGER, &CSingleDisplayDlg::OnBnClickedBtnSofttrigger)
ON_CBN_SELCHANGE(IDC_COMBO_TRIGGERMODE, &CSingleDisplayDlg::OnCbnSelchangeComboTriggermode)
ON_BN_CLICKED(IDC_BTN_SAVECFG, &CSingleDisplayDlg::OnBnClickedBtnSavecfg)
ON_BN_CLICKED(IDC_BTN_OPENCFG, &CSingleDisplayDlg::OnBnClickedBtnOpencfg)
ON_BN_CLICKED(IDC_BTN_SAVEPIC, &CSingleDisplayDlg::OnBnClickedBtnSavepic)
END_MESSAGE_MAP()
bool printCallBack(char const* p)
{
std::cout << p << std::endl;
return true;
}
// CSingleDisplayDlg 消息处理程序
BOOL CSingleDisplayDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
::CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
//Dahua::Infra::attachPrint(Dahua::Infra::PrintProc(&printCallBack));
//Dahua::Infra::setPrintLogLevel(1);
// TODO: 在此添加额外的初始化代码
EnableBtns(false);
// 启动显示线程
if (!_displayThread.isThreadOver())
{
_displayThread.destroyThread();
}
if (!_displayThread.createThread())
{
MessageBox("Create Display Thread Failed.", "", 0);
return FALSE;
}
/// 初始化属性控件
InitAttributes();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 使能按钮
void CSingleDisplayDlg::EnableBtns(bool enable)
{
GetDlgItem(IDC_BTN_PLAY)->EnableWindow(enable);
GetDlgItem(IDC_BTN_SOFTTRIGGER)->EnableWindow(enable);
GetDlgItem(IDC_BTN_HORITRANS)->EnableWindow(enable);
GetDlgItem(IDC_BTN_VERTTRANS)->EnableWindow(enable);
GetDlgItem(IDC_BTN_SAVEPIC)->EnableWindow(enable);
}
void CSingleDisplayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 显示线程
void CSingleDisplayDlg::DisplayThreadProc(Dahua::Infra::CThreadLite& lite)
{
while ( lite.looping() )
{
if(!_bRunning)
{
Sleep(10);
continue;
}
if(m_bStopGrabbing)
{
StopStreamGrabbing();
}
FrameBufferSPtr displayFrame = getConvertedImage();
if (!displayFrame )
{
continue;
}
_render.display(displayFrame->bufPtr(), displayFrame->Width(), displayFrame->Height(), displayFrame->PixelFormat());
}
}
bool CSingleDisplayDlg::isTimeToDisplay(uintmax_t nCurTime)
{
Infra::CGuard guard(m_mxTime);
// 不显示
if (m_dDisplayInterval <= 0)
{
return false;
}
// 第一帧必须显示
if (m_nFirstFrameTime == 0 || m_nLastFrameTime == 0)
{
m_nFirstFrameTime = Infra::CTime::getCurrentMicroSecond() * 1000;;
m_nLastFrameTime = m_nFirstFrameTime;
TRACE("set m_nFirstFrameTime: %I64d\n", m_nFirstFrameTime);
return true;
}
uint64_t nCurTimeTmp = Infra::CTime::getCurrentMicroSecond() * 1000;
uint64_t nAcquisitionInterval = nCurTimeTmp - m_nLastFrameTime;
if (nAcquisitionInterval > FRAME_DISPLAY_INTERVAL30)
{
m_nLastFrameTime = nCurTimeTmp;
return true;
}
// 当前帧相对于第一帧的时间间隔
uint64_t nPre = (m_nLastFrameTime - m_nFirstFrameTime) % FRAME_DISPLAY_INTERVAL30;
if (nPre + nAcquisitionInterval > FRAME_DISPLAY_INTERVAL30)
{
m_nLastFrameTime = nCurTimeTmp;
return true;
}
re