// PlayerTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PlayerTest.h"
#include "PlayerTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayerTestDlg dialog

CPlayerTestDlg::CPlayerTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPlayerTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPlayerTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_pVod = NULL;
	m_nPlayTimer = 0;
	m_nTestTimer = 0;
	m_bStartSlide = FALSE;
	m_szCurFileName = "";
}

void CPlayerTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPlayerTestDlg)
	DDX_Control(pDX, IDC_SLIDER_VOLUME, m_SliderVolume);
	DDX_Control(pDX, IDC_SLIDER_POS, m_SliderPos);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPlayerTestDlg, CDialog)
	//{{AFX_MSG_MAP(CPlayerTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
	ON_BN_CLICKED(IDC_BTN_PLAY, OnBtnPlay)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_BN_CLICKED(IDC_BTN_FAST, OnBtnFast)
	ON_BN_CLICKED(IDC_BTN_SLOW, OnBtnSlow)
	ON_BN_CLICKED(IDC_CHK_AUDIO, OnChkAudio)
	ON_WM_HSCROLL()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_STEP, OnBtnStep)
	ON_WM_SIZE()
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPlayerTestDlg message handlers

BOOL CPlayerTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// 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
	
	
	m_pVod = new CVideoVod();
	m_pVod->Create(
		NULL,
		NULL,
		WS_CHILD|WS_VISIBLE,//|WS_EX_TOOLWINDOW, 
		CRect(0,0,0,0), 
		this, 
		6000);
	m_pVod->SetSlipMode(1, 0);
	// TODO: Add extra initialization here
	
	GetClientRect(&m_clientRect);
	AdjWindowSize(m_clientRect);

	m_SliderVolume.SetRange(0, 100);
	m_SliderVolume.SetPos(100);

	MRPSDK_Init();
	SetPlaySpeed("");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPlayerTestDlg::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 CPlayerTestDlg::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 CPlayerTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPlayerTestDlg::OnDestroy() 
{
	if(m_nTestTimer > 0)
		KillTimer(m_nTestTimer);
	m_nTestTimer = 0;
	CDialog::OnDestroy();
	
	OnBtnStop();
	if(m_pVod)
	{
		m_pVod->DestroyWindow();
		delete m_pVod;
		m_pVod = NULL;
	}
	MRPSDK_Cleanup();
}

void CPlayerTestDlg::OnBtnOpen() 
{
	int iRet;
	CString strFilter = "Video Files(*.mp4,*.ts,*.asf,*.flv)|*.mp4|*.ts|*.asf|*.flv|All Files(*.*)|*.*|";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
		strFilter, this);
	if(dlg.DoModal() != IDOK)
		return;
	CString szFileName = dlg.GetPathName();
	iRet = StartPlay(szFileName);
	if(iRet)
	{
		AfxMessageBox("打开文件失败!");
		return;
	}
}

int CPlayerTestDlg::StartPlay(const char *szFileName)
{
	int iRet;
	CString szTime;
	OnBtnStop();
	iRet = m_pVod->vodstart(szFileName);
	if(!iRet)
	{
		//AfxMessageBox("打开文件失败!");
		return -1;
	}
	/*for(i=0; i<MRPSDK_Chn_GetStreams(m_hPlayer); i++)
	{
		LVSDK2_STREAM_T stStream;
		MRPSDK_Chn_GetStream(m_hPlayer, i, &stStream);
		if(stStream.codec_type != LVMEDIA_TYPE_VIDEO)
			continue;
		MRPSDK_Chn_SetVideoWnd(m_hPlayer, i, GetDlgItem(IDC_STC_WND)->m_hWnd);
	}*/
	iRet = MRPSDK_Chn_GetFileTime(m_pVod->m_lVodHandle);
	m_SliderPos.SetRange(0, iRet);
	m_SliderPos.SetPos(0);
	szTime.Format("%02d:%02d:%02d", iRet / 3600, (iRet % 3600) / 60, iRet % 60);
	SetDlgItemText(IDC_STC_TOLTIME, szTime);
	if(MRPSDK_Chn_SoundIsExist(m_pVod->m_lVodHandle))
	{
		MRPSDK_Chn_SoundPlay(m_pVod->m_lVodHandle, TRUE);
		m_SliderVolume.SetPos(MRPSDK_Chn_GetVolume(m_pVod->m_lVodHandle));
		m_SliderVolume.EnableWindow(TRUE);
		GetDlgItem(IDC_CHK_AUDIO)->EnableWindow(TRUE);
		((CButton*)GetDlgItem(IDC_CHK_AUDIO))->SetCheck(TRUE);
	}
	else
	{
		m_SliderVolume.EnableWindow(FALSE);
		((CButton*)GetDlgItem(IDC_CHK_AUDIO))->SetCheck(FALSE);
		GetDlgItem(IDC_CHK_AUDIO)->EnableWindow(FALSE);
	}


	m_nPlayTimer = SetTimer(10, 1000, NULL);
	m_szCurFileName = szFileName;
	return 0;
}

void CPlayerTestDlg::OnBtnPlay() 
{
	if(m_pVod->m_lVodHandle)
	{
		MRPSDK_Chn_Play(m_pVod->m_lVodHandle);
		SetPlaySpeed("1X");
	}
}

void CPlayerTestDlg::OnBtnStop() 
{
	if(m_nPlayTimer)
		KillTimer(m_nPlayTimer);
	SetPlaySpeed("");
	m_nPlayTimer = 0;
	m_pVod->StopAllChn();
	SetDlgItemText(IDC_STC_CURTIME, "00:00:00");
	SetDlgItemText(IDC_STC_TOLTIME, "00:00:00");
	m_SliderPos.SetPos(0);
}

void CPlayerTestDlg::OnBtnFast() 
{
	if(m_pVod->m_lVodHandle)
	{
		int nTempMode;
		CString szInfo;
		static int nMode = 0;
		if(nMode == 0)
			nMode = 2;
		else if(nMode == 2)
			nMode = 4;
		else if(nMode == 4)
			nMode = 8;
		else if(nMode == 8)
			nMode = 16;
		else// if(nMode == 16)
			nMode = 2;
		MRPSDK_Chn_Fast(m_pVod->m_lVodHandle, nMode);
		szInfo.Format("%dX", nMode);
		SetPlaySpeed(szInfo);
		nTempMode = MRPSDK_Chn_GetCurPlayMode(m_pVod->m_lVodHandle);
		TRACE("cur mode = %d\n", nTempMode);
	}
}

void CPlayerTestDlg::OnBtnSlow() 
{
	if(m_pVod->m_lVodHandle)
	{
		int nTempMode;
		CString szInfo;
		static int nMode = 0;
		if(nMode == 0)
			nMode = 2;
		else if(nMode == 2)
			nMode = 4;
		else if(nMode == 4)
			nMode = 8;
		else if(nMode == 8)
			nMode = 16;
		else// if(nMode == 16)
			nMode = 2;
		MRPSDK_Chn_Slow(m_pVod->m_lVodHandle, nMode);
		szInfo.Format("%dX", nMode);
		SetPlaySpeed(szInfo);
		nTempMode = MRPSDK_Chn_GetCurPlayMode(m_pVod->m_lVodHandle);
		TRACE("cur mode = %d\n", nTempMode);
	}
}

void CPlayerTestDlg::OnChkAudio() 
{
	BOOL bEnable = ((CButton*)GetDlgItem(IDC_CHK_AUDIO))->GetCheck();
	if(m_pVod->m_lVodHandle)
		MRPSDK_Chn_SoundPlay(m_pVod->m_lVodHandle, bEnable);
}

void CPlayerTestDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	if(pScrollBar == (CScrollBar*)&m_SliderPos)
	{
		CString szTime;
		m_bStartSlide = TRUE;
		nPos = m_SliderPos.GetPos();
		szTime.Format("%02d:%02d:%02d", nPos / 3600, (nPos % 3600) / 60, nPos % 60);
		SetDlgItemText(IDC_STC_CURTIME, szTime);
		if(nSBCode == SB_ENDSCROLL && m_pVod->m_lVodHandle)
		{
			MRPSDK_Chn_SeekTime(m_pVod->m_lVodHandle, nPos);
			m_bStartSlide = FALSE;
		}
	}
	else if(pScrollBar == (CScrollBar*)&m_SliderVolume)
	{
		if(m_pVod->m_lVodHandle && (nSBCode == SB_THUMBTRACK))
		{
			//nPos = m_SliderPos.GetPos();
			//TRACE("nPos:%d\n", nPos);
			MRPSDK_Chn_SetVolume(m_pVod->m_lVodHandle, nPos);
		}
		
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CPlayerTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 10)
	{
		if(m_pVod->m_lVodHandle && !m_bStartSlide)
		{
			CString szTime;
			int nTime = MRPSDK_Chn_GetCurTime(m_pVod->m_lVodHandle);
			szTime.Format("%02d:%02d:%02d", nTime / 3600, (nTime % 3600) / 60, nTime % 60);
			SetDlgItemText(IDC_STC_CURTIME, szTime);
			m_SliderPos.SetPos(nTime);
		}
	}
	else if(nIDEvent == 11)
	{
		if(!m_szCurFileName.IsEmpty())
		{
			OnBtnStop();
			StartPlay(m_szCurFileName);
		}
	}
	CDialog::OnTimer(nIDEvent);
}

void CPlayerTestDlg::OnBtnStep() 
{
	if(m_pVod->m_lVodHandle)
	{
		MRPSDK_Chn_Step(m_pVod->m_lVodHandle);
		SetPlaySpeed("Step");
	}
}

void CPlayerTestDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
	if ((cx ==0 && cy == 0) || 
		(cx == m_clientRect.Width() && cy == m_clientRect.Height())) 
	{
		return;
	}
	else if(m_pVod && m_pVod->m_hWnd)
	{
		GetClientRect(&m_clientRect);
		AdjWindowSize(m_clientRect);
	}
}

void CPlayerTestDlg::AdjWindowSize(CRect &rcClient)
{
	CRect rcWnd, rcTmp, rcBtn, rcTime, rcTime1;

	rcWnd = rcClient;
	rcWnd.bottom = rcClient.bottom - 60;
	m_pVod->MoveWindow(rcWnd);

	GetDlgItem(IDC_STC_CURTIME)->GetWindowRect(&rcTmp);
	rcTime.left = 4;
	rcTime.right= rcTime.left + rcTmp.Width();
	rcTime.top  = rcWnd.bottom + 10;
	rcTime.bottom = rcTime.top + rcTmp.Height();
	GetDlgItem(IDC_STC_CURTIME)->MoveWindow(rcTime);

	rcTime1 = rcTime;
	rcTime1.right= rcClient.right - 10;
	rcTime1.left = rcTime1.right - rcTmp.Width();
	GetDlgItem(IDC_STC_TOLTIME)->MoveWindow(rcTime1);

	m_SliderPos.GetWindowRect(&rcTmp);
	rcBtn.left  = rcTime.right + 2;
	rcBtn.right = rcTime1.left - 2;
	rcBtn.top   = rcTime.top - 2;
	rcBtn.bottom = rcBtn.top + rcTmp.Height();
	m_SliderPos.MoveWindow(rcBtn);
	
	GetDlgItem(IDC_BTN_OPEN)->GetWindowRect(&rcTmp);
	rcBtn = rcTmp;
	rcBtn.left = 4;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	rcBtn.top   = rcTime.bottom + 10;
	rcBtn.bottom = rcBtn.top + rcTmp.Height();
	GetDlgItem(IDC_BTN_OPEN)->MoveWindow(rcBtn);

	GetDlgItem(IDC_BTN_PLAY)->GetWindowRect(&rcTmp);
	rcBtn.left = rcBtn.right + 10;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	rcBtn.bottom = rcBtn.top + rcTmp.Height();
	GetDlgItem(IDC_BTN_PLAY)->MoveWindow(rcBtn);

	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	GetDlgItem(IDC_BTN_STOP)->MoveWindow(rcBtn);

	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	GetDlgItem(IDC_BTN_STEP)->MoveWindow(rcBtn);

	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	GetDlgItem(IDC_BTN_FAST)->MoveWindow(rcBtn);

	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	GetDlgItem(IDC_BTN_SLOW)->MoveWindow(rcBtn);

	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	GetDlgItem(IDC_CHK_AUDIO)->MoveWindow(rcBtn);

	GetDlgItem(IDC_STC_PLAYSPEED)->GetWindowRect(&rcTmp);
	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	rcBtn.top   += 4;
	rcBtn.bottom = rcBtn.top + rcTmp.Height();
	GetDlgItem(IDC_STC_PLAYSPEED)->MoveWindow(&rcBtn);

	m_SliderVolume.GetWindowRect(&rcTmp);
	rcBtn.left = rcBtn.right + 6;
	rcBtn.right = rcBtn.left + rcTmp.Width();
	rcBtn.top   += 1;
	rcBtn.bottom = rcBtn.top + rcTmp.Height();
	m_SliderVolume.MoveWindow(rcBtn);
}

void CPlayerTestDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_pVod->m_lVodHandle)
		m_pVod->SetFullScreen(!m_pVod->GetFullScreen());
	CDialog::OnLButtonDblClk(nFlags, point);
}
