// DlgNetFile.cpp : implementation file
//

#include "stdafx.h"
#include "playtest.h"
#include "DlgNetFile.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MYTIMER_KILL(id) if(id) {KillTimer(id); id = 0;}
#define PLAYEVENT_MSG		(WM_USER + 200)

void WINAPI s_playdownloadcallback(LONG lFileHandle,char *pbuff,int size,void *context)
{
	TRACE("download:%d\n",size);
}

/////////////////////////////////////////////////////////////////////////////
// CDlgNetFile dialog
static LONG s_hFindHandle = -1;
static LONG s_hDownHandle = -1;
static LONG s_hPlayHandle = -1;
static CDlgNetFile *s_pDlgNetFile;
static UINT s_searchTimer = 0;
static UINT s_downTimer = 0;
static UINT s_playTimer = 0;
static UINT s_playstaytimer = 0;

#define TIMERID_SEARCH		10
#define TIMERID_DOWNLOAD	11
#define TIMERID_PLAYBACK	12
#define TIMERID_PLAYSTAY	13

CDlgNetFile::CDlgNetFile(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgNetFile::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgNetFile)
	m_ctTimeStart = 0;
	m_ctTimeStop = 0;
	m_ctDateStart = 0;
	m_ctDateStop = 0;
	m_serurl = _T("192.168.2.168");
	m_serport = 3000;
	m_serchannel = 0;
	m_serfiletype = 0;
	m_netfiletype = 0;
	m_PlayStayTime = 20;
	//}}AFX_DATA_INIT

	m_bSearchDown = FALSE;
	m_bDownLoadDown = FALSE;
	s_pDlgNetFile = this;

	m_bCutStart = FALSE;
}

void CDlgNetFile::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgNetFile)
	DDX_Control(pDX, IDC_DOWNPROGRESS, m_downpos);
	DDX_Control(pDX, IDC_LISTFILE, m_listfile);
	DDX_DateTimeCtrl(pDX, IDC_TIMESTART, m_ctTimeStart);
	DDX_DateTimeCtrl(pDX, IDC_TIMESTOP, m_ctTimeStop);
	DDX_DateTimeCtrl(pDX, IDC_DATESTART, m_ctDateStart);
	DDX_DateTimeCtrl(pDX, IDC_DATESTOP, m_ctDateStop);
	DDX_Text(pDX, IDC_EDIT_SERURL, m_serurl);
	DDV_MaxChars(pDX, m_serurl, 40);
	DDX_Text(pDX, IDC_EDIT_SERPORT, m_serport);
	DDX_CBIndex(pDX, IDC_COMBO_CHAN, m_serchannel);
	DDX_CBIndex(pDX, IDC_COMBO_FILETYPE, m_serfiletype);
	DDX_CBIndex(pDX, IDC_COMBO_JEPGFILE, m_netfiletype);
	DDX_Text(pDX, IDC_EDIT_PLAYSTAY, m_PlayStayTime);
	DDV_MinMaxInt(pDX, m_PlayStayTime, 1, 20);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgNetFile, CDialog)
	//{{AFX_MSG_MAP(CDlgNetFile)
	ON_BN_CLICKED(IDC_BTNSEARCH, OnBtnsearch)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BTNDOWN, OnBtndown)
	ON_BN_CLICKED(IDC_BTNNFILEPLAY, OnBtnnfileplay)
	ON_BN_CLICKED(IDC_BTNNFILESTOP, OnBtnnfilestop)
	ON_NOTIFY(NM_DBLCLK, IDC_LISTFILE, OnDblclkListfile)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_NPLAYPAUS, OnBtnNplaypaus)
	ON_BN_CLICKED(IDC_BTN_NPLAYSTEP, OnBtnNplaystep)
	ON_BN_CLICKED(IDC_BTN_NPLAYFAST, OnBtnNplayfast)
	ON_BN_CLICKED(IDC_BTN_NPLAYSLOW, OnBtnNplayslow)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_CHECK_NFILEAUDIO, OnCheckNfileaudio)
	ON_BN_CLICKED(IDC_BTN_CUTFILE, OnBtnCutfile)
	ON_BN_CLICKED(IDC_BTNTIMEDOWN, OnBtntimedown)
	ON_MESSAGE(PLAYEVENT_MSG,   OnPlayEvent)
	ON_BN_CLICKED(IDC_BTNNTIMEPLAY, OnBtnntimeplay)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgNetFile message handlers

void CDlgNetFile::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CDlgNetFile::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

BOOL CDlgNetFile::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_listfile.InsertColumn(0, "file name", LVCFMT_LEFT, 100,-1);
	m_listfile.InsertColumn(1, "file size(M)", LVCFMT_LEFT, 100,-1);
	m_listfile.InsertColumn(2, "start time", LVCFMT_LEFT, 100,-1);
	m_listfile.InsertColumn(3, "end time", LVCFMT_LEFT, 100,-1);
	m_listfile.InsertColumn(4, "file type", LVCFMT_LEFT, 100,-1);
	m_listfile.SetExtendedStyle(LVS_EX_FULLROWSELECT);

	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetLineSize(1);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPageSize(1);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetRange(0,100);

	m_downpos.SetRange(0,100);
	m_downpos.SetPos(0);

	CTime time1 = CTime::GetCurrentTime();
	CTime time(time1.GetYear(),time1.GetMonth(),time1.GetDay(),0,0,0);
	CTime time0(time1.GetYear(),time1.GetMonth(),time1.GetDay(),23,59,59);
	m_ctDateStart = time;
	m_ctTimeStart = time;
	m_ctDateStop = time;
	m_ctTimeStop = time0;
	UpdateData(FALSE); 

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
static void WINAPI searchnotifycallback(NDVRFILE_FILEINFO *pfileinfo,void *userdata)
{
	CString tempstring;
	int  nItem = -1;
	float size;
	nItem = s_pDlgNetFile->m_listfile.InsertItem(s_pDlgNetFile->m_listfile.GetItemCount(),pfileinfo->sFileName,0);
	size  = (float)pfileinfo->dwFileSize/ (float)1024.0 / (float)1024.0;
	tempstring.Format("%.2f",size);
	s_pDlgNetFile->m_listfile.SetItemText(nItem,1,tempstring);
	tempstring.Format("%04d%02d%02d%02d%02d%02d",pfileinfo->StartTime.m_year,pfileinfo->StartTime.m_month,
        pfileinfo->StartTime.m_day,pfileinfo->StartTime.m_hour,pfileinfo->StartTime.m_minute,
        pfileinfo->StartTime.m_second);
	s_pDlgNetFile->m_listfile.SetItemText(nItem,2,tempstring);
	tempstring.Format("%04d%02d%02d%02d%02d%02d",pfileinfo->StopTime.m_year,pfileinfo->StopTime.m_month,
        pfileinfo->StopTime.m_day,pfileinfo->StopTime.m_hour,pfileinfo->StopTime.m_minute,
        pfileinfo->StopTime.m_second);
	s_pDlgNetFile->m_listfile.SetItemText(nItem,3,tempstring);
    if(pfileinfo->dwRecvType == 0x01)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"record manually");
    }
    else if(pfileinfo->dwRecvType == 0x02)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"record regularly");
    }
    else if(pfileinfo->dwRecvType == 0x04)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"Moving detection");
    }
    else if(pfileinfo->dwRecvType == 0x08)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"sensor alarm");
    }
    else if(pfileinfo->dwRecvType == 0x10)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"vedio lost");
    }
    else if(pfileinfo->dwRecvType == 0x20)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"vedio hide");
    }   
    else if(pfileinfo->dwRecvType == 0x40)
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"command trigger");
    }
    else
    {
    	s_pDlgNetFile->m_listfile.SetItemText(nItem,4,"unknown");
    }
}

void CDlgNetFile::OnBtnsearch()
{
	if(!m_bSearchDown)
	{
		char serurl[40];
        NDVRFILE_FINDINFO findinfo;
		
		CPLAY_NETTIME StartTime,StopTime;
		MYTIMER_KILL(s_searchTimer);
		UpdateData(TRUE);
		
		StartTime.m_year   = (int)m_ctDateStart.GetYear();
		StartTime.m_month  = (BYTE)m_ctDateStart.GetMonth();
		StartTime.m_day    = (BYTE)m_ctDateStart.GetDay();
		StartTime.m_hour   = (BYTE)m_ctTimeStart.GetHour();
		StartTime.m_minute = (BYTE)m_ctTimeStart.GetMinute();
		StartTime.m_second = (BYTE)m_ctTimeStart.GetSecond();
		StopTime.m_year    = (int)m_ctDateStop.GetYear();
		StopTime.m_month   = (BYTE)m_ctDateStop.GetMonth();
		StopTime.m_day     = (BYTE)m_ctDateStop.GetDay();
		StopTime.m_hour    = (BYTE)m_ctTimeStop.GetHour();
		StopTime.m_minute  = (BYTE)m_ctTimeStop.GetMinute();
		StopTime.m_second  = (BYTE)m_ctTimeStop.GetSecond();

		if(s_hFindHandle != -1)
			LCPLAYM4_nFileFindClose(s_hFindHandle);

		sprintf(serurl,"%s",m_serurl);
        memset(&findinfo,0,sizeof(NDVRFILE_FINDINFO));
        if(m_serchannel == 0)
        {
            findinfo.channel = -1;
        }
        else
        {
            findinfo.channel = m_serchannel - 1;
        }
        findinfo.dwFileType = m_netfiletype + 1;
        if(m_serfiletype == 0)
        {
            findinfo.dwRecvType = 0xff;
        }
        else if(m_serfiletype == 1)
        {
            findinfo.dwRecvType = 0x01;
        }
        else if(m_serfiletype == 2)
        {
            findinfo.dwRecvType = 0x02;
        }
        else if(m_serfiletype == 3)
        {
            findinfo.dwRecvType = 0x04;
        }
        else if(m_serfiletype == 4)
        {
            findinfo.dwRecvType = 0x08;
        }
        else if(m_serfiletype == 5)
        {
            findinfo.dwRecvType = 0x10;
        }
        else if(m_serfiletype == 6)
        {
            findinfo.dwRecvType = 0x20;
        }
        else if(m_serfiletype == 7)
        {
            findinfo.dwRecvType = 0x40;
        }
        findinfo.dwIsLocked = 0;
        findinfo.bTimelimite = ((CButton*)GetDlgItem(IDC_CHECK_TIMELIMITE))->GetCheck();
        memcpy(&findinfo.StartTime,&StartTime,sizeof(CPLAY_NETTIME));
		memcpy(&findinfo.StopTime,&StopTime,sizeof(CPLAY_NETTIME));
        
		s_hFindHandle = LCPLAYM4_nDVRFileFindFile(serurl,
			"888888","888888",&findinfo,
			searchnotifycallback,this,
			m_serport);
		if(s_hFindHandle < 0)
		{
			AfxMessageBox("obtain file list unsuccessfully!");
			return;
		}
		m_listfile.DeleteAllItems();
		GetDlgItem(IDC_BTNSEARCH)->SetWindowText("stop searching");
		m_bSearchDown = TRUE;
		GetDlgItem(IDC_STATICSTATE)->ShowWindow(SW_SHOW);

		s_searchTimer = SetTimer(TIMERID_SEARCH,200,NULL);
	}
	else
	{
		m_bSearchDown = FALSE;
		MYTIMER_KILL(s_searchTimer);
		if(s_hFindHandle != -1)
			LCPLAYM4_nFileFindClose(s_hFindHandle);
		s_hFindHandle = -1;
		
		GetDlgItem(IDC_BTNSEARCH)->SetWindowText("Search");
		m_bSearchDown = FALSE;
		GetDlgItem(IDC_STATICSTATE)->ShowWindow(SW_HIDE);
	}
}

void CDlgNetFile::OnDestroy() 
{
	if(m_bSearchDown)
		OnBtnsearch();
	if(m_bDownLoadDown)
		OnBtndown();
	OnBtnnfilestop();

	CDialog::OnDestroy();	
}

void CDlgNetFile::OnBtndown()
{
	if(!m_bDownLoadDown)
	{
		char serurl[40];

		MYTIMER_KILL(s_downTimer);

		UpdateData(TRUE);
		sprintf(serurl,"%s",m_serurl);
		char lpfilename[100];
		int  nItem = -1;
		int selno = 0;
		selno = m_listfile.GetSelectedCount();
		if(selno == 0)
		{
			AfxMessageBox("Please select file!");
			m_listfile.SetFocus();
			return;
			//selno = 0;
			//nItem = m_listcap.GetNextItem(-1, LVNI_ALL);
		}
		else
		{
			nItem = m_listfile.GetNextItem(-1, LVNI_SELECTED);
			selno = m_listfile.GetItemData(nItem);
			
		}
		m_listfile.GetItemText(nItem,0,lpfilename,sizeof(lpfilename));
		s_hDownHandle = LCPLAYM4_nFileDownLoadFileByName(serurl,"888888","888888",
			lpfilename,
			"d:\\test.mp4",
			m_serport,0,s_playdownloadcallback,this);
		m_bDownLoadDown = TRUE;
		GetDlgItem(IDC_BTNDOWN)->SetWindowText("stop download");

		m_downpos.SetPos(0);
		m_downpos.ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATEDOWN)->ShowWindow(SW_SHOW);
		
		s_downTimer = SetTimer(TIMERID_DOWNLOAD,200,NULL);
	}
	else
	{
		MYTIMER_KILL(s_downTimer);
		m_bDownLoadDown = FALSE;
		if(s_hDownHandle != -1)
			LCPLAYM4_nFileStopDownloadFile(s_hDownHandle);
		s_hDownHandle = -1;
		GetDlgItem(IDC_BTNDOWN)->SetWindowText("download");
		m_downpos.ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATEDOWN)->ShowWindow(SW_HIDE);
	}
}

void CDlgNetFile::OnBtnnfileplay() 
{
	if(s_hPlayHandle != -1) 
	{
		LCPLAYM4_Play(s_hPlayHandle,m_hWnd);
		return;
	}

	UpdateData(TRUE);
	char serurl[40];
	sprintf(serurl,"%s",m_serurl);
	char lpfilename[100];
	int  nItem = -1;
	int selno = 0;
	selno = m_listfile.GetSelectedCount();
	if(selno == 0)
	{
		AfxMessageBox("Please select file!");
		m_listfile.SetFocus();
		return;
	}
	else
	{
		nItem = m_listfile.GetNextItem(-1, LVNI_SELECTED);
		selno = m_listfile.GetItemData(nItem);

	}
	m_listfile.GetItemText(nItem,0,lpfilename,sizeof(lpfilename));
	s_hPlayHandle = LCPLAYM4_nFilePlayBackByName(serurl,"888888","888888",lpfilename,
		GetDlgItem(IDC_NETWND)->m_hWnd,
		m_serport);
	if(s_hPlayHandle == -1)
	{
		AfxMessageBox("net play failed\n");
		return;
	}
	LCPLAYM4_nFilePlayBackStayTime(s_hPlayHandle,m_PlayStayTime);
	LCPLAYM4_RegEventMsgWnd(s_hPlayHandle,m_hWnd,PLAYEVENT_MSG);
	m_bsliderpos = FALSE;
	GetDlgItem(IDC_BTNNFILESTOP)->EnableWindow(TRUE);
}

void CDlgNetFile::OnBtnnfilestop() 
{
	MYTIMER_KILL(s_playTimer);
	MYTIMER_KILL(s_playstaytimer);
	GetDlgItem(IDC_STATIC_PLAYSTAY)->ShowWindow(SW_HIDE);
	if(s_hPlayHandle != -1)
	{
		if(m_bCutStart) OnBtnCutfile();
		//LCPLAYM4_RegEventMsgWnd(s_hPlayHandle,NULL,0);
		LCPLAYM4_Stop(s_hPlayHandle);
		s_hPlayHandle = -1;
		//Sleep(100);
		
	}
	GetDlgItem(IDC_BTN_NPLAYPAUS)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_NPLAYSTEP)->EnableWindow(FALSE);
	GetDlgItem(IDC_CHECK_NFILEAUDIO)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_NPLAYFAST)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_NPLAYSLOW)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTNNFILESTOP)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTN_CUTFILE)->EnableWindow(FALSE);
	GetDlgItem(IDC_SLIDER_POS)->EnableWindow(FALSE);
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(0);
	GetDlgItem(IDC_PLAYTIME)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_FILETIME)->ShowWindow(SW_HIDE);
}

void CDlgNetFile::OnDblclkListfile(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int  nItem = -1;
	int selno = 0;
	selno = m_listfile.GetSelectedCount();
	if(selno == 0)
	{
		return;
	}
	if(s_hPlayHandle != -1) 
	{
		OnBtnnfilestop();
	}
	OnBtnnfileplay();
	*pResult = 0;
}

void CDlgNetFile::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == TIMERID_SEARCH)
	{
		if(LCPLAYM4_nFileFindGetState(s_hFindHandle) == 1)
		{
			m_bSearchDown = TRUE;
			OnBtnsearch();
		}
	}
	else if(nIDEvent == TIMERID_DOWNLOAD)
	{
		int pos;
		pos = LCPLAYM4_nFileGetDownloadPos(s_hDownHandle);
		if(pos < 0)
		{
			m_bDownLoadDown = TRUE;
			OnBtndown();
		}
		else
		{
			CString info;
			info.Format("downloading files...%d%%",pos);
			m_downpos.SetPos(pos);
			GetDlgItem(IDC_STATEDOWN)->SetWindowText(info);
		}
	}
	else if(nIDEvent == TIMERID_PLAYBACK)
	{
		if(!m_bsliderpos)
		{
            time_t gmttime;
            struct tm *localt;
            CString lpstr;
            DWORD timecc = LCPLAYM4_GetPlayedTime(s_hPlayHandle),timeset;
            if((gmttime = LCPLAYM4_GetPlayedGMTTime(s_hPlayHandle)) != 0)
            {
                localt = localtime((time_t*)&gmttime);
                if(localt)
                {
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                        localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
                    GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
                }
            }
            else
            {
                timeset = (timecc / 1000);
                localt = gmtime((time_t*)&timeset);
                if(localt)
                {
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
                    GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
                }
            }
			((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(timecc / 1000);
		}
	}
	else if(nIDEvent == TIMERID_PLAYSTAY)
	{
		CString strinfo;
		int ret;
		ret = LCPLAYM4_nFilePlayBackGetStayPos(s_hPlayHandle);
		if(ret < 0) 
		{
			MYTIMER_KILL(s_playstaytimer);
			GetDlgItem(IDC_STATIC_PLAYSTAY)->ShowWindow(SW_HIDE);
			return;
		}
		strinfo.Format("building buffer:%d%%",ret);
		GetDlgItem(IDC_STATIC_PLAYSTAY)->SetWindowText(strinfo);
	}
	CDialog::OnTimer(nIDEvent);
}

void CDlgNetFile::OnBtnNplaypaus() 
{
	if(s_hPlayHandle == -1) return;
	LCPLAYM4_Pause(s_hPlayHandle);
}

void CDlgNetFile::OnBtnNplaystep() 
{
	if(s_hPlayHandle == -1) return;
	LCPLAYM4_OneByOne(s_hPlayHandle);
}

void CDlgNetFile::OnBtnNplayfast() 
{
	if(s_hPlayHandle == -1) return;
	LCPLAYM4_Fast(s_hPlayHandle);
}

void CDlgNetFile::OnBtnNplayslow() 
{
	if(s_hPlayHandle == -1) return;
	LCPLAYM4_Slow(s_hPlayHandle);
}

void CDlgNetFile::OnPlayEvent(WPARAM wParam, LPARAM lParam)
{
	if(lParam != (LPARAM)s_hPlayHandle)
	{
		return;
	}
	if(wParam == 1) // play end
	{
		OnBtnnfilestop();
	}
	else if(wParam == 2) // net play start
	{
		DWORD filetime,firstGMTTime = 0;
        CString lpstr;

        GetDlgItem(IDC_BTN_NPLAYPAUS)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_NPLAYSTEP)->EnableWindow(TRUE);
		GetDlgItem(IDC_CHECK_NFILEAUDIO)->EnableWindow(LCPLAYM4_HaveAudio(s_hPlayHandle));
		GetDlgItem(IDC_BTN_NPLAYFAST)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_NPLAYSLOW)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_CUTFILE)->EnableWindow(TRUE);
		GetDlgItem(IDC_SLIDER_POS)->EnableWindow(TRUE);
		GetDlgItem(IDC_PLAYTIME)->SetWindowText("0000:00:00 00:00:00");
		GetDlgItem(IDC_PLAYTIME)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_FILETIME)->ShowWindow(SW_SHOW);
		
        filetime = LCPLAYM4_GetFileTime(s_hPlayHandle,&firstGMTTime) / 1000;
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetLineSize(1);
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPageSize(1);
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetRange(0,filetime);
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->SetPos(0);
		s_playTimer = SetTimer(TIMERID_PLAYBACK,1000,0);

        if(firstGMTTime > 0)
        {
            struct tm *localt;
            firstGMTTime = firstGMTTime + filetime;
            localt = localtime((time_t*)&firstGMTTime);
            if(localt)
            {
                lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                    localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_FILETIME)->SetWindowText(lpstr);
            }
        }
        else
        {
            struct tm *localt;
            localt = gmtime((time_t*)&filetime);
            if(localt)
            {
                lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
                GetDlgItem(IDC_FILETIME)->SetWindowText(lpstr);
            }
        }
	}
	else if(wParam == 3) //网络播放正在建立缓冲
	{
		int ret = 0;
		CString strinfo;
		ret = LCPLAYM4_nFilePlayBackGetStayPos(s_hPlayHandle);
		if(ret < 0) return;
		strinfo.Format("building buffer:%d%%",ret);
		GetDlgItem(IDC_STATIC_PLAYSTAY)->SetWindowText(strinfo);
		GetDlgItem(IDC_STATIC_PLAYSTAY)->ShowWindow(SW_SHOW);
		s_playstaytimer = SetTimer(TIMERID_PLAYSTAY,500,0);
	}
	else if(wParam == 4) //停止建立缓冲
	{
		//MYTIMER_KILL(s_playstaytimer);
		//GetDlgItem(IDC_STATIC_PLAYSTAY)->ShowWindow(SW_HIDE);
	}
}

void CDlgNetFile::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{	
	if(((CScrollBar*)GetDlgItem(IDC_SLIDER_POS)) == pScrollBar
		&& s_hPlayHandle != -1)
	{
		if(nSBCode == SB_THUMBTRACK || nSBCode == SB_THUMBPOSITION)
		{
            DWORD firstGMTTime = 0;
            CString lpstr;
			DWORD slidpos;
			slidpos = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->GetPos();
            LCPLAYM4_GetFileTime(s_hPlayHandle,&firstGMTTime);
            if(firstGMTTime > 0)
            {
                struct tm *localt;
                firstGMTTime += slidpos;
                localt = localtime((time_t*)&firstGMTTime);
                if(localt)
                {
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",localt->tm_year + 1900,localt->tm_mon + 1,
                        localt->tm_mday,localt->tm_hour,localt->tm_min,localt->tm_sec);
                    GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
                }
            }
            else
            {
                struct tm *localt;
                localt = gmtime((time_t*)&slidpos);
                if(localt)
                {
                    lpstr.Format("%04d:%02d:%02d %02d:%02d:%02d",00,00,00,localt->tm_hour,localt->tm_min,localt->tm_sec);
                    GetDlgItem(IDC_PLAYTIME)->SetWindowText(lpstr);
                }
            }
			m_bsliderpos = TRUE;
		}
		else if(nSBCode == SB_ENDSCROLL)
		{
			DWORD slidpos;
			slidpos = ((CSliderCtrl*)GetDlgItem(IDC_SLIDER_POS))->GetPos();
			LCPLAYM4_SetPlayedTime(s_hPlayHandle,slidpos * 1000);
			m_bsliderpos = FALSE;
		}
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CDlgNetFile::OnCheckNfileaudio() 
{
	if(s_hPlayHandle == -1) return;
	if(((CButton*)GetDlgItem(IDC_CHECK_NFILEAUDIO))->GetCheck())
	{
		LCPLAYM4_PlaySound(s_hPlayHandle);
	}
	else
	{
		LCPLAYM4_StopSound(s_hPlayHandle);
	}
}

void CDlgNetFile::OnBtnCutfile()
{	
	if(s_hPlayHandle == -1) return;
	if(m_bCutStart)
	{
		LCPLAYM4_SetFileCutEnd(s_hPlayHandle);
		GetDlgItem(IDC_BTN_CUTFILE)->SetWindowText("start cutting");
		m_bCutStart = FALSE;
		GetDlgItem(IDC_SLIDER_POS)->EnableWindow(TRUE);
	}
	else
	{
		if(!LCPLAYM4_SetFileCutStart(s_hPlayHandle,"c:\\testcut.mp4"))
		{
			AfxMessageBox("start cutting unsuccessfully!");
			return;
		}
		GetDlgItem(IDC_BTN_CUTFILE)->SetWindowText("sotp cutting");
		m_bCutStart = TRUE;
		GetDlgItem(IDC_SLIDER_POS)->EnableWindow(FALSE);
	}
}

void CDlgNetFile::OnBtntimedown() 
{
	if(!m_bDownLoadDown)
	{
		char serurl[40];
        CPLAY_NETTIME StartTime,StopTime;

		MYTIMER_KILL(s_downTimer);
		if(!UpdateData(TRUE)) return;
        if(!CheckTimeSpan(&StartTime,&StopTime))  return;

        sprintf(serurl,"%s",m_serurl);
		s_hDownHandle = LCPLAYM4_nFileDownLoadFileByTime(serurl,"888888","888888",
			m_serchannel - 1, &StartTime,&StopTime,
			"d:\\test.mp4",
			m_serport,0,s_playdownloadcallback,this);
		m_bDownLoadDown = TRUE;
		GetDlgItem(IDC_BTNTIMEDOWN)->SetWindowText("stop download");

		m_downpos.SetPos(0);
		m_downpos.ShowWindow(SW_SHOW);
		GetDlgItem(IDC_STATEDOWN)->ShowWindow(SW_SHOW);
		
		s_downTimer = SetTimer(TIMERID_DOWNLOAD,200,NULL);
	}
	else
	{
		MYTIMER_KILL(s_downTimer);
		m_bDownLoadDown = FALSE;
		if(s_hDownHandle != -1)
			LCPLAYM4_nFileStopDownloadFile(s_hDownHandle);
		s_hDownHandle = -1;
		GetDlgItem(IDC_BTNTIMEDOWN)->SetWindowText("time download");
		m_downpos.ShowWindow(SW_HIDE);
		GetDlgItem(IDC_STATEDOWN)->ShowWindow(SW_HIDE);
	}	
}
BOOL CDlgNetFile::CheckTimeSpan(CPLAY_NETTIME *StartTime,CPLAY_NETTIME *StopTime) 
{
    if(m_serchannel <= 0)
    {
        AfxMessageBox("Please select a channel");
        GetDlgItem(IDC_COMBO_CHAN)->SetFocus();
        return FALSE;
    }
    StartTime->m_year   = (int)m_ctDateStart.GetYear();
    StartTime->m_month  = (BYTE)m_ctDateStart.GetMonth();
    StartTime->m_day    = (BYTE)m_ctDateStart.GetDay();
    StartTime->m_hour   = (BYTE)m_ctTimeStart.GetHour();
    StartTime->m_minute = (BYTE)m_ctTimeStart.GetMinute();
    StartTime->m_second = (BYTE)m_ctTimeStart.GetSecond();
    StopTime->m_year    = (int)m_ctDateStop.GetYear();
    StopTime->m_month   = (BYTE)m_ctDateStop.GetMonth();
    StopTime->m_day     = (BYTE)m_ctDateStop.GetDay();
    StopTime->m_hour    = (BYTE)m_ctTimeStop.GetHour();
    StopTime->m_minute  = (BYTE)m_ctTimeStop.GetMinute();
    StopTime->m_second  = (BYTE)m_ctTimeStop.GetSecond();
    
    CTime starttime(StartTime->m_year,StartTime->m_month,StartTime->m_day,StartTime->m_hour,StartTime->m_minute,StartTime->m_second);
    CTime stoptime(StopTime->m_year,StopTime->m_month,StopTime->m_day,StopTime->m_hour,StopTime->m_minute,StopTime->m_second);
    CTimeSpan spantime =  stoptime - starttime;
    if(spantime < 0 || spantime > 7200)
    {
        AfxMessageBox("interval time must less than 2 hours");
        GetDlgItem(IDC_DATESTART)->SetFocus();
        return FALSE;
    }
    return TRUE;
}

void CDlgNetFile::OnBtnntimeplay() 
{
	if(s_hPlayHandle != -1) 
	{
		LCPLAYM4_Play(s_hPlayHandle,m_hWnd);
		return;
	}

    CPLAY_NETTIME StartTime,StopTime;
	char serurl[40];

	if(!UpdateData(TRUE))
    {
        return;
    }
    if(!CheckTimeSpan(&StartTime,&StopTime))  return;

	sprintf(serurl,"%s",m_serurl);
	s_hPlayHandle = LCPLAYM4_nFilePlayBackByTime(serurl,"888888","888888",m_serchannel - 1,
        &StartTime,&StopTime,
		GetDlgItem(IDC_NETWND)->m_hWnd,
		m_serport);
	if(s_hPlayHandle == -1)
	{
		AfxMessageBox("net time play failed\n");
		return;
	}
	LCPLAYM4_nFilePlayBackStayTime(s_hPlayHandle,m_PlayStayTime);
	LCPLAYM4_RegEventMsgWnd(s_hPlayHandle,m_hWnd,PLAYEVENT_MSG);
	m_bsliderpos = FALSE;
	GetDlgItem(IDC_BTNNFILESTOP)->EnableWindow(TRUE);	
}
