// CodeParamDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "NetSDKDLL.h"
#include "NetSDKDemoDlg.h"
#include "CodeParamDlg.h"


// CCodeParamDlg 对话框

IMPLEMENT_DYNAMIC(CCodeParamDlg, CDialog)

CCodeParamDlg::CCodeParamDlg(CRect rcWnd, CWnd* pParent /*=NULL*/)
	: CDialog(CCodeParamDlg::IDD, pParent)
	,m_rcClient(0, 0, rcWnd.Width(), rcWnd.Height())
{

}

CCodeParamDlg::~CCodeParamDlg()
{
	DestroyWindow();
}

void CCodeParamDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_GROUP, m_ctlGroup);
}


BEGIN_MESSAGE_MAP(CCodeParamDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_GETVIDEOPARAM, &CCodeParamDlg::OnBnClickedBtnGetvideoparam)
	ON_BN_CLICKED(IDC_BTN_GETAUDIOPARAM, &CCodeParamDlg::OnBnClickedBtnGetaudioparam)
END_MESSAGE_MAP()


// CCodeParamDlg 消息处理程序



BOOL CCodeParamDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	
	if (pMsg->message == WM_KEYDOWN)
	{
		if (pMsg->wParam == 13)
			return TRUE;
 		if (pMsg->wParam == 27)
		{
			::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN, 27, 0);
			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CCodeParamDlg::OnBnClickedBtnGetvideoparam()
{
	// TODO: 在此添加控件通知处理程序代码
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();
	CString strKey = pNetSDKDemoDlg->GetWndUser(hWnd);

	long lRealHandle = 0;
	{
		CMapAutoLock Lock;
		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
		if (bFound)
		{
			lRealHandle = pDevInfo->lRealHandle;
		}
	}

	if (lRealHandle == 0)
	{
		AfxMessageBox("请选择正在播放视频的窗口!!!");
		return ;
	}

	VIDEO_PARAM vp;
	memset(&vp, 0x00, sizeof(VIDEO_PARAM));
	long lRet = IP_NET_DVR_GetVideoParam(lRealHandle, &vp);

	if (lRet != 0)
	{
		AfxMessageBox("没有找到媒体流句柄");
	}
	else
	{
		if (vp.width <= 0)
		{
			CString strText = _T("0");
			
			GetDlgItem(IDC_ED_VCODE)->SetWindowText(strText);
			GetDlgItem(IDC_ED_WIDTH)->SetWindowText(strText);
			GetDlgItem(IDC_ED_HEIGHT)->SetWindowText(strText);
			GetDlgItem(IDC_ED_FRAMERATE)->SetWindowText(strText);
			GetDlgItem(IDC_ED_VBITRATE)->SetWindowText(strText);
		}
		else
		{
			CString strText;
			strText.Format("%s", vp.codec);
			GetDlgItem(IDC_ED_VCODE)->SetWindowText(strText);
			strText.Format("%d", vp.width);
			GetDlgItem(IDC_ED_WIDTH)->SetWindowText(strText);
			strText.Format("%d", vp.height);
			GetDlgItem(IDC_ED_HEIGHT)->SetWindowText(strText);
			strText.Format("%d", vp.framerate);
			GetDlgItem(IDC_ED_FRAMERATE)->SetWindowText(strText);
			strText.Format("%d", vp.bitrate);
			GetDlgItem(IDC_ED_VBITRATE)->SetWindowText(strText);
		}
	}

}
void CCodeParamDlg::OnBnClickedBtnGetaudioparam()
{
	// TODO: 在此添加控件通知处理程序代码
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();
	CString StrKey = pNetSDKDemoDlg->GetWndUser(hWnd);

	long lRealHandle = 0;
	{
		CMapAutoLock Lock;

		CDevInfo *pDevInfo = NULL;
		BOOL bFound = theApp.m_DevList.Lookup(StrKey, pDevInfo);
		if (bFound)
		{
			lRealHandle = pDevInfo->lRealHandle;
		}
		
	}
	
	if (lRealHandle == 0)
	{
		AfxMessageBox("请选择正在播放视屏的窗口!!!");
		return ;
	}

	AUDIO_PARAM ap;
	memset(&ap, 0x00, sizeof(AUDIO_PARAM));
	long lRet = IP_NET_DVR_GetAudioParam(lRealHandle, &ap);
	if (lRet != 0)
	{
		AfxMessageBox("没有找到媒体流句柄");
	}
	else
	{
		if (ap.channels <= 0)
		{
			CString strText= _T("0");

			GetDlgItem(IDC_ED_ACODE)->SetWindowText(strText);
			GetDlgItem(IDC_ED_CHANNEL)->SetWindowText(strText);
			GetDlgItem(IDC_ED_SAMPLERATE)->SetWindowText(strText);
			GetDlgItem(IDC_ED_BITSPERSAMPLE)->SetWindowText(strText);
			GetDlgItem(IDC_ED_ABITRATE)->SetWindowText(strText);
		}
		else
		{
			CString strText;
			strText.Format("%s", ap.codec);
			GetDlgItem(IDC_ED_ACODE)->SetWindowText(strText);
			strText.Format("%d", ap.channels);
			GetDlgItem(IDC_ED_CHANNEL)->SetWindowText(strText);
			strText.Format("%d", ap.samplerate);
			GetDlgItem(IDC_ED_SAMPLERATE)->SetWindowText(strText);
			strText.Format("%d", ap.bitspersample);
			GetDlgItem(IDC_ED_BITSPERSAMPLE)->SetWindowText(strText);
			strText.Format("%d", ap.bitrate);
			GetDlgItem(IDC_ED_ABITRATE)->SetWindowText(strText);
		}
	}
}

BOOL CCodeParamDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_ctlGroup.MoveWindow(CRect(m_rcClient.left+2, m_rcClient.top,m_rcClient.Width()-2, m_rcClient.Height()-2));
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
