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

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "RecordDlg.h"
#include "RecordEx.h"
#include "NetSDKDemoDlg.h"
#include "tinyxml.h"
#include "Process.h"
#include "DllPlayer.h"

#define PLAYSTATUS_NULL _T("")
#define PLAYSTATUS_PLAY _T("正在播放")
#define PLAYSTATUS_OVER _T("播放完成")
#define PLAYSTATUS_PAUSE _T("暂停播放")
#define PLAYSTATUS_STOP _T("停止")
#define PLAYSTATUS_FAST _T("快进")
#define PLAYSTATUS_SLOW _T("快退")

#define FILE_LOCAL _T("本地文件")
#define FILE_REMOTE _T("远程文件")
struct NetSatateEvent
{
	int iEvent;
	CString strDescribe;
};

static const int g_iMaxEvent = 43;
static NetSatateEvent g_strEvent[43] =
{
	{EVENT_CONNECTING, _T("连接中")},
	{EVENT_CONNECTOK, _T("连接成功")},
	{EVENT_CONNECTFAILED, _T("连接失败")},
	{EVENT_SOCKETERROR, _T("sock失败")},
	{EVENT_LOGINOK, _T("登录成功")},
	{EVENT_LOGINFAILED, _T("登录失败")},
	{EVENT_STARTAUDIOOK, _T("对讲启动成功")},
	{EVENT_STARTAUDIOFAILED, _T("对讲启动失败")},
	{EVENT_STOPAUDIOOK, _T("停止对讲成功")},
	{EVENT_STOPAUDIOFAILED, _T("停止对讲失败")},
	{EVENT_SENDPTZOK, _T("发送云台控制命令成功")},
	{EVENT_SENDPTZFAILED, _T("发送云台控制命令失败")},
	{EVENT_SENDAUXOK, _T("发送辅助通道成功")},
	{EVENT_SENDAUXFAILED, _T("发送辅助通道失败")},
	{EVENT_UPLOADOK, _T("上传文件成功")},
	{EVENT_UPLOADFAILED, _T("上传文件失败")},
	{EVENT_DOWNLOADOK , _T("下载成功")},
	{EVENT_DOWNLOADFAILED , _T("下载失败")},
	{EVENT_REMOVEOK , _T("删除文件成功")},
	{EVENT_REMOVEFAILED , _T("删除文件失败")},
	{EVENT_SENDPTZERROR , _T("云台操作失败")},
	{EVENT_PTZPRESETINFO, _T("预置点信息")},
	{EVNET_PTZNOPRESETINFO, _T("没有预置点")},
	{EVENT_PTZALARM, _T("告警信息")},
	{EVENT_RECVVIDEOPARAM, _T("视频参数返回")},
	{EVENT_RECVAUDIOPARAM, _T("音频参数返回")},
	{EVENT_CONNECTRTSPERROR, _T("请求实时视频失败")},
	{EVENT_CONNECTRTSPOK, _T("读求媒体流成功")},
	{EVENT_RTSPTHREADEXIT, _T("媒体流线程退出")},
	{EVENT_URLERROR, _T("解释连接媒体流URL时失败")},
	{EVENT_RECVVIDEOAUDIOPARAM, _T("收到音视频参数")},
	{EVENT_LOGIN_USERERROR, _T("登录用户或密码错误")},
	{EVENT_LOGOUT_FINISH, _T("登录线程已退出主循环")},
	{EVENT_LOGIN_RECONNECT, _T("进行重新登录")},
	{EVENT_LOGIN_HEARTBEAT_LOST, _T("心跳丢失")},
	{EVENT_STARTAUDIO_ISBUSY, _T("设备对讲被占用")},
	{EVENT_STARTAUDIO_PARAMERROR, _T("对讲音频参数错误")},
	{EVENT_STARTAUDIO_AUDIODDISABLED, _T("设备不支持对讲")},
	{EVENT_CONNECT_RTSPSERVER_ERROR, _T("连接错误，可能是IP或端口错误")},
	{EVENT_CREATE_RTSPCLIENT_ERROR, _T("创建rtsp客户端错误")},
	{EVENT_GET_RTSP_CMDOPTION_ERROR, _T("连接rtsp服务器，返回错误")},
	{EVENT_RTSP_AUTHERROR, _T("认证失败，可能是用户或密码错误")},
	{-1, NULL}
};

// 前端回放事件回调
LONG __stdcall CRecordDlg::PlayActionEventCallBack(LONG lUser,LONG nType,LONG nFlag,char * pData,void * pUser)
{
	CRecordDlg *pRecordDlg = (CRecordDlg *)pUser; 
#if 0
 	if (ACTION_STOP == nType)
 	{
		CDevInfo *pDevInfo = NULL;
		POSITION pos = theApp.m_DevList.GetStartPosition();
		while(pos)
		{
			CDevInfo *pDevInfo = NULL;
			CString strKey;
			theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
			if (pDevInfo->iUserID == lUser)
			{
				if (pDevInfo->m_pRemotePlayBack != NULL)
				{
					Sleep(700);
					((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndManager->SetWndUser(pDevInfo->m_pRemotePlayBack->m_hWnd, NULL);
					SAFE_DELETE(pDevInfo->m_pRemotePlayBack);		
				}
				
				break;
			}
		}

 	}
#endif
	return 0;
}

LONG __stdcall CRecordDlg::RecordDataCallBack(LONG lRealHandle,DWORD dwDataType,BYTE *pBuffer,DWORD dwBufSize,LPFRAME_EXTDATA  pExtData)
{
	CRecordDlg *pRecordDlg = (CRecordDlg *)pExtData->pUserData;

	if (dwDataType == 0)
	{
		// Video
		long lPlayerID = 0;
		CString strKey;

		{
			CMapAutoLock MapLock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->iUserID == lRealHandle)
				{
					if (pDevInfo->m_pRemotePlayBack)
						lPlayerID = pDevInfo->m_pRemotePlayBack->m_lPlayID;

					if (NULL == pDevInfo->m_pRemotePlayBack || (pDevInfo->m_pRemotePlayBack && pDevInfo->m_pRemotePlayBack->m_playState != 1))
					{
						return -2;
					}
					break;
				}
			}
		}

		if (lPlayerID > 0 )
		{
			//lionel modify @ 2012-11-9
			
			long lInputErr = IP_TPS_InputVideoData(lPlayerID, pBuffer+sizeof(UpdPackHead), dwBufSize-sizeof(UpdPackHead), pExtData->bIsKey, pExtData->timestamp);
			if (lInputErr != 0)
				TRACE("IP_TPS_InputVideoData failed!!!!!\n"); 
		}
	}

	else if(dwDataType==1)
	{
		return 0l;
		// Audio
		long lPlayerID = 0;
		CString strKey;
		{
			CMapAutoLock MapLock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);

				if (pDevInfo->iUserID == lRealHandle)
				{
					lPlayerID = pDevInfo->m_pRemotePlayBack->m_lPlayID;
					
					if(1 != pDevInfo->m_pRemotePlayBack->m_playState)
					{
						return -2;
					}

					break;
				}
			}
		}
		
		if (lPlayerID > 0)
		{
			long lInputErr = IP_TPS_InputAudioData(lPlayerID, pBuffer+sizeof(UpdPackHead), dwBufSize-sizeof(UpdPackHead), pExtData->timestamp);
			if (lInputErr != 0) 
			{
				TRACE("IP_TPS_InputAudioData failed  lInputErr = %d!!!!!\n", lInputErr);
			}
		}
	}

	else if(dwDataType==2)
	{
		STREAM_AV_PARAM *pavParam = (STREAM_AV_PARAM *)pBuffer;

		BOOL bNew = TRUE;

		long lPlayerID = 0;
		HWND hWnd = NULL;
		CString strKey;
		{
			CMapAutoLock Lock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->iUserID == lRealHandle)
				{
					if (pDevInfo->m_pRemotePlayBack != NULL)
					{
						lPlayerID = pDevInfo->m_pRemotePlayBack->m_lPlayID;
						hWnd = pDevInfo->m_pRemotePlayBack->m_hWnd;
					}
					
					break;
				}
			}
		}

		if ( lPlayerID <= 0)
		{
			srand( (unsigned)time( NULL ) );
			int RANGE_MIN = 1;
			int RANGE_MAX = 9999;
			lPlayerID = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
		}

		if (pavParam->bHaveVideo)
		{
			int isAudioParam = 0;
			int nBufPoolSize = 40;
			long lRet = IP_TPS_OpenStream(lPlayerID, (BYTE *)&(pavParam->videoParam), sizeof(VIDEO_PARAM), isAudioParam, nBufPoolSize);
			if (lRet != 0)
			{
				TRACE("Video IP_TPS_OpenStream faild!!!\n");
				return 0;
			}
		}

// 		if (pavParam->bHaveAudio)
// 		{
// 			int isAudioParam = 1;
// 			int nBufPoolSize = 40;
// 			long lRet = IP_TPS_OpenStream(lPlayerID, (BYTE *)&(pavParam->audioParam), sizeof(AUDIO_PARAM), isAudioParam, nBufPoolSize);
// 			if (lRet != 0)	TRACE("Audio IP_TPS_OpenStream faild!!!\n");// 发送到日志对话框
// 		}

		{
			CMapAutoLock MapLock;
			CDevInfo *pDevInfo =NULL;
			BOOL bFound = theApp.m_DevList.Lookup(strKey, pDevInfo);
			if (bFound && NULL != pDevInfo->m_pRemotePlayBack)
			{
				pDevInfo->m_pRemotePlayBack->m_lPlayID = lPlayerID;
			}
		}

		int iPlay = 0;
		if (lPlayerID > 0 && NULL != hWnd)
		{
			iPlay = IP_TPS_Play(lPlayerID, hWnd);
		}
		
		TRACE("iPlay = %d, lPlayerID = %d\n", iPlay, lPlayerID);
	}
	return 0;
}
// CRecordDlg 对话框

IMPLEMENT_DYNAMIC(CRecordDlg, CDialog)

CRecordDlg::CRecordDlg(CRect rcWnd, CWnd* pParent /*=NULL*/)
	: CDialog(CRecordDlg::IDD, pParent)
	,m_rcClient(0, 0, rcWnd.Width(), rcWnd.Height())
{
	
	m_iFilePage = 0;
	m_bDownLoad = FALSE;
	m_hHandel = NULL;
	m_uiThreadID = 0;
	m_bLogQuery = FALSE;
	m_strFileDevIP = _T("");

	m_eFastSpeed = SPEED_NOMAL;
	m_eSlowSpeed = SPEED_NOMAL;

	m_iPlayState = 0;

	InitializeCriticalSection(&m_csList);
}

CRecordDlg::~CRecordDlg()
{

	::PostThreadMessage(m_uiThreadID, WM_QUIT, 0, 0);
	if (WaitForSingleObject(m_hHandel, 500) == WAIT_TIMEOUT)
	{
		TerminateThread(m_hHandel, -1 );
	}
	CloseHandle(m_hHandel);
	m_hHandel = NULL;

// 	EnterCriticalSection(&m_csList);
// 	while(!m_listDLTask.IsEmpty())
// 	{
// 		DownLoadTask *pTask = m_listDLTask.RemoveHead();
// 		SAFE_DELETE(pTask);
// 	}
// 	LeaveCriticalSection(&m_csList);

	DeleteCriticalSection(&m_csList);

	DestroyWindow();
}

void CRecordDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_RECORDLIST, m_ctlRecordList);
	DDX_Control(pDX, IDC_STATIC_IP, m_ctlDevIP);
	DDX_Control(pDX, IDC_BTN_DOWNLOAD, m_ctlBTNDownLoad);
	DDX_Control(pDX, IDC_BTN_PAGEUP, m_ctlBTNPageUP);
	DDX_Control(pDX, IDC_BTN_PAGEDOWN, m_ctlBTNPageDown);
	DDX_Control(pDX, IDC_BUTTON_PAUSE, m_btnPause);
	DDX_Control(pDX, IDC_BUTTON_FAST, m_btnFast);
	DDX_Control(pDX, IDC_BUTTON_SLOW, m_btnSlow);
	//	DDX_Control(pDX, IDC_COMBO1, m_cbxFast);
	// 	DDX_Control(pDX, IDC_COMBO2, m_cbxSlow);
	DDX_Control(pDX, IDC_BUTTON_RESUME_SPEED, m_btnReSpeed);
}


BEGIN_MESSAGE_MAP(CRecordDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_QUERY, &CRecordDlg::OnBnClickedBtnQuery)
	ON_BN_CLICKED(IDC_BTN_DOWNLOAD, &CRecordDlg::OnBnClickedBtnDownload)
	ON_BN_CLICKED(IDC_BTN_PAGEUP, &CRecordDlg::OnBnClickedBtnPageup)
	ON_BN_CLICKED(IDC_BTN_PAGEDOWN, &CRecordDlg::OnBnClickedBtnPagedown)
	ON_WM_TIMER()
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_RECORDLIST, &CRecordDlg::OnNMDblclkListRecordlist)
	ON_BN_CLICKED(IDC_BTN_ADDLOCFILE, &CRecordDlg::OnBnClickedBtnAddlocfile)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, &CRecordDlg::OnBnClickedButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_FAST, &CRecordDlg::OnBnClickedButtonFast)
	ON_BN_CLICKED(IDC_BUTTON_SLOW, &CRecordDlg::OnBnClickedButtonSlow)
	ON_BN_CLICKED(IDC_BUTTON_RESUME_SPEED, &CRecordDlg::OnBnClickedButtonResumeSpeed)
END_MESSAGE_MAP()


// CRecordDlg 消息处理程序

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

	// TODO:  在此添加额外的初始化
	m_ctlRecordList.MoveWindow(CRect(0, 0, m_rcClient.Width()-150, m_rcClient.Height()));
	
	DWORD dwStyle = GetWindowLong(m_ctlRecordList.m_hWnd, GWL_STYLE);
	dwStyle &=~(LVS_TYPEMASK);
	dwStyle &=~(LVS_EDITLABELS);

	SetWindowLong(m_ctlRecordList.m_hWnd, GWL_STYLE, dwStyle|LVS_REPORT|LVS_NOLABELWRAP|LVS_SHOWSELALWAYS);
	DWORD dwStyles = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES;
	m_ctlRecordList.SetExtendedStyle( m_ctlRecordList.GetExtendedStyle()|dwStyles);
	
	m_hHandel = (HANDLE)_beginthreadex(NULL, 0, RecordProcessThread, this, 0, &m_uiThreadID);
	EnterCriticalSection(&m_csList);
	int i = m_ctlRecordList.InsertColumn(0, _T("文件路径"),LVCFMT_CENTER,350);
	m_ctlRecordList.InsertColumn(1, _T("文件大小"), LVCFMT_CENTER, 100);
	m_ctlRecordList.InsertColumn(2, _T("下载状态"), LVCFMT_CENTER, 100);
	m_ctlRecordList.InsertColumn(3, _T("播放状态"), LVCFMT_LEFT, 100);
	m_ctlDevIP.SetWindowText("");
	LeaveCriticalSection(&m_csList);

	// 设置回放流回调
	IP_NET_DVR_SetReplayDataCallBack(RecordDataCallBack, this);

	// 设置前端文件回放事件回调
	IP_NET_DVR_SetPlayActionEventCallBack(PlayActionEventCallBack, this);

	UpdateUI();
#if 0
	m_cbxFast.AddString(_T("0X"));
	m_cbxFast.AddString(_T("1X"));
	m_cbxFast.AddString(_T("2X"));
	m_cbxFast.AddString(_T("3X"));
	m_cbxFast.AddString(_T("4X"));
	m_cbxFast.AddString(_T("5X"));
	m_cbxFast.AddString(_T("6X"));
	m_cbxFast.AddString(_T("7X"));
	m_cbxFast.AddString(_T("8X"));

	m_cbxFast.SetCurSel(1);

	m_cbxSlow.AddString(_T("0X"));
	m_cbxSlow.AddString(_T("1X"));
	m_cbxSlow.AddString(_T("2X"));
	m_cbxSlow.AddString(_T("3X"));
	m_cbxSlow.AddString(_T("4X"));
	m_cbxSlow.AddString(_T("5X"));
	m_cbxSlow.AddString(_T("6X"));
	m_cbxSlow.AddString(_T("7X"));
	m_cbxSlow.AddString(_T("8X"));
	m_cbxSlow.SetCurSel(1);
#endif
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


BOOL CRecordDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	
	if (EVENT_DOWNLOADOK == pMsg->message || EVENT_DOWNLOADFAILED == pMsg->message)
	{
		EnterCriticalSection(&m_csList);
		if (m_listDLTask.IsEmpty())
		{
			return TRUE;
		}
		DLoadTask dTask = m_listDLTask.RemoveHead();
		LeaveCriticalSection(&m_csList);
		m_ctlRecordList.SetItemText(dTask.iItemIndex, 2, EVENT_DOWNLOADOK == pMsg->message ? "完成" : "失败");


		if (!m_listDLTask.IsEmpty())
		{
			GetFile();
		}
		else
		{
			m_bDownLoad = FALSE;
			m_ctlBTNDownLoad.SetWindowText("下载");
			GetDlgItem(IDC_BTN_QUERY)->EnableWindow(TRUE); 

		}
		return TRUE;
	}
	
	
	
	if (pMsg->message == WM_FILE_DOWNLOAD)
	{
		char *pRecordFile = NULL;
		char *pStrIP = NULL;
		{
			CMapAutoLock Lock;
			POSITION pos = theApp.m_DevList.GetStartPosition();
			while (pos)
			{
				CString strKey;
				CDevInfo *pDevInfo = NULL;
				theApp.m_DevList.GetNextAssoc(pos, strKey, pDevInfo);
				if (pDevInfo->iUserID == pMsg->wParam)
				{	
					if (pMsg->lParam == CMD_GET_RECORD_FILE_LIST)
						pRecordFile = pDevInfo->pRecordFileList;
					else
						pRecordFile = pDevInfo->pLogFileList;

					pDevInfo->pRecordFileList = NULL;	
					pDevInfo->pLogFileList = NULL;
					pStrIP = strdup(pDevInfo->m_IPCEntry.lanCfg.IPAddress);
					break;
				}
			}
		}
		
		CString strIP;
		m_ctlDevIP.GetWindowText(strIP);
		//if (strIP.Compare(pStrIP) == 0)
		{
#if 0
			char pRFile[] = "<RESPONSE_PARAM>\n<RECORD_FILE\nFilePath=\"/mnt/mmc0/motion/2009/04/01/103222-av-1.avi\"\nFileLength=\"14832236\"\n/>\n\
							<RECORD_FILE\nFilePath=\"/mnt/mmc0/motion/2009/04/01/103107-av-1.avi\"\n\
							FileLength=\"14463472\"\n/>\n</RESPONSE_PARAM>\n"; 
#endif
			m_strFileDevIP = strIP;
			ParseFileList(pRecordFile);
			int iItemCount = m_ctlRecordList.GetItemCount();
			if (iItemCount <= 0 && m_iFilePage > 1)
			{
				m_iFilePage -= 1;
			}
		}
		
		SAFE_FREE(pStrIP);
		SAFE_FREE(pRecordFile);
		return TRUE;
	}
	
	else if (pMsg->message == WM_DOWNLOAD_POS)
	{
		CString strPos;
		strPos.Format("下载中..%d..", pMsg->lParam);
		m_ctlRecordList.SetItemText(pMsg->wParam, 2, strPos);
	}
#if 0
	else if (pMsg->message == WM_DOWNLOAD_STATUS)
	{
		CString strStatus;
		if (pMsg->lParam == EVENT_DOWNLOADOK)
		{
			strStatus.Format("%s", g_strEvent[EVENT_DOWNLOADOK].strDescribe);
			m_ctlRecordList.SetItemText(pMsg->wParam, 2, strStatus);
		}
		else	if (pMsg->lParam == EVENT_DOWNLOADFAILED)
		{
			strStatus.Format("%s", g_strEvent[EVENT_DOWNLOADFAILED].strDescribe);
			m_ctlBTNDownLoad.SetWindowText(_T("下载"));
			m_ctlRecordList.SetItemText(pMsg->wParam, 2, strStatus);
			m_bDownLoad = FALSE;
		}
		else if (pMsg->lParam == WM_DOWNLOAD_START)
		{
			m_ctlBTNDownLoad.SetWindowText(_T("停止"));
			m_ctlRecordList.SetItemText(pMsg->wParam, 2, strStatus);
			m_bDownLoad = TRUE;
		}
		else
		{
			strStatus.Format("%s", g_strEvent[EVENT_DOWNLOADOK].strDescribe);
			m_ctlBTNDownLoad.SetWindowText(_T("下载"));
			m_bDownLoad = FALSE;
		}	
	}

#endif

	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 CRecordDlg::ParseFileList(char *pXMLBuffer)
{
	_bstr_t logfilelist(pXMLBuffer);
		
	TiXmlDocument doc;
	doc.Parse(pXMLBuffer);
	if(!doc.Error())
	{
		int logfilecount=0;
		TiXmlElement * pItem=doc.RootElement()->FirstChildElement();
		EnterCriticalSection(&m_csList);
		m_ctlRecordList.DeleteAllItems();
		LeaveCriticalSection(&m_csList);
		int idx=0;
		while(pItem)
		{
			EnterCriticalSection(&m_csList);
			const char * filename=pItem->Attribute("FilePath");
			int itemId=m_ctlRecordList.InsertItem(idx++,_bstr_t(filename));
			const char * filesize=pItem->Attribute("FileLength");
			m_ctlRecordList.SetItemText(itemId,1,_bstr_t(filesize)); 
			pItem=pItem->NextSiblingElement();
			LeaveCriticalSection(&m_csList);
		}
	}
}

void CRecordDlg::OnBnClickedBtnQuery()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_bDownLoad) 
	{
		return ;
	}

	CRecordEx *pRecordEx = ((CNetSDKDemoDlg *)GetParent())->m_pRecordEx;
	CString strKey;
	m_ctlDevIP.GetWindowText(strKey);

	if (strKey.GetLength() <= 0)
	{
		MessageBox(_T("请先选择左边树形表中的设备"), _T("提示"), MB_OK);
		return;
	}
	m_iFilePage = 0;

	pRecordEx->Query(strKey, m_iFilePage);

}

int CRecordDlg::GetFile()
{
	EnterCriticalSection(&m_csList);
	m_hTask = m_listDLTask.GetHead();
	LeaveCriticalSection(&m_csList);
	long lRet = IP_NET_DVR_GetFileByName(m_hTask.pDevInfo->iUserID, m_hTask.iFileType, m_hTask.FileName, m_hTask.SavePath);
	if (0 == lRet)
	{
		m_bDownLoad = TRUE;
		m_ctlBTNDownLoad.SetWindowText("停止");
		GetDlgItem(IDC_BTN_QUERY)->EnableWindow(FALSE); 
	}

	return 0;
}

int CRecordDlg::BeginDownLoad()
{
	int iItemCount = m_ctlRecordList.GetItemCount();
	for (int i=0; i<iItemCount; i++)
	{
		if (m_ctlRecordList.GetCheck(i))
		{
			m_ctlRecordList.SetItemText(i, 2, _T(""));
		}
	}

	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(m_strFileDevIP, pDevInfo);
	if (NULL == pDevInfo)
	{
		return -1;
	}

	m_listDLTask.RemoveAll();

	for (int i=0; i< m_ctlRecordList.GetItemCount(); i++)
	{
		if (m_ctlRecordList.GetCheck(i))
		{
			DLoadTask dTask = {0};
			dTask.iItemIndex = i;

			dTask.iFileType = m_bLogQuery ? 0 : 1;

			dTask.pDevInfo = pDevInfo;

			CString strTmp = m_ctlRecordList.GetItemText(i, 0);
			strcpy(dTask.FileName, strTmp.GetBuffer());

			((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pRecordEx->m_ctlSavePath.GetWindowText(strTmp);
			strcpy(dTask.SavePath, strTmp.GetBuffer());

			m_listDLTask.AddTail(dTask);
		}
	}
	
	if (!m_listDLTask.IsEmpty())
	{
		GetFile();
	}
	
	return 0;
}

int CRecordDlg::StopDownLoad()
{
	m_ctlBTNDownLoad.SetWindowText("下载");
	m_bDownLoad = FALSE;
	GetDlgItem(IDC_BTN_QUERY)->EnableWindow(TRUE);

	EnterCriticalSection(&m_csList);
	if (!m_listDLTask.IsEmpty())
	{
		m_hTask = m_listDLTask.GetHead();
		IP_NET_DVR_StopGetFile(m_hTask.pDevInfo->iUserID);
	}
	
	if (!m_listDLTask.IsEmpty())
	{
		m_listDLTask.RemoveAll();
	}

	LeaveCriticalSection(&m_csList);
	
	return 0;
}

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

	if (!m_bDownLoad)
	{
		BeginDownLoad();
	}
	else
	{
		StopDownLoad();
	}
}

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

	if (m_iFilePage <= 0) 
	{
		AfxMessageBox("已经是最顶页了");
		return ;
	}
	if (m_bDownLoad)
	{
		return ;
	}
	
	CRecordEx *pRecordEx = ((CNetSDKDemoDlg *)GetParent())->m_pRecordEx;
	m_iFilePage -= 1;
	pRecordEx->Query(m_strFileDevIP, m_iFilePage);

}

void CRecordDlg::OnBnClickedBtnPagedown()
{
	// TODO: 在此添加控件通知处理程序代码
	
	if (m_bDownLoad)
	{
		return ;
	}

	CRecordEx *pRecordEx = ((CNetSDKDemoDlg *)GetParent())->m_pRecordEx;

	m_iFilePage += 1;
	pRecordEx->Query(m_strFileDevIP, m_iFilePage);
}

void CRecordDlg::UpdateUI()
{
	
	switch(m_iPlayState)
	{
	case 0: //停止
		{
			m_btnPause.SetWindowText(_T("恢复"));
			m_btnPause.EnableWindow(FALSE);
			m_btnReSpeed.EnableWindow(FALSE);
			m_btnFast.EnableWindow(FALSE);
			m_btnSlow.EnableWindow(FALSE);
			break;
		}
		
	case 1://播放
		{
			m_btnPause.SetWindowText(_T("暂停"));
			m_btnPause.EnableWindow(TRUE);
			m_btnReSpeed.EnableWindow(TRUE);
			m_btnFast.EnableWindow(TRUE);
			m_btnSlow.EnableWindow(TRUE);
			break;
		}
		
	case 2://暂停
		{
			m_btnPause.SetWindowText(_T("恢复"));
			m_btnPause.EnableWindow(TRUE);
			m_btnReSpeed.EnableWindow(TRUE);
			m_btnFast.EnableWindow(TRUE);
			m_btnSlow.EnableWindow(TRUE);
			break;
		}
		
	default:
		{
			m_btnPause.SetWindowText(_T("恢复"));
			m_btnPause.EnableWindow(FALSE);
			m_btnReSpeed.EnableWindow(FALSE);
			m_btnFast.EnableWindow(FALSE);
			m_btnSlow.EnableWindow(FALSE);
			break;
		}

	}

	switch(m_eFastSpeed)
	{
	case SPEED_NOMAL:
		{
			m_btnFast.SetWindowText(_T("快放"));
			break;
		}
	case SPEED_2X:
		{
			m_btnFast.SetWindowText(_T("快放2X"));
			break;
		}
	case SPEED_4X:
		{
			m_btnFast.SetWindowText(_T("快放4X"));
			break;
		}
	case SPEED_8X:
		{
			m_btnFast.SetWindowText(_T("快放8X"));
			break;
		}
	case SPEED_16X:
		{
			m_btnFast.SetWindowText(_T("快放16X"));
			break;
		}
	default:
		{
			m_btnFast.SetWindowText(_T("快放"));
			break;
		}
	}

	switch(m_eSlowSpeed)
	{
	case SPEED_NOMAL:
		{
			m_btnSlow.SetWindowText(_T("慢放"));
			break;
		}
	case SPEED_2X:
		{
			m_btnSlow.SetWindowText(_T("慢放2X"));
			break;
		}
	case SPEED_4X:
		{
			m_btnSlow.SetWindowText(_T("慢放4X"));
			break;
		}
	case SPEED_8X:
		{
			m_btnFast.SetWindowText(_T("慢放8X"));
			break;
		}
	case SPEED_16X:
		{
			m_btnSlow.SetWindowText(_T("慢放16X"));
			break;
		}
	default:
		{
			m_btnSlow.SetWindowText(_T("慢放"));
			break;
		}
	}
	return;
}

void CRecordDlg::SetSelIP(CString &strIP)
{
	if (!m_bDownLoad)
	{
		m_ctlDevIP.SetWindowText(strIP);
	}
}
void CRecordDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	switch (nIDEvent)
	{
	case WM_TIMER_RECORD:
		//IP_NET_DVR_GetDownloadPos()
		break;
	}
	CDialog::OnTimer(nIDEvent);
}


unsigned int __stdcall CRecordDlg::RecordProcessThread(void *pUser)
{
	CRecordDlg *pThis = (CRecordDlg *)pUser;
	CString strFinish;
	while (1)
	{
		while(!pThis->m_bDownLoad)
		{
			Sleep(100);
		}
		
		EnterCriticalSection(&pThis->m_csList);
		DLoadTask dTask = pThis->m_hTask;
		LeaveCriticalSection(&pThis->m_csList);

		if (dTask.pDevInfo->iUserID <= 0)
		{
			::SendMessage(pThis->m_hWnd, EVENT_DOWNLOADFAILED, 0, 0);
			continue;
		}
		long lFinish = IP_NET_DVR_GetDownloadPos(dTask.pDevInfo->iUserID);
		if (lFinish > 100)
		{
			pThis->m_ctlRecordList.SetItemText(pThis->m_hTask.iItemIndex, 2, "完成");
		}
		else if (lFinish >= 0)
		{
			strFinish.Format("%d%%", lFinish);
			pThis->m_ctlRecordList.SetItemText(pThis->m_hTask.iItemIndex, 2, strFinish);
		}
		else
		{
			NULL;
		}

		Sleep(1000);
	}

	return 0;
}

void CRecordDlg::OnNMDblclkListRecordlist(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
	NMHEADER *pp = (NMHEADER *)pNMHDR;
	
	if (pp->iItem < 0) return ;
	if (m_bLogQuery) return ;
	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;

	// 检查窗口是否空闲
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();
	CString StrKey = pNetSDKDemoDlg->GetWndUser(hWnd);
	if (StrKey.GetLength())
	{
		((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->CloseMedia(StrKey);
		//AfxMessageBox("指定窗口被占用，请先关闭然后再打开。");
		//return ;
	}
	// 已下载，本地回放
	int iMark = m_ctlRecordList.GetSelectionMark();
	CString strDLFileName = m_ctlRecordList.GetItemText(iMark,0);

	CloseMedia(strDLFileName);

	FILE *pF = fopen(strDLFileName, "r");
	if (pF)
	{
		fclose(pF);
		srand( (unsigned)time( NULL ) );
		int RANGE_MIN = 1;
		int RANGE_MAX = 9999;
		long lPlayID = (((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);

		IPC_ENTRY entry = {0};
		CDevInfo *pDevInfo = new CDevInfo(&entry);
		if (NULL == pDevInfo)
		{
			return;
		}

		CAVPlayer *pAVPlayer = new CAVPlayer(hWnd);
		if (NULL == pAVPlayer)
		{
			SAFE_DELETE(pDevInfo);
			return;
		}


		IP_TPS_PlayLocFile(lPlayID, hWnd, strDLFileName, 1);
		IP_TPS_SetStatusEventCallBack(lPlayID, OnPlayerEventRecv, this);

		pDevInfo->iUserID = lPlayID;
		pDevInfo->m_pRemotePlayBack = pAVPlayer;

		pAVPlayer->m_lPlayID = lPlayID;
		pAVPlayer->m_bLocal = TRUE;
		pAVPlayer->m_playState = 1;

		pNetSDKDemoDlg->SetWndUser(hWnd, strDLFileName);
		
		theApp.m_DevList.SetAt(strDLFileName, pDevInfo);
		
		m_iPlayState = 1;
		UpdateUI();
		return;
	}

	// 未下载，前端回放
	OpenMedia(hWnd, m_strFileDevIP.GetBuffer());
	UpdateUI();
	return;
}

LONG CALLBACK  CRecordDlg::OnPlayerEventRecv(long nPort,LONG nStateCode,char *pResponse,void *pUser)
{
	CRecordDlg * parent=(CRecordDlg*)pUser;
	TRACE("nPort = %d\n", nPort);
	return 0;
}

int CRecordDlg::OpenMedia(HWND hWnd, LPCTSTR strKey)
{
	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (NULL == pDevInfo)
	{
		return -1;
	}
	
	if (0 == pDevInfo->iUserID || pDevInfo->iLoginStatus <= 0)
	{
		((CNetSDKDemoDlg *)theApp.m_pMainWnd)->m_pWndDevList->LoginDevi(pDevInfo);
	
	}

	int iRet = IP_NET_DVR_GetReplayAblity(pDevInfo->iUserID);
	if (0 == iRet)
	{
		MessageBox("该设备不支持在线回放", "提示", MB_OK);
		return -2;
	}
	else if (iRet == -1)
	{
		MessageBox("该设备不支持在线回放", "提示", MB_OK);
		return -2;
	}
	

//  	if (pDevInfo->m_pRemotePlayBack)
//  	{
//  		MessageBox("该设备正在回放视频", "提示", MB_OK);
//  		return -3;
//  	}

	int iMark = m_ctlRecordList.GetSelectionMark();
	CString strr = m_ctlRecordList.GetItemText(iMark,0);
	int iPlay = IP_NET_DVR_PlayDeviceFile(pDevInfo->iUserID, strr.GetBuffer());
	if (0 != iPlay)
	{
		MessageBox("回放失败", "提示", MB_OK);
		return -4;
	}

	CAVPlayer *pAVPlayer = new CAVPlayer(hWnd);
	pAVPlayer->m_bLocal = FALSE;
	pAVPlayer->m_playState = 1;
	//pAVPlayer->m_lPlayID = pDevInfo->lPlayerID;
	pAVPlayer->m_hWnd = hWnd;
	SAFE_DELETE(pDevInfo->m_pRemotePlayBack);
	pDevInfo->m_pRemotePlayBack = pAVPlayer;

	((CNetSDKDemoDlg *)theApp.m_pMainWnd)->SetWndUser(hWnd, strKey);
	m_iPlayState = 1;
	
	return -1;
}

long CRecordDlg::CloseMedia(LPCTSTR strKey)
{ 
	CDevInfo *pDevInfo = NULL;
	CMapAutoLock Lock;
	theApp.m_DevList.Lookup(strKey, pDevInfo);

	if (NULL == pDevInfo)
	{
		return -1;
	}
	
	if (pDevInfo->m_pRemotePlayBack == NULL)
	{
		//SAFE_DELETE(pDevInfo->m_pRemotePlayBack);
		//SAFE_DELETE(pDevInfo);
		return -2;
	}

	

	if (pDevInfo->m_pRemotePlayBack->m_bLocal)
	{
		IP_TPS_StopPlayLocFile(pDevInfo->m_pRemotePlayBack->m_lPlayID);
		pDevInfo->m_pRemotePlayBack->m_playState = 0;
		((CNetSDKDemoDlg *)theApp.m_pMainWnd)->SetWndUser(pDevInfo->m_pRemotePlayBack->m_hWnd, NULL);

		SAFE_DELETE(pDevInfo->m_pRemotePlayBack);
		SAFE_DELETE(pDevInfo);
		theApp.m_DevList.RemoveKey(strKey);
	}
	else
	{
		IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_STOP, 0);
		
 		if (pDevInfo->m_pRemotePlayBack != NULL)
 		{
			pDevInfo->m_pRemotePlayBack->m_playState = 0;
			Sleep(700);
			((CNetSDKDemoDlg *)theApp.m_pMainWnd)->SetWndUser(pDevInfo->m_pRemotePlayBack->m_hWnd, NULL);
 			SAFE_DELETE(pDevInfo->m_pRemotePlayBack);
 		}
			
		m_iPlayState = 0;
		m_eFastSpeed = m_eSlowSpeed = SPEED_NOMAL;
		UpdateUI();
		//Sleep(800);
	}

	
	return 0;
}

BOOL CRecordDlg::SetLogQuery(BOOL bLogQuery)
{
	if (m_bDownLoad)
		return FALSE;
	m_ctlRecordList.DeleteAllItems();
	m_ctlBTNPageUP.EnableWindow(!bLogQuery);
	m_ctlBTNPageDown.EnableWindow(!bLogQuery);
	GetDlgItem(IDC_BTN_ADDLOCFILE)->EnableWindow(!bLogQuery);
	
	m_bLogQuery = bLogQuery;

	return TRUE;
}


void CRecordDlg::OnBnClickedBtnAddlocfile()
{
	// TODO: 在此添加控件通知处理程序代码
	char szFilters[]=
		"AVI Files (*.avi)|*.avi||";

	CFileDialog fileDlg (TRUE, "*", "",
		OFN_FILEMUSTEXIST| OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT, szFilters, this);

	if( fileDlg.DoModal ()==IDOK )
	{
		POSITION posfile = fileDlg.GetStartPosition();
		while (posfile)
		{
			CString pathName = fileDlg.GetNextPathName(posfile);
			CString strPath = fileDlg.GetPathName();
			int iCount = m_ctlRecordList.GetItemCount();

			int iItem = m_ctlRecordList.InsertItem(iCount, pathName);
			int iFileLen = 0;
			FILE *fp = NULL;
			if((fp=fopen(pathName,"r")))
			{
				fseek(fp,0,SEEK_END);
				iFileLen = ftell(fp);
				fclose(fp);
			}
			CString strFileLen;
			strFileLen.Format("%d", iFileLen);
			m_ctlRecordList.SetItemText(iItem, 1, strFileLen.GetBuffer());
			m_ctlRecordList.SetItemText(iItem, 2, FILE_LOCAL);
		}
	}
}


void CRecordDlg::EnableBotton(BOOL bEnable)
{
	CWnd *pWnd = GetDlgItem(IDC_BTN_QUERY);
	pWnd->EnableWindow(bEnable);
	pWnd = GetDlgItem(IDC_BTN_PAGEUP);
	pWnd->EnableWindow(bEnable);
	pWnd = GetDlgItem(IDC_BTN_PAGEDOWN);
	pWnd->EnableWindow(bEnable);
}

void CRecordDlg::RestorePlayStatus()
{
	int iListCount = m_ctlRecordList.GetItemCount();
	for (int i=0; i<iListCount; i++)
	{
		if (m_ctlRecordList.GetCheck(i))
		{
			m_ctlRecordList.SetItemText(i, 3, PLAYSTATUS_NULL);
		}
	}
}

void CRecordDlg::GetPlayFile(CString &strFileName)
{
	strFileName = _T("");
	int iListCount = m_ctlRecordList.GetItemCount();
	for (int i=0; i<iListCount; i++)
	{
		if (m_ctlRecordList.GetCheck(i))
		{
			CString strPlayStatus = m_ctlRecordList.GetItemText(i, 3);
			if (strPlayStatus.Compare(PLAYSTATUS_NULL) == 0)
			{
				strFileName = m_ctlRecordList.GetItemText(i, 0);
				m_ctlRecordList.SetItemText(i, 3, PLAYSTATUS_PLAY);
				break;
			}
		}
	}
	return ;
}

void CRecordDlg::InvalidatePlayStatus(CString strFileName, int iPlayStatus)
{
	int iListCount = m_ctlRecordList.GetItemCount();
	for (int i=0; i<iListCount; i++)
	{
		if (m_ctlRecordList.GetCheck(i))
		{
			CString strText = m_ctlRecordList.GetItemText(i, 0);
			if (strText == strFileName)
			{	
				if (iPlayStatus == 0)
					m_ctlRecordList.SetItemText(i, 3, PLAYSTATUS_OVER);
				else if (iPlayStatus == 1)
					m_ctlRecordList.SetItemText(i, 3, PLAYSTATUS_PLAY);
				else if (iPlayStatus == 2)
					m_ctlRecordList.SetItemText(i, 3, PLAYSTATUS_PAUSE);
				break;
			}
		}
	}
}



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

	CNetSDKDemoDlg *pNetSDKDemoDlg = (CNetSDKDemoDlg *)theApp.m_pMainWnd;//(CNetSDKDemoDlg *)GetParent();
	HWND hWnd = pNetSDKDemoDlg->GetCurSelWnd();

	CString strKey = pNetSDKDemoDlg->GetWndUser(hWnd);
	if (strKey.GetLength() <= 0)
	{
		return;
	}

	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (NULL == pDevInfo || pDevInfo->iUserID <= 0 || NULL == pDevInfo->m_pRemotePlayBack)
	{
		return;
	}

	if (pDevInfo->m_pRemotePlayBack->m_playState == 1 && m_iPlayState == 1)
	{
		if (pDevInfo->m_pRemotePlayBack->m_bLocal)
		{
			long lRet = IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_PAUSE, 0);
		}
		else
		{
			IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_PAUSE, 0);
		}
		
		pDevInfo->m_pRemotePlayBack->m_playState = 2;
		m_iPlayState = 2;

		m_eFastSpeed = SPEED_NOMAL;
		m_eSlowSpeed = SPEED_NOMAL;
		//m_btnPause.SetWindowText(_T("播放"));
		//IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_STOP, 0);
	}
	else
	{
		if (pDevInfo->m_pRemotePlayBack->m_bLocal)
		{
			IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_PLAY, 0);

		}
		else
		{
			IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_RESUME, 0);
		}
		
		pDevInfo->m_pRemotePlayBack->m_playState = 1;
		m_iPlayState = 1;

		m_eFastSpeed = SPEED_NOMAL;
		m_eSlowSpeed = SPEED_NOMAL;
		//m_btnPause.SetWindowText(_T("暂停"));
	}

	UpdateUI();
	
}

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

	CString strKey = pNetSDKDemoDlg->GetWndUser(hWnd);
	if (strKey.GetLength() <= 0)
	{
		return;
	}

	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (NULL == pDevInfo || pDevInfo->iUserID <= 0 || NULL == pDevInfo->m_pRemotePlayBack)
	{
		return;
	}

	
	switch(m_eFastSpeed)
	{
	case SPEED_NOMAL:
		{
			m_eFastSpeed = SPEED_2X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 2);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 2);
			}
			
			break;
		}
	case SPEED_2X:
		{
			m_eFastSpeed = SPEED_4X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 4);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 4);
			}

			break;
		}
	case SPEED_4X:
		{
			m_eFastSpeed = SPEED_8X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 8);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 8);
			}

			break;
		}
	case SPEED_8X:
		{
			m_eFastSpeed = SPEED_16X;

			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 16);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 16);
			}

			
			break;
		}
	case SPEED_16X:
		{
			m_eFastSpeed = SPEED_NOMAL;

			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 0);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 0);
			}

			break;
		}
	default:
		{
			m_eFastSpeed = SPEED_NOMAL;

			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_FAST, 0);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_FAST, 0);
			}

			break;
		}
	}


	m_iPlayState = 1;
	pDevInfo->m_pRemotePlayBack->m_playState = 1;
	m_eSlowSpeed = SPEED_NOMAL;

	UpdateUI();
	
}

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

	CString strKey = pNetSDKDemoDlg->GetWndUser(hWnd);
	if (strKey.GetLength() <= 0)
	{
		return;
	}

	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (NULL == pDevInfo || pDevInfo->iUserID <= 0 || NULL == pDevInfo->m_pRemotePlayBack)
	{
		return;
	}

	
	switch(m_eSlowSpeed)
	{
	case SPEED_NOMAL:
		{
			m_eSlowSpeed = SPEED_2X;
			
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 2);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 2);
			}

			break;
		}
	case SPEED_2X:
		{
			m_eSlowSpeed = SPEED_4X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 4);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 4);
			}
			break;
		}
	case SPEED_4X:
		{
			m_eSlowSpeed = SPEED_8X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 8);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 8);
			}
			break;
		}
	case SPEED_8X:
		{
			m_eSlowSpeed = SPEED_16X;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 16);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 16);
			}
			break;
		}
	case SPEED_16X:
		{
			m_eSlowSpeed = SPEED_NOMAL;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 0);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 0);
			}
			break;
		}
	default:
		{
			m_eSlowSpeed = SPEED_NOMAL;
			if (pDevInfo->m_pRemotePlayBack->m_bLocal)
			{
				IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_SLOW, 0);

			}
			else
			{
				IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_SLOW, 0);
			}
			break;
		}
	}

	m_iPlayState = 1;
	pDevInfo->m_pRemotePlayBack->m_playState = 1;
	m_eFastSpeed = SPEED_NOMAL;

	UpdateUI();
}

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

	CString strKey = pNetSDKDemoDlg->GetWndUser(hWnd);
	if (strKey.GetLength() <= 0)
	{
		return;
	}

	CDevInfo *pDevInfo = NULL;
	theApp.m_DevList.Lookup(strKey, pDevInfo);
	if (NULL == pDevInfo || pDevInfo->iUserID <= 0 || NULL == pDevInfo->m_pRemotePlayBack)
	{
		return;
	}

	if (pDevInfo->m_pRemotePlayBack->m_playState == 1 || pDevInfo->m_pRemotePlayBack->m_playState == 2)
	{
		if (pDevInfo->m_pRemotePlayBack->m_bLocal)
		{
			IP_TPS_ControlPlay(pDevInfo->iUserID, PLAYER_ACTION_PLAY, 4);
		}
		else
		{
			IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_PLAY, 4);
		}

		//IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_PLAY, 0);
		pDevInfo->m_pRemotePlayBack->m_playState = 1;
		m_iPlayState = 1;
		m_eFastSpeed = m_eSlowSpeed = SPEED_NOMAL;
		//m_btnPause.SetWindowText(_T("播放"));
		//IP_NET_DVR_ControlPlay(pDevInfo->iUserID, ACTION_STOP, 0);
	}

	UpdateUI();
}
