// By5000Asiplay DemoView.cpp : CBy5000AsiplayDemoView 类的实现
//

#include "stdafx.h"
#include "By5000Asiplay Demo.h"

#include "By5000Asiplay DemoDoc.h"
#include "By5000Asiplay DemoView.h"
#include ".\by5000asiplay demoview.h"
#include "ImageReader.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

DWORD WINAPI FilePlaybackControlThread(LPVOID pBy5000playDemoDlg);


// CBy5000AsiplayDemoView

IMPLEMENT_DYNCREATE(CBy5000AsiplayDemoView, CFormView)

BEGIN_MESSAGE_MAP(CBy5000AsiplayDemoView, CFormView)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BROWSE, OnBnClickedBrowse)
	ON_BN_CLICKED(IDC_PLAY, OnBnClickedPlay)
	ON_BN_CLICKED(IDC_STOP, OnBnClickedStop)
	ON_BN_CLICKED(IDC_SWITCH, OnBnClickedSwitch)
	ON_BN_CLICKED(ID_BUTTON_RESTORE_DEFAULT, OnBnClickedButtonRestoreDefault)
	ON_BN_CLICKED(IDC_BUTTON_SET_MEDIA_RATE, OnBnClickedButtonSetMediaRate)
	ON_BN_CLICKED(IDC_CHECK_VIDEO_OUTPUT_PREVIEW, OnBnClickedCheckVideoOutputPreview)
	ON_BN_CLICKED(IDC_CHECK_VIDEO_CAPTURE_PREVIEW, OnBnClickedCheckVideoCapturePreview)
	ON_BN_CLICKED(IDC_CHECK_ANALYZE_MEDIA_FILE, OnBnClickedCheckAnalyzeMediaFile)
	ON_BN_CLICKED(IDC_CHECK_AUDIO_VOLUMN_OVERLAY_MONITOR, OnBnClickedCheckAudioVolumnOverlayMonitor)
	ON_BN_CLICKED(IDC_CHECK_SHOW_SAFE_MARGINS, OnBnClickedCheckShowSafeMargins)
	ON_BN_CLICKED(IDC_CHECK_CAPAUDIO_VOLUMN_OVERLAY_MONITOR, OnBnClickedCheckCapaudioVolumnOverlayMonitor)
	ON_BN_CLICKED(IDC_CHECK_FIRST_FRAME_PAUSE, OnBnClickedCheckFirstFramePause)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_HOLDWND, OnTcnSelchangeTabHoldwnd)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnBnClickedButtonTest)
	ON_BN_CLICKED(IDC_RESET_CARD, OnBnClickedResetCard)
	ON_BN_CLICKED(IDC_CHECK_DVD_SUB_ENABLE, OnBnClickedCheckDvdSubEnable)
	ON_BN_CLICKED(IDC_CHECK_WRITE_LOGFILE, OnBnClickedCheckWriteLogfile)
	ON_BN_CLICKED(IDC_CHECK__AUTOSWICTH, OnBnClickedCheck)
END_MESSAGE_MAP()

// CBy5000AsiplayDemoView 构造/析构

CBy5000AsiplayDemoView::CBy5000AsiplayDemoView()
	: CFormView(CBy5000AsiplayDemoView::IDD)
	, m_uiCardNumber(0)
{
	// TODO: 在此处添加构造代码
	m_bIsCardOpen=FALSE;
	m_bIsDvdSubPause = TRUE;

}

CBy5000AsiplayDemoView::~CBy5000AsiplayDemoView()
{
}

void CBy5000AsiplayDemoView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
//	DDX_Control(pDX, IDC_LIST_TS_SERVICE_TABLE, m_ListCtrlTsServiceTable);
	DDX_Control(pDX, IDC_TAB_HOLDWND, m_tabctlHoldWnd);
	DDX_Control(pDX, IDC_MEDIA_TIME_CODE, m_MediaTimeCode);
}

BOOL CBy5000AsiplayDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式

	return CFormView::PreCreateWindow(cs);
}

void CBy5000AsiplayDemoView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	HWND hWnd;
	RECT   rectWindow;
	UINT uiWidth,uiHeight;

	m_bIsExit = FALSE;
	m_bIsPlaying = FALSE;
	m_bIsFirstFramePause = FALSE;
	m_hFilePlaybackControlThread = NULL;
	m_hExitEvent = NULL;
	m_hEndOfFilePlaybackEvent = NULL;
	m_bAutoSwtich = TRUE;
	m_ulCurrentPlayDuration = 0;
	m_ulCurrentPlayTime =0;
	m_bSetSwtich = TRUE;
	CBy5000AsiplayDemoApp* pApp = (CBy5000AsiplayDemoApp*)AfxGetApp();
	if (!pApp) return;
	m_uiCardNumber = pApp->m_uiCardNumber;
	if (pApp->m_uiCardCount>4)
	{
		for (int i=0;i<pApp->m_uiCardCount;i++)
		{
			if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA, i))
				//	if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | By5000ASIPLAY_OPTION_CONTROL_RELAY, m_uiCardNumber))
				//if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | 0x8000, m_uiCardNumber))	
			{
				AfxMessageBox(_T("Card open failed!"));
				AfxPostQuitMessage(0);
				return /*TRUE*/;
			}
			else
				m_bIsCardOpen=TRUE;
			Sleep(100);
		}
	} 
	else
	{
		if (pApp->m_uiCardCount>0)
		{
			if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA , m_uiCardNumber))
				//	if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | By5000ASIPLAY_OPTION_CONTROL_RELAY, m_uiCardNumber))
				//if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | 0x8000, m_uiCardNumber))	
			{
				AfxMessageBox(_T("Card open failed!"));
				AfxPostQuitMessage(0);
				return /*TRUE*/;
			}
			else
				m_bIsCardOpen=TRUE;
		}
		else
		{
			if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA|0x8000 , m_uiCardNumber))
				//	if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | By5000ASIPLAY_OPTION_CONTROL_RELAY, m_uiCardNumber))
				//if (!Seekfit_By5000asiplay_OpenCard(By5000ASIPLAY_MODE_MEDIA | 0x8000, m_uiCardNumber))	
			{
				AfxMessageBox(_T("Card open failed!"));
				AfxPostQuitMessage(0);
				return /*TRUE*/;
			}
			else
				m_bIsCardOpen=TRUE;
		}
	}
	UINT uiCardCount;
	CString str;
	Seekfit_By5000asiplay_GetCardTypeNumber(m_uiCardType,m_uiCardNumber);
	if(m_uiCardType == SEEKFIT_CARD_TYPE_By5000ASIPLAY)
		str = _T("By5000ASI");
	else if (m_uiCardType == SEEKFIT_CARD_TYPE_By5000ASIPROPLAY)
	{
		str = _T("BY5KII ASI ");
	}
	GetDlgItem(IDC_EDIT_CARD_TYPE)->SetWindowText(str);


	Seekfit_By5000asiplay_GetFPGAVersion(&uiCardCount,m_uiCardNumber);
	str.Format(_T("%d.%d.%d"),  (uiCardCount >> 16) & 0xff, \
		(uiCardCount >> 8) & 0xff, \
		uiCardCount & 0xff );
	GetDlgItem(IDC_EDIT_FPGA_VERSION)->SetWindowText(str);
	Seekfit_By5000asiplay_GetBoardVersion(&uiCardCount,m_uiCardNumber);
	str.Format(_T("%d.%d.%d"),  (uiCardCount >> 16) & 0xff, \
		(uiCardCount >> 8) & 0xff, \
		uiCardCount & 0xff );
	GetDlgItem(IDC_EDIT_BORAD_VERSION)->SetWindowText(str);
	ULONG Serialnumber = 0;
	Seekfit_By5000asiplay_GetSerialNumber(Serialnumber,m_uiCardNumber);
	str.Format(_T("%.8x"),Serialnumber);
	GetDlgItem(IDC_EDIT_CARD_NUM)->SetWindowText(str);
	m_hExitEvent = CreateEvent(	NULL,
		TRUE,	// Manual reset event
		FALSE,
		NULL
		) ;
	if (m_hExitEvent == NULL)
		return /*FALSE*/ ;
	CString strEventName;
	strEventName.Format(_T("SeekfitBy5000asiplayEndOfFilePlaybackEvent%d"), m_uiCardNumber);
	Seekfit_By5000asiplay_InitialEndOfFilePlaybackEvent(strEventName, m_hEndOfFilePlaybackEvent, m_uiCardNumber);

	m_hFilePlaybackControlThread = CreateThread(0, 0, FilePlaybackControlThread, this, 0, 0);

	::GetClientRect(this->m_hWnd, &rectWindow);
	uiWidth = rectWindow.right - rectWindow.left;
	uiHeight = rectWindow.bottom - rectWindow.top;
	m_nWindowWidth = uiWidth;
	m_nWindowHeight = uiHeight;
	GetDlgItem(IDC_VIDEO_PREVIEW, &hWnd);
	::GetClientRect(hWnd, &rectWindow);
	uiWidth = rectWindow.right - rectWindow.left;
	uiHeight = rectWindow.bottom - rectWindow.top;
	Seekfit_By5000asiplay_VideoPreviewWindowSet(0, hWnd, 0, 0, uiWidth - 2, uiHeight, m_uiCardNumber);
	((CButton *)GetDlgItem(IDC_CHECK_VIDEO_OUTPUT_PREVIEW))->SetCheck(1);
	((CButton *)GetDlgItem(IDC_CHECK_DVD_SUB_ENABLE))->SetCheck(1);
	Seekfit_By5000asiplay_EnableVideoPreviewWindow(0, m_uiCardNumber);

	GetDlgItem(IDC_VIDEO_PREVIEW_CAPTURE, &hWnd);
	::GetClientRect(hWnd, &rectWindow);
	uiWidth = rectWindow.right - rectWindow.left;
	uiHeight = rectWindow.bottom - rectWindow.top;
	Seekfit_By5000asiplay_VideoPreviewWindowSet(1, hWnd, 0, 0, uiWidth - 2, uiHeight, m_uiCardNumber);
	//	Seekfit_By5000asiplay_EnableVideoPreviewWindow(1, m_uiCardNumber);
	((CButton *)GetDlgItem(IDC_CHECK_AUDIO_VOLUMN_OVERLAY_MONITOR))->SetCheck(0);
	GetDlgItem(IDC_STATIC_LEFT_VOLUMN_BAR, &hWnd);
	Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_WINDOW_VERTICAL_UP_MODE, hWnd, m_uiCardNumber);
	GetDlgItem(IDC_STATIC_RIGHT_VOLUMN_BAR, &hWnd);
	Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_WINDOW_VERTICAL_UP_MODE, hWnd, m_uiCardNumber);

	GetDlgItem(IDC_STATIC_CAPTURE_LEFT_VOLUMN_BAR, &hWnd);
	Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_WINDOW_VERTICAL_DOWN_MODE, hWnd, m_uiCardNumber);
	GetDlgItem(IDC_STATIC_CAPTURE_RIGHT_VOLUMN_BAR, &hWnd);
	Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_WINDOW_VERTICAL_DOWN_MODE, hWnd, m_uiCardNumber);

	//Seekfit_By5000asiplay_EnableAudioVolumnOverlayMonitor(AUDIO_VOLUMN_LEFT, m_uiCardNumber);
	//Seekfit_By5000asiplay_EnableAudioVolumnOverlayMonitor(AUDIO_VOLUMN_RIGHT, m_uiCardNumber);

	//	Seekfit_By5000asiplay_CapturedStreamEnableDelay(m_uiCardNumber);
	Seekfit_By5000asiplay_CapturedStreamSetDelayTime(5, 0, m_uiCardNumber);


	m_nIDTimer = SetTimer(1, 20, NULL);
	SetDlgItemText(IDC_MEDIA_RATE, _T("1"));
	m_bAnalyzeMediaFile = TRUE;
	((CButton *)GetDlgItem(IDC_CHECK_ANALYZE_MEDIA_FILE))->SetCheck(1);
	((CButton *)GetDlgItem(IDC_CHECK_WRITE_LOGFILE))->SetCheck(1);

	m_tabctlHoldWnd.InsertItem(0,_T("系统"));
	m_tabctlHoldWnd.InsertItem(1,_T("音频设置"));
	m_tabctlHoldWnd.InsertItem(2,_T("在播文件"));
	m_tabctlHoldWnd.InsertItem(3,_T("待播文件"));
	m_tabctlHoldWnd.InsertItem(4,_T("采集设置"));
	m_tabctlHoldWnd.InsertItem(5,_T("TS设置"));


	m_dlgSysSet.Create(IDD_DLG_SYSTEM,&m_tabctlHoldWnd);
	m_dlgSysSet.m_uiCardNumber = m_uiCardNumber;

	
	m_dlgAudio.Create(IDD_DLG_AUDIO,&m_tabctlHoldWnd);
m_dlgAudio.m_uiCardNumber = m_uiCardNumber;

	
	
	m_dlgCapture.Create(IDD_DLG_CAPTURE,&m_tabctlHoldWnd);
m_dlgCapture.m_uiCardNumber = m_uiCardNumber;
m_dlgCapture.m_uiCardType = m_uiCardType;


	m_dlgCurVideo.Create(IDD_DLG_VIDEO_CURRENT,&m_tabctlHoldWnd);
	m_dlgCurVideo.m_uiCardNumber = m_uiCardNumber;
//

//	m_dlgNextVideo.m_uiVideoMode = m_uiVideoMode;
	m_dlgNextVideo.Create(IDD_DLG_VIDEO_NEXT,&m_tabctlHoldWnd);
	m_dlgNextVideo.m_uiCardNumber = m_uiCardNumber;
//

	m_dlgTs.Create(IDD_DLG_TS,&m_tabctlHoldWnd);
	m_dlgTs.m_uiCardNumber = m_uiCardNumber;

	CRect rectClient;
	m_tabctlHoldWnd.GetClientRect(rectClient);
	//ClientToScreen(rectClient);
	rectClient.top += 40; 
	rectClient.bottom -= 20; 
	rectClient.left += 1 ; 
	rectClient.right -= 1; 

	m_dlgSysSet.MoveWindow(&rectClient);
	m_dlgSysSet.ShowWindow(SW_SHOW);

	m_dlgCapture.MoveWindow(&rectClient);
	m_dlgCapture.ShowWindow(SW_HIDE);

	m_dlgAudio.MoveWindow(&rectClient);
	m_dlgAudio.ShowWindow(SW_HIDE);

	m_dlgCurVideo.MoveWindow(&rectClient);
	m_dlgCurVideo.ShowWindow(SW_HIDE);

	m_dlgNextVideo.MoveWindow(&rectClient);
	m_dlgNextVideo.ShowWindow(SW_HIDE);

	m_dlgTs.MoveWindow(&rectClient);
	m_dlgTs.ShowWindow(SW_HIDE);


	m_tabctlHoldWnd.SetCurSel(0);
	//m_nTsSearchProgramStatus = TS_SEARCH_PROGRAM_NULL;
	
	/*CBitmap * pBitmap ;
	pBitmap = new CBitmap;
	CTgaReader tgaReader;
	BITMAP bm;
	ULONG *pPicture;
	tgaReader.LoadImage("D:\\zmk_demo\\debuglib\\beijing.tga",pBitmap);
	pBitmap->GetBitmap(&bm);
	pPicture = new ULONG[ bm.bmWidth*bm.bmHeight ];
	pBitmap ->GetBitmapBits(bm.bmWidth*bm.bmHeight*4,pPicture);

	Seekfit_By5000asiplay_MediaSetBackgroundPicture(pPicture, m_uiCardNumber);
	delete pPicture;*/
}

void CBy5000AsiplayDemoView::OnBnClickedCheckFirstFramePause()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_FIRST_FRAME_PAUSE);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		m_bIsFirstFramePause = FALSE;
		Seekfit_By5000asiplay_MediaDisableFirstFramePause(m_uiCardNumber);
	}
	else
	{	
		m_bIsFirstFramePause = TRUE;
		Seekfit_By5000asiplay_MediaEnableFirstFramePause(m_uiCardNumber);
	}
}

void CBy5000AsiplayDemoView::OnBnClickedCheckAudioVolumnOverlayMonitor()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_AUDIO_VOLUMN_OVERLAY_MONITOR);
	if (Check->GetCheck() == BST_UNCHECKED)
	{
		HWND hWnd;
		GetDlgItem(IDC_STATIC_LEFT_VOLUMN_BAR, &hWnd);
		Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_WINDOW_VERTICAL_UP_MODE, hWnd, m_uiCardNumber);
		GetDlgItem(IDC_STATIC_RIGHT_VOLUMN_BAR, &hWnd);
		Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_WINDOW_VERTICAL_UP_MODE, hWnd, m_uiCardNumber);
	}
	else
	{
		Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_OVERLAY_VERTICAL_UP_MODE, NULL, m_uiCardNumber);
		Seekfit_By5000asiplay_MediaSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_OVERLAY_VERTICAL_UP_MODE, NULL, m_uiCardNumber);
	}
}

void CBy5000AsiplayDemoView::OnBnClickedCheckCapaudioVolumnOverlayMonitor()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_CAPAUDIO_VOLUMN_OVERLAY_MONITOR);
	if (Check->GetCheck() == BST_UNCHECKED)
	{
		HWND hWnd;
		GetDlgItem(IDC_STATIC_CAPTURE_LEFT_VOLUMN_BAR, &hWnd);
		Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_WINDOW_VERTICAL_DOWN_MODE, hWnd, m_uiCardNumber);
		GetDlgItem(IDC_STATIC_CAPTURE_RIGHT_VOLUMN_BAR, &hWnd);
		Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_WINDOW_VERTICAL_DOWN_MODE, hWnd, m_uiCardNumber);
	}
	else
	{
		Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_LEFT, VOLUMN_OVERLAY_VERTICAL_UP_MODE, NULL, m_uiCardNumber);
		Seekfit_By5000asiplay_CapturedStreamSetVolumnShowBarMode(AUDIO_VOLUMN_RIGHT, VOLUMN_OVERLAY_VERTICAL_UP_MODE, NULL, m_uiCardNumber);
	}
}

void CBy5000AsiplayDemoView::OnBnClickedCheckAnalyzeMediaFile()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_ANALYZE_MEDIA_FILE);
	if (Check->GetCheck() == BST_UNCHECKED)
		m_bAnalyzeMediaFile = FALSE;
	else
		m_bAnalyzeMediaFile = TRUE;
}

void CBy5000AsiplayDemoView::OnBnClickedCheckShowSafeMargins()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK_SHOW_SAFE_MARGINS);
	if (Check->GetCheck() == BST_UNCHECKED)
	{
		Seekfit_By5000asiplay_DisableShowSafeMargins(m_uiCardNumber);
	}
	else
	{
		Seekfit_By5000asiplay_EnableShowSafeMargins(m_uiCardNumber);
	}
}


void CBy5000AsiplayDemoView::OnBnClickedCheckVideoCapturePreview()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_VIDEO_CAPTURE_PREVIEW);
	if (Check->GetCheck() == BST_UNCHECKED)
		Seekfit_By5000asiplay_DisableVideoPreviewWindow(1, m_uiCardNumber);
	else
		Seekfit_By5000asiplay_EnableVideoPreviewWindow(1, m_uiCardNumber);
}


void CBy5000AsiplayDemoView::OnBnClickedCheckVideoOutputPreview()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_VIDEO_OUTPUT_PREVIEW);
	if (Check->GetCheck() == BST_UNCHECKED)
		Seekfit_By5000asiplay_DisableVideoPreviewWindow(0, m_uiCardNumber);
	else
		Seekfit_By5000asiplay_EnableVideoPreviewWindow(0, m_uiCardNumber);
}

void CBy5000AsiplayDemoView::OnBnClickedButtonSetMediaRate()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str = _T("");
	FLOAT fRate = 0;
	GetDlgItemText(IDC_MEDIA_RATE, str);
#ifdef _UNICODE
	swscanf_s(str,_T("%d"),&fRate);

#else
	sscanf_s(str,_T("%d"),&fRate);
#endif
	Seekfit_By5000asiplay_MediaSetRate(fRate, m_uiCardNumber);
}

void CBy5000AsiplayDemoView::OnBnClickedButtonRestoreDefault()
{
	// TODO: 在此添加控件通知处理程序代码
	Seekfit_By5000asiplay_RestoreDefault(m_uiCardNumber);
	Seekfit_By5000asiplay_SetVideoSource(By5000ASIPLAY_VIDEO_SOURCE_CVBS,m_uiCardNumber);
	m_dlgSysSet.SysRestore();
	return;

	/*UINT uiTemp;
	CButton * pButton;
	Seekfit_By5000asiplay_GetVideoMode(uiTemp, m_uiCardNumber);
	if (uiTemp == VIDEO_MODE_PAL)
		pButton = (CButton*)(GetDlgItem(IDC_PAL));
	else
		pButton = (CButton*)(GetDlgItem(IDC_NTSC));
	pButton ->SetCheck(1);
	m_uiVideoMode = uiTemp;

	Seekfit_By5000asiplay_GetSyncSource(uiTemp, m_uiCardNumber);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_INTERNAL));
	pButton ->SetCheck(0);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_CVBS1));
	pButton ->SetCheck(0);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_SVIDEO));
	pButton ->SetCheck(0);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_YUV));
	pButton ->SetCheck(0);
	if (uiTemp == By5000ASIPLAY_SYNC_SOURCE_INTERNAL)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_INTERNAL));
	else if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_CVBS)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_CVBS1));
	else if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_SVIDEO)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_SVIDEO));
	else if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_YUV)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_YUV));
	else
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SYNC_INTERNAL));
	pButton ->SetCheck(1);

	Seekfit_By5000asiplay_GetVideoSource(uiTemp, m_uiCardNumber);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_CVBS1));
	pButton ->SetCheck(0);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_SVIDEO));
	pButton ->SetCheck(0);
	pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_YUV));
	pButton ->SetCheck(0);
	if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_CVBS)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_CVBS1));
	else if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_SVIDEO)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_SVIDEO));
	else if (uiTemp == By5000ASIPLAY_VIDEO_SOURCE_YUV)
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_YUV));
	else
		pButton = (CButton*)(GetDlgItem(IDC_VIDEO_SOURCE_CVBS1));
	pButton ->SetCheck(1);*/
}



void CBy5000AsiplayDemoView::OnBnClickedBrowse()
{
	// TODO: 在此添加控件通知处理程序代码
	TCHAR szFilter[] = _T("Media Files (*.dat; *.mpg; *.vob; *.m2p; *.ts; *.m2t; *.pes; *.m2v; *.m1v; *.m2a; *.mpv; *.rm; *.rmvb; *.avi; *.asf; *.wmv ; *.mp3 ; *.wav;*.flv)|*.dat; *.mpg; *.vob; *.m2p; *.ts; *.m2t; *.pes; *.m2v; *.m1v; *.m2a; *.mpv; *.rm; *.rmvb; *.avi; *.asf; *.wmv ; *.mp3 ; *.wav;*.flv|All Files(*.*)|*.*||");
	CFileDialog DlgFile(TRUE, NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	DlgFile.m_ofn.lpstrTitle = _T("播放节目");
	BOOL bResult;
	ULONG ulInTime;
	ULONG ulOutTime;
	MEDIA_FILE_INFORMATION m_mfiMediaFileInformation;

	if( DlgFile.DoModal() == IDOK)
	{
		m_strMpegFileName = DlgFile.GetPathName();
		SetDlgItemText(IDC_MEDIA_FILE, m_strMpegFileName);
		m_dlgCurVideo.m_strFileName = m_strMpegFileName;
		if (m_bAnalyzeMediaFile)
		{
			bResult = FALSE;
			Seekfit_By5000asiplay_GetAnalyzedMediaFileInformation(m_strMpegFileName, 80, bResult, m_mfiMediaFileInformation, m_uiCardNumber);
			if (bResult)
			{
				bResult=FALSE;
				Seekfit_By5000asiplay_GetAnalyzedMediaFileInformation(m_strMpegFileName, 80, bResult, m_mfiMediaFileInformation, m_uiCardNumber);
				if (bResult)
				{
					AfxMessageBox(_T("素材文件存在问题，请重新转码！"));
					return;
				}
				else
				{
					Seekfit_By5000asiplay_MediaNextSetDemultiplexerNumber(m_mfiMediaFileInformation.uiDemultiNumber,m_uiCardNumber);
				}
			}
			else
			{
				Seekfit_By5000asiplay_MediaNextSetDemultiplexerNumber(m_mfiMediaFileInformation.uiDemultiNumber,m_uiCardNumber);
			}
		}
	
		Seekfit_By5000asiplay_MediaSetDVDSubEnable(m_bIsDvdSubPause,m_uiCardNumber);
		bResult = Seekfit_By5000asiplay_MediaNextOpen(m_strMpegFileName, m_uiCardNumber);

		
		if (!bResult)
		{
			AfxMessageBox(_T("打开文件失败！"));
			return;
		}
		if(m_bIsFirstFramePause)
		{
			ulInTime = 0x00000001;
			ulOutTime = 0x00000000;
			Seekfit_By5000asiplay_MediaNextSeek(ulInTime, ulOutTime, m_uiCardNumber);
		}
		Seekfit_By5000asiplay_MediaNextGetFileInformation(m_mfiMediaFileInformation, m_uiCardNumber);
		m_ulCurrentPlayDuration = m_mfiMediaFileInformation.ulTimeDuration;

		//ulInTime = 0x00000500;
		//ulOutTime = 0x00000a00;
		//Seekfit_By5000asiplay_MediaNextSeek(ulInTime, ulOutTime, m_uiCardNumber);
		//Seekfit_By5000asiplay_MediaNextPeek(ulInTime, ulOutTime, m_uiCardNumber);
	}
}



void CBy5000AsiplayDemoView::OnDestroy()
{
	CFormView::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	KillTimer(m_nIDTimer);
	Sleep(100);

	SetEvent(m_hExitEvent);
	SetEvent(m_hEndOfFilePlaybackEvent);
	if(m_hFilePlaybackControlThread)
	{
		HANDLE handle[1] = {m_hFilePlaybackControlThread};
		WaitForMultipleObjects(1, handle, TRUE, INFINITE);
		CloseHandle(m_hFilePlaybackControlThread);
		m_hFilePlaybackControlThread = NULL;
	}
	CloseHandle(m_hExitEvent);
	m_hExitEvent = NULL;

	if(m_hEndOfFilePlaybackEvent)
		Seekfit_By5000asiplay_CloseEndOfFilePlaybackEvent(m_hEndOfFilePlaybackEvent);
	Seekfit_By5000asiplay_CloseCard(m_uiCardNumber);
}


void CBy5000AsiplayDemoView::OnBnClickedPlay()
{
	// TODO: 在此添加控件通知处理程序代码

	m_bIsPlaying = !m_bIsPlaying;

	if (m_bIsPlaying)
	{
		SetDlgItemText(IDC_PLAY, _T("暂停"));
		Seekfit_By5000asiplay_EnableMediaFilePlayback(m_uiCardNumber);
		//		Seekfit_By5000asiplay_DisableCapturedStreamPlayback(m_uiCardNumber);
		Seekfit_By5000asiplay_MediaPlay(m_uiCardNumber);
	}
	else
	{
		SetDlgItemText(IDC_PLAY, _T("播放"));
		Seekfit_By5000asiplay_MediaPause(m_uiCardNumber);
	}
}

void CBy5000AsiplayDemoView::OnBnClickedStop()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bIsPlaying = FALSE;
	SetDlgItemText(IDC_PLAY, _T("播放"));
	Seekfit_By5000asiplay_MediaStop(m_uiCardNumber);
	Seekfit_By5000asiplay_DisableMediaFilePlayback(m_uiCardNumber);
	//	Seekfit_By5000asiplay_EnableCapturedStreamPlayback(m_uiCardNumber);
}

void CBy5000AsiplayDemoView::OnBnClickedSwitch()
{
	// TODO: 在此添加控件通知处理程序代码
	if(!m_bIsFirstFramePause)
	{
		m_bIsPlaying = TRUE;
		SetDlgItemText(IDC_PLAY, _T("暂停"));
	}
	else
	{
		m_bIsPlaying = FALSE;
		SetDlgItemText(IDC_PLAY, _T("播放"));
	}

	Seekfit_By5000asiplay_EnableMediaFilePlayback(m_uiCardNumber);
	Seekfit_By5000asiplay_MediaSwitch(m_uiCardNumber);
}

DWORD WINAPI FilePlaybackControlThread(LPVOID pBy5000AsiplayDemoView)
{
	return ((CBy5000AsiplayDemoView *)pBy5000AsiplayDemoView)->FilePlaybackControlHandler();
}

DWORD CBy5000AsiplayDemoView::FilePlaybackControlHandler()
{
	DWORD dwWaitResult;
	BOOL bResult;
	MEDIA_FILE_INFORMATION m_mfiMediaFileInformation;

	while(WaitForSingleObject(m_hExitEvent, 0) == WAIT_TIMEOUT)
	{
		//计算播出时间 最后5s开始准备待播文件 
		Sleep(1);
		Seekfit_By5000asiplay_MediaGetTimeCode(m_ulCurrentPlayTime, m_uiCardNumber);
		if (m_ulCurrentPlayDuration <=0||((m_ulCurrentPlayDuration>>8)-(m_ulCurrentPlayTime>>8)) >= 5)
		{
			Sleep(1);
			continue;
		}
		//载入待播文件 准备切换
		if (!m_strMpegFileName.IsEmpty()&&m_bSetSwtich)
		{
			if (m_bAnalyzeMediaFile)
			{
				bResult = FALSE;
				
				Seekfit_By5000asiplay_GetAnalyzedMediaFileInformation(m_strMpegFileName, 80, bResult, m_mfiMediaFileInformation, m_uiCardNumber);
				if (bResult)
				{
					bResult= FALSE;
					Seekfit_By5000asiplay_GetAnalyzedMediaFileInformation(m_strMpegFileName, 80, bResult, m_mfiMediaFileInformation, m_uiCardNumber);
					if (bResult)
					{
						AfxMessageBox(_T("素材文件存在问题，请重新转码！"));
						//return;
					}
					else
					{
						Seekfit_By5000asiplay_MediaNextSetDemultiplexerNumber(m_mfiMediaFileInformation.uiDemultiNumber,m_uiCardNumber);
					}
				}
				else
				{
					Seekfit_By5000asiplay_MediaNextSetDemultiplexerNumber(m_mfiMediaFileInformation.uiDemultiNumber,m_uiCardNumber);
				}
				//m_ulCurrentPlayDuration = m_mfiMediaFileInformation.ulTimeDuration;
			}
			Seekfit_By5000asiplay_MediaNextOpen(m_strMpegFileName, m_uiCardNumber);
			Seekfit_By5000asiplay_MediaNextGetFileInformation(m_mfiMediaFileInformation, m_uiCardNumber);
			m_ulCurrentPlayDuration = m_mfiMediaFileInformation.ulTimeDuration;
			m_bSetSwtich = FALSE;
		}
		//等待结束事件 切换节目
		dwWaitResult = WaitForSingleObject(m_hEndOfFilePlaybackEvent, INFINITE);

		switch(dwWaitResult)
		{
		case WAIT_OBJECT_0:
		
			if (!m_bAutoSwtich)
			{
				Seekfit_By5000asiplay_MediaSwitch(m_uiCardNumber);
			}
			m_bSetSwtich = TRUE;
			
//			if(!m_strMpegFileName.IsEmpty())
//			{
//				Seekfit_By5000asiplay_MediaSetDVDSubEnable(m_bIsDvdSubPause,m_uiCardNumber);
//			Seekfit_By5000asiplay_MediaNextOpen(m_strMpegFileName, m_uiCardNumber);
//				Seekfit_By5000asiplay_MediaSwitch(m_uiCardNumber);
//			}
			if(m_bIsFirstFramePause && !m_bIsExit)
			{
				m_bIsPlaying = FALSE;
				SetDlgItemText(IDC_PLAY, _T("播放"));
			}
			break;

		case WAIT_TIMEOUT:
			TRACE(_T("*************** TIMEOUT !!!!!!!!!!!!!\n"));
			break;
		default:
			TRACE(_T("*************** Anything else ?????????????\n"));
		}
	}

	return 0;
}




// CBy5000AsiplayDemoView 诊断

#ifdef _DEBUG
void CBy5000AsiplayDemoView::AssertValid() const
{
	CFormView::AssertValid();
}

void CBy5000AsiplayDemoView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CBy5000AsiplayDemoDoc* CBy5000AsiplayDemoView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBy5000AsiplayDemoDoc)));
	return (CBy5000AsiplayDemoDoc*)m_pDocument;
}
#endif //_DEBUG


// CBy5000AsiplayDemoView 消息处理程序


void CBy5000AsiplayDemoView::OnTimer(UINT nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CString str;
	UINT uiHour;
	UINT uiMinute;
	UINT uiSecond;
	UINT uiFrame;
	ULONG ulTime;

	Seekfit_By5000asiplay_MediaGetTimeCode(ulTime, m_uiCardNumber);
	uiHour = ulTime >> 24;
	uiMinute = (ulTime >> 16) & 0xff;
	uiSecond = (ulTime >> 8) & 0xff;
	uiFrame = ulTime & 0xff;
	str.Format(_T("%02d:%02d:%02d:%02d"), uiHour, uiMinute, uiSecond, uiFrame);
	m_MediaTimeCode.SetWindowText(str);

	UINT uiStatus;
	UINT uiNextStatus;
	Seekfit_By5000asiplay_MediaGetStatus(uiStatus, m_uiCardNumber);
	Seekfit_By5000asiplay_MediaNextGetStatus(uiNextStatus, m_uiCardNumber);
	if (uiStatus == MEDIA_STATUS_EMPTY)
		str = _T("空");
	else if (uiStatus == MEDIA_STATUS_PAUSE)
		str = _T("暂停");
	else if (uiStatus == MEDIA_STATUS_PLAY)
		str = _T("播放");
	else if (uiStatus == MEDIA_STATUS_STOP)
		str = _T("停止");
	else
		str = _T("空");

	SetDlgItemText(IDC_STATIC_MEDIA_STATUS, str);

	if (uiNextStatus == MEDIA_NEXT_STATUS_EMPTY)
		str = _T("空");
	else if (uiNextStatus == MEDIA_NEXT_STATUS_OPENED)
		str = _T("打开");
	else
		str = _T("空");

	SetDlgItemText(IDC_STATIC_MEDIA_NEXT_STATUS, str);
	CFormView::OnTimer(nIDEvent);
}

void CBy5000AsiplayDemoView::OnTcnSelchangeTabHoldwnd(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码

	m_dlgAudio.UpdateData(TRUE);
	m_dlgCurVideo.UpdateData(TRUE);
	m_dlgCapture.UpdateData(TRUE);
	m_dlgNextVideo.UpdateData(TRUE);
	m_dlgSysSet.UpdateData(TRUE);
	m_dlgTs.UpdateData(TRUE);

	int nCurSel = m_tabctlHoldWnd.GetCurSel(); 
	switch(nCurSel)
	{
	case 0:
		m_dlgSysSet.ShowWindow(TRUE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(FALSE);
		break;
	case 1:
		m_dlgSysSet.ShowWindow(FALSE);
		m_dlgAudio.ShowWindow(TRUE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(FALSE);
		break;
	case 2:
		m_dlgSysSet.ShowWindow(FALSE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(TRUE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(FALSE);
		break;
	case 3:
		m_dlgSysSet.ShowWindow(FALSE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(TRUE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(FALSE);
		break;
	case 4:
		m_dlgSysSet.ShowWindow(FALSE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(TRUE);
		m_dlgTs.ShowWindow(FALSE);
		break;
	case 5:
		m_dlgSysSet.ShowWindow(FALSE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(TRUE);
		break;

	default:
		m_dlgSysSet.ShowWindow(TRUE);
		m_dlgAudio.ShowWindow(FALSE);
		m_dlgCurVideo.ShowWindow(FALSE);
		m_dlgNextVideo.ShowWindow(FALSE);
		m_dlgCapture.ShowWindow(FALSE);
		m_dlgTs.ShowWindow(FALSE);

	}    
	*pResult = 0;
}


void CBy5000AsiplayDemoView::OnBnClickedButtonTest()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	UINT uiPara[4];
#ifdef UNICODE
	GetDlgItemText(IDC_EDIT_TEST_PARA1, str);
	swscanf(str, _T("%x"), &uiPara[0]);
	GetDlgItemText(IDC_EDIT_TEST_PARA2, str);
	swscanf(str, _T("%x"), &uiPara[1]);
	GetDlgItemText(IDC_EDIT_TEST_PARA3, str);
	swscanf(str, _T("%x"), &uiPara[2]);

#else

	GetDlgItemText(IDC_EDIT_TEST_PARA1, str);
	sscanf_s(str, _T("%x"), &uiPara[0]);
	GetDlgItemText(IDC_EDIT_TEST_PARA2, str);
	sscanf_s(str, _T("%x"), &uiPara[1]);
	GetDlgItemText(IDC_EDIT_TEST_PARA3, str);
	sscanf_s(str, _T("%x"), &uiPara[2]);
#endif
	

	Seekfit_By5000asiplay_Test(uiPara, 4, m_uiCardNumber);

	str.Format(_T("%x"), uiPara[3]);
	SetDlgItemText(IDC_EDIT_TEST_PARA4, str);

}

void CBy5000AsiplayDemoView::OnBnClickedResetCard()
{
	// TODO: 在此添加控件通知处理程序代码
#ifdef TEST_RESET_CARD
	if(m_bIsCardOpen)
	{
		Seekfit_By5000asiplay_CloseCard(0);
		m_bIsCardOpen = FALSE;
	}
	else
	{	
		Seekfit_By5000asiplay_OpenCard(0);		
		m_bIsCardOpen = TRUE;
	}
#endif
}
void CBy5000AsiplayDemoView::OnBnClickedCheckDvdSubEnable()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_DVD_SUB_ENABLE);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		m_bIsDvdSubPause = FALSE;
	}
	else
	{	
		m_bIsDvdSubPause = TRUE;
	}
}

void CBy5000AsiplayDemoView::OnBnClickedCheckWriteLogfile()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;

	Check = (CButton *)GetDlgItem(IDC_CHECK_WRITE_LOGFILE);
	if (Check->GetCheck() == BST_UNCHECKED)
	{	
		Seekfit_By5000asiplay_SetWriteFlag(FALSE,m_uiCardNumber);
	}
	else
	{	
		Seekfit_By5000asiplay_SetWriteFlag(TRUE,m_uiCardNumber);
	}
}

void CBy5000AsiplayDemoView::OnBnClickedCheck()
{
	// TODO: 在此添加控件通知处理程序代码
	CButton * Check;
	Check = (CButton *)GetDlgItem(IDC_CHECK__AUTOSWICTH);
	if (Check->GetCheck() == BST_CHECKED)
	{	
		m_bAutoSwtich = TRUE;
		Seekfit_By5000asiplay_MediaEnableAutoSwitch(m_uiCardNumber);

	}
	else
	{	
		m_bAutoSwtich = FALSE;
		Seekfit_By5000asiplay_MediaDisableAutoSwitch(m_uiCardNumber);
	}
}
