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

#include "stdafx.h"
#include "By5000Asiplay Demo.h"
#include "TsDlg.h"
#include ".\tsdlg.h"
#include "By5000asiplay.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTsDlg 对话框

IMPLEMENT_DYNAMIC(CTsDlg, CDialog)
CTsDlg::CTsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTsDlg::IDD, pParent)
{
	
}

CTsDlg::~CTsDlg()
{
}

void CTsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_TS_SERVICE_TABLE, m_TsListCtrlTsServiceTable);
}


BEGIN_MESSAGE_MAP(CTsDlg, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_TSENCODER_CONFIG, OnBnClickedButtonTsencoderConfig)
	ON_BN_CLICKED(IDC_CHECK_TS_STREAM_MONITOR, OnBnClickedCheckTsStreamMonitor)
	ON_BN_CLICKED(IDC_CHECK_TS_PLAYBACK_SWITCH, OnBnClickedCheckTsPlaybackSwitch)
	ON_BN_CLICKED(IDC_CHECK_TS_CAPTURE_SWITCH, OnBnClickedCheckTsCaptureSwitch)
	ON_WM_TIMER()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_TS_SERVICE_TABLE, OnHdnItemdblclickListTsServiceTable)
	ON_BN_CLICKED(IDC_BUTTON_STARTUDP, &CTsDlg::OnBnClickedButtonStartudp)
	ON_BN_CLICKED(IDC_RADIO_INPUT_ASI, &CTsDlg::OnBnClickedRadioInputAsi)
	ON_BN_CLICKED(IDC_RADIO_INPUT_IP, &CTsDlg::OnBnClickedRadioInputIp)
	ON_BN_CLICKED(IDC_CHECK_IP_SEND, &CTsDlg::OnBnClickedCheckIpSend)
	ON_BN_CLICKED(IDC_BUTTON1, &CTsDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CTsDlg 消息处理程序

void CTsDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
//	OnOK();
}

BOOL CTsDlg::OnInitDialog()
{
	UINT uiTemp = 0;
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	((CEdit *)GetDlgItem(IDC_EDIT_TSBITRATE))->SetWindowText(_T("4300"));
	((CEdit *)GetDlgItem(IDC_EDIT_TSID))->SetWindowText(_T("1"));
	((CEdit *)GetDlgItem(IDC_EDIT_ONNET_ID))->SetWindowText(_T("1"));
	((CEdit *)GetDlgItem(IDC_EDIT_SERVICE_ID))->SetWindowText(_T("4097"));
	((CEdit *)GetDlgItem(IDC_EDIT_PMT_ID))->SetWindowText(_T("4096"));
	((CEdit *)GetDlgItem(IDC_EDIT_VIDEO_ID))->SetWindowText(_T("256"));
	((CEdit *)GetDlgItem(IDC_EDIT_AUDIO_ID))->SetWindowText(_T("257"));

	((CEdit *)GetDlgItem(IDC_EDIT_TS_PROVIDER))->SetWindowText(_T("SeekFit"));
	((CEdit *)GetDlgItem(IDC_EDIT_TS_CHANNELNAME))->SetWindowText(_T("测试-1"));

	((CEdit *)GetDlgItem(IDC_EDIT_POTR2))->SetWindowText(_T("1233"));
	((CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_ADD2))->SetWindowText(_T("224.8.0.68"));

	((CEdit *)GetDlgItem(IDC_EDIT_POTR))->SetWindowText(_T("1234"));
	((CIPAddressCtrl *)GetDlgItem(IDC_IPADDRESS_ADD))->SetWindowText(_T("224.8.0.68"));

	m_nTsSearchProgramStatus = TS_SEARCH_PROGRAM_NULL;
	m_TsNitSearchStatus = TS_SEARCH_PROGRAM_NULL;
	m_TsListCtrlTsServiceTable.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	m_TsListCtrlTsServiceTable.InsertColumn(0, _T("ServiceId"),LVCFMT_LEFT,50);
	m_TsListCtrlTsServiceTable.InsertColumn(1, _T("ServiceName"),LVCFMT_LEFT,120);
	m_TsListCtrlTsServiceTable.InsertColumn(2, _T("VideoId"),LVCFMT_LEFT,50);
	m_TsListCtrlTsServiceTable.InsertColumn(3, _T("AudioId"),LVCFMT_LEFT,50);
	m_TsListCtrlTsServiceTable.InsertColumn(4, _T("是否加扰"),LVCFMT_LEFT,50);
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_TS_CAPTURE_SWITCH);
	//ts采集功能默认是打开的。
	Check->SetCheck(1);
	
	Check = (CButton *)GetDlgItem(IDC_RADIO_INPUT_ASI);
	Check->SetCheck(1);

	m_bUdpCapture = FALSE;
	SetTimer(1, 100, NULL);
	m_uiTsId =0xFFFe;
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CTsDlg::OnBnClickedButtonTsencoderConfig()
{
	// TODO: 在此添加控件通知处理程序代码
	TS_ENCODER_PARAM tsEncoder;
	INT nTmp = 0;
	CString str1;
	CString str2;
	tsEncoder.uiStreamType = TS_STREAM_TYPE_MEPG2;
	((CEdit *)GetDlgItem(IDC_EDIT_TSBITRATE))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	
	tsEncoder.uiVideoRate = nTmp;
	//((CEdit *)GetDlgItem(IDC_EDIT_TSVIDEOBITRATE))->GetWindowText(str1);
	//sscanf_s(str1,_T("%d"),&nTmp);
	//tsEncoder.uiVideoRate= 6000;
	((CEdit *)GetDlgItem(IDC_EDIT_TSID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiTsId = nTmp;
	((CEdit *)GetDlgItem(IDC_EDIT_SERVICE_ID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiServiceId = nTmp;
	((CEdit *)GetDlgItem(IDC_EDIT_ONNET_ID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiOnNetId = nTmp;
	((CEdit *)GetDlgItem(IDC_EDIT_PMT_ID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiPmtPid = nTmp;
	((CEdit *)GetDlgItem(IDC_EDIT_VIDEO_ID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiVideoPid = nTmp;
	((CEdit *)GetDlgItem(IDC_EDIT_AUDIO_ID))->GetWindowText(str1);
#ifdef _UNICODE
	swscanf_s(str1,_T("%d"),&nTmp);

#else
	sscanf_s(str1,_T("%d"),&nTmp);
#endif
	tsEncoder.uiAudioPid = nTmp;
	
	((CEdit *)GetDlgItem(IDC_EDIT_TS_PROVIDER))->GetWindowText(str1);
	tsEncoder.strProviderName = (LPCTSTR)str1;
	((CEdit *)GetDlgItem(IDC_EDIT_TS_CHANNELNAME))->GetWindowText(str2);
	tsEncoder.strServiceName = (LPCTSTR)str2;
	Seekfit_By5000asiplay_TsPlaybackEncoderConfig((LPVOID)&tsEncoder,m_uiCardNumber);
}
void CTsDlg::OnBnClickedCheckTsPlaybackSwitch()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_TS_PLAYBACK_SWITCH);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		Seekfit_By5000asiplay_UdpSendStop(m_uiCardNumber);
		Check = (CButton *)GetDlgItem(IDC_CHECK_IP_SEND);
		Check->SetCheck(FALSE);
		Seekfit_By5000asiplay_TsPlaybackDisable(m_uiCardNumber);
	}
	else
	{	

		Seekfit_By5000asiplay_TsPlaybackEnable(m_uiCardNumber);
	}
}

void CTsDlg::OnBnClickedCheckTsCaptureSwitch()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_TS_CAPTURE_SWITCH);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		Seekfit_By5000asiplay_TsCaptureDisable(m_uiCardNumber);
	}
	else
	{	
		Seekfit_By5000asiplay_TsCaptureEnable(m_uiCardNumber);
	}
}
void CTsDlg::OnBnClickedCheckTsStreamMonitor()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_TS_STREAM_MONITOR);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		Seekfit_By5000asiplay_TsPlaybackSwitchStreamMonitor(0,m_uiCardNumber);
	}
	else
	{	
		Seekfit_By5000asiplay_TsPlaybackSwitchStreamMonitor(1,m_uiCardNumber);
	}
}

void CTsDlg::OnTimer(UINT nIDEvent)
{

	//ts programinfo
	//INT nLastServiceId = -1;
	//INT nCurServiceId = -1;
	TS_PROGRAM_INFO TsProgramInfo;
	Seekfit_By5000asiplay_TsCaptureGetProgramInformation(TsProgramInfo, m_nTsSearchProgramStatus, m_uiCardNumber);
	if(m_nTsSearchProgramStatus == TS_SEARCH_PROGRAM_OK)
	{
		m_TsProgramInfo = TsProgramInfo;
		INT nChannelsNum = TsProgramInfo.sdtServiceTable.nMaxServiceInfoNum;
			
		//if(/*!bAsiBitRateSet && */TsProgramInfo.dbCurRate!=0)
		//{
		//	Seekfit_By5000asiio_SetAsiBitRate(TsProgramInfo.dbCurRate);
		//}

		CString str;
		str.Format(_T("%.3f"),TsProgramInfo.dbCurRate);
		SetDlgItemText(IDC_STATIC_TS_CURRATE,str);
		
		str.Format(_T("网络ID :0x%x"),TsProgramInfo.uiOnNetID);
		SetDlgItemText(IDC_STATIC_ONNETID,str);

		str.Format(_T("流ID :0x%x"),TsProgramInfo.uiTsID);
		SetDlgItemText(IDC_STATIC_TSID,str);
		
		
			
	/*	int aaa=m_TsListCtrlTsServiceTable.GetItemCount();
		int aa=GetLastError();*/
		if(m_uiTsId != TsProgramInfo.uiTsID)
		{
			m_TsListCtrlTsServiceTable.DeleteAllItems();
			ASSERT(m_TsListCtrlTsServiceTable.GetItemCount() == 0);
			for(INT i = 0;i< nChannelsNum;i++)
			{
				//inset item
				str.Format(_T("%d"), TsProgramInfo.sdtServiceTable.aServiceInfo[i].nServiceId);
				m_TsListCtrlTsServiceTable.InsertItem(i,LPCTSTR(str));
#ifdef _UNICODE
				int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, (LPCSTR)TsProgramInfo.sdtServiceTable.aServiceInfo[i].aServiceName, -1, NULL, 0 );
				wchar_t* pResult = new wchar_t[nLen];
				MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, (LPCSTR)TsProgramInfo.sdtServiceTable.aServiceInfo[i].aServiceName, -1, pResult, nLen );

				m_TsListCtrlTsServiceTable.SetItemText(i,1,(LPCTSTR)pResult);
#else
				m_TsListCtrlTsServiceTable.SetItemText(i,1,(LPCTSTR)(LPTSTR)TsProgramInfo.sdtServiceTable.aServiceInfo[i].aServiceName);
#endif

				for (int j=0;j<TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_num;j++)
				{
					if (TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type == STREAMTYPE_VIDEO)
					{
						str.Format(_T("0x%x"), TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].elementary_pid);
						m_TsListCtrlTsServiceTable.SetItemText(i,2,(LPCTSTR)(str));
					}
					else if (TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type == STREAMTYPE_AUDIO11172||
						TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type == STREAMTYPE_AUDIO13818||
						TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type ==0x06||
						(TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type >=0x80&&
						TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].stream_type < 0xff)
						)
					{
						str.Format(_T("0x%x"), TsProgramInfo.sdtServiceTable.aServiceInfo[i].es_info[j].elementary_pid);
						m_TsListCtrlTsServiceTable.SetItemText(i,3,(LPCTSTR)(str));
					}
				}
				if(TsProgramInfo.sdtServiceTable.aServiceInfo[i].bScrambling)
				{
					m_TsListCtrlTsServiceTable.SetItemText(i,4,_T("YES"));
				}
				else
				{
					m_TsListCtrlTsServiceTable.SetItemText(i,4,_T("NO"));
				}

			}
			if (TsProgramInfo.sdtServiceTable.nMaxServiceInfoNum>1)
			{
				Seekfit_By5000asiplay_TsSelectProgram(0,m_uiCardNumber);
			}
		}
		else
		{
			for(INT i = 0;i< nChannelsNum;i++)
			{
				if(TsProgramInfo.sdtServiceTable.aServiceInfo[i].bScrambling)
				{
					m_TsListCtrlTsServiceTable.SetItemText(i,4,_T("YES"));
				}
				else
				{
					m_TsListCtrlTsServiceTable.SetItemText(i,4,_T("NO"));
				}
			}
		}
		m_uiTsId = TsProgramInfo.uiTsID;
		
	}
	
	if(Seekfit_By5000asiplay_GetTransportInformation(m_nitWorkInfo,m_TsNitSearchStatus,m_uiCardNumber))
	{
		if (m_TsNitSearchStatus == TS_SEARCH_PROGRAM_OK)
		{
			//TRACE("ok");
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CTsDlg::OnHdnItemdblclickListTsServiceTable( NMHDR *pNMHDR, LRESULT *pResult )
{
//	LPNMHEADER phdr = reinterpret_cast<LPNMHEADER>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	INT index=m_TsListCtrlTsServiceTable.GetSelectionMark();

	CString str=m_TsListCtrlTsServiceTable.GetItemText(index,0);

	if(str.GetLength()>0)
	{
		LONG num;
		#ifdef _UNICODE
				num=_tstoi(str);
		#else
				num=atoi(str);
		#endif
		Seekfit_By5000asiplay_TsSelectProgram(num,m_uiCardNumber);
	}
}
void CTsDlg::OnBnClickedButtonStartudp()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strIp,str;
	UINT uiPort=0;

	TS_OVER_IP_MULTICAST_PARAM TsOverIpParam;
	GetDlgItemText(IDC_IPADDRESS_ADD,strIp);
	if (strIp.IsEmpty())
	{
		MessageBox(_T("请输入组播IP地址"));
		return ;
	}
	TsOverIpParam.strDestIp = strIp;
	GetDlgItemText(IDC_EDIT_POTR,str);
	if (str.IsEmpty())
	{
		MessageBox(_T("请输入组播IP端口号"));
		return ;
	}
#ifdef _UNICODE
	uiPort=_tstoi(str);
#else
	uiPort=atoi(str);
#endif
	//uiPort=atoi(str);
	TsOverIpParam.uiDestUdpPort = uiPort;
	TsOverIpParam.uiTsPacketLength = 188;
	TsOverIpParam.uiTsBitRate = 38000000;
	TsOverIpParam.bFECEnable = FALSE;
	TsOverIpParam.bRtpEnable = FALSE;

	if (!m_bUdpCapture)
	{
		Seekfit_By5000asiplay_SetReceiveParameter(TsOverIpParam);
		m_bUdpCapture = TRUE;
		Seekfit_By5000asiplay_UdpReceiveRun(m_uiCardNumber);
		SetDlgItemText(IDC_BUTTON_STARTUDP,_T("IP输入开"));
	}
	else
	{
		m_bUdpCapture = FALSE;
		Seekfit_By5000asiplay_UdpReceiveStop(m_uiCardNumber);
		SetDlgItemText(IDC_BUTTON_STARTUDP,_T("IP输入关"));
	}
}
void CTsDlg::OnBnClickedRadioInputAsi()
{
	// TODO: 在此添加控件通知处理程序代码
	Seekfit_By5000asiplay_TsSetInputSource(0,m_uiCardNumber);
}

void CTsDlg::OnBnClickedRadioInputIp()
{
	// TODO: 在此添加控件通知处理程序代码
		Seekfit_By5000asiplay_TsSetInputSource(1,m_uiCardNumber);
}


void CTsDlg::OnBnClickedCheckIpSend()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strIp,str;
	UINT uiPort=0;

	TS_OVER_IP_MULTICAST_PARAM TsOverIpParam;
	GetDlgItemText(IDC_IPADDRESS_ADD2,strIp);
	if (strIp.IsEmpty())
	{
		MessageBox(_T("请输入组播IP地址"));
		return ;
	}
	TsOverIpParam.strDestIp = strIp;
	GetDlgItemText(IDC_EDIT_POTR2,str);
	if (str.IsEmpty())
	{
		MessageBox(_T("请输入组播IP端口号"));
		return ;
	}
#ifdef _UNICODE
	uiPort=_tstoi(str);
#else
	uiPort=atoi(str);
#endif
	TsOverIpParam.uiDestUdpPort = uiPort;
	TsOverIpParam.uiTsPacketLength = 188;
	TsOverIpParam.uiTsBitRate = 9000000;
	TsOverIpParam.bFECEnable = FALSE;
	TsOverIpParam.bRtpEnable = FALSE;


	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_IP_SEND);
	if (Check->GetCheck() == BST_CHECKED)
	{
		Seekfit_By5000asiplay_SetSendParameter(TsOverIpParam);
		Seekfit_By5000asiplay_UdpSendRun(m_uiCardNumber);
	}
	else
	{
		Seekfit_By5000asiplay_UdpSendStop(m_uiCardNumber);
	}

}

void CTsDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	Seekfit_By5000asiplay_TsSelectProgram(11,m_uiCardNumber);

}
