⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vodclientdlg.cpp

📁 最近在学习directshow, Directshow实务精选的源代码
💻 CPP
字号:
// VODClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VODClient.h"
#include "VODClientDlg.h"

#include "UNetwork.h"
#include "defines.h"
#include "CServerAddressDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVODClientDlg dialog

CVODClientDlg::CVODClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVODClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVODClientDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	mState    = DISCONNECTED;
	mServerIP = 0x7f000001; // 127.0.0.1
	mClientIP = 0x7f000001;
	mPlayingPID = 0;
}

void CVODClientDlg::OnDestroy() 
{
	SendMediaControlCommand(Cmd_RequestStop);
	mPlayer.StopGraph();
	mPlayer.StopReceiving();
	SendSimpleCommand(Cmd_RequestTCPDisconnect);

	CDialog::OnDestroy();
}

void CVODClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVODClientDlg)
	DDX_Control(pDX, IDC_BUTTON_STOP, mButtonStop);
	DDX_Control(pDX, IDC_BUTTON_REFRESH, mButtonRefresh);
	DDX_Control(pDX, IDC_BUTTON_PLAY, mButtonPlay);
	DDX_Control(pDX, IDC_BUTTON_PAUSE, mButtonPause);
	DDX_Control(pDX, IDC_BUTTON_CONNECT, mButtonConnect);
	DDX_Control(pDX, IDC_EDIT_STATUS, mEditStatus);
	DDX_Control(pDX, IDC_LIST_PROGRAMS, mProgramListCtrl);
	DDX_Control(pDX, IDC_EDIT_HOST_INFO, mEditHostInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVODClientDlg, CDialog)
	//{{AFX_MSG_MAP(CVODClientDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVODClientDlg message handlers

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

	// 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
	
	// TODO: Add extra initialization here
	char hostName[100];
	char hostIP[50];
	if (UNetwork::GetHostInfo(hostIP, hostName))
	{
		mClientIP = ntohl(inet_addr(hostIP));
		CString info;
		info.Format("Local: %s  IP: %s", hostName, hostIP);
		mEditHostInfo.SetWindowText(info);
	}

	mProgramListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	mProgramListCtrl.InsertColumn(0, "File", LVCFMT_CENTER, 120);
	mProgramListCtrl.InsertColumn(1, "Type", LVCFMT_CENTER, 80);
	mProgramListCtrl.InsertColumn(2, "Size", LVCFMT_CENTER, 80);
	mProgramListCtrl.InsertColumn(3, "PID", LVCFMT_CENTER, 60);

	// Set up TCP listening
	mTCPListener.AddMsgReceiver(this);
	mTCPListener.SetListenPort(CLIENT_TCP_PORT);
	if (mTCPListener.Create())
	{
		mTCPListener.StartListening();
	}
	// Set up command listening
	mCommandManager.AddMsgReceiver(this);
	mCommandManager.SetLocalPort(CLIENT_UDP_PORT);
	if (mCommandManager.CreateReceiver())
	{
		mCommandManager.StartReceiving();
	}

	// Update status
	ReflectStatus();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CVODClientDlg::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 CVODClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

bool CVODClientDlg::ReceiveMessage(MessageT inMessage, 
								   void * ioParam,
								   void * ioParam2)
{
	if (inMessage == msg_CommandReceived)
	{
		ProcessCommand(ioParam);
		return true;
	}
	else if (inMessage == msg_NewSocketAccepted)
	{
		mState = CONNECTED;
		ReflectStatus();
		return true;
	}

	return CMsgReceiver::ReceiveMessage(inMessage, ioParam, ioParam2);
}

void CVODClientDlg::ProcessCommand(void * inCommand)
{
	// First, get the command id
	long * pCmd = (long *) inCommand;

	switch (ntohl(*pCmd))
	{
	case Cmd_ProgramList:
		{
			pCmd++;
			Program_List * pData = (Program_List *) pCmd;
			pData->my_ntoh();
		
			CMediaInfo info;
			info.CopyFrom(pData);
			AddProgramItem(info);
		}
		break;

	default:
		break;
	}
}

void CVODClientDlg::AddProgramItem(CMediaInfo & inMedia)
{
	int count = mProgramListCtrl.GetItemCount();
	// We needn't display the full path of the file
	char * p = strrchr(inMedia.mFilePath, '\\');
	mProgramListCtrl.InsertItem(count, p+1);
	// Determine file type
	CString str;
	switch (inMedia.mFileType)
	{
	case FT_MPEG1:
		str = "MPEG1";
		break;
	case FT_MPEG2:
		str = "MPEG2";
		break;
	case FT_AVI:
		str = "AVI";
		break;
	case FT_MP3:
		str = "MP3";
		break;
	default:
		str = "Unknown";
		break;
	}
	mProgramListCtrl.SetItemText(count, 1, str);
	str.Format("%.3fM", inMedia.mFileSize / 1024. / 1024.);
	mProgramListCtrl.SetItemText(count, 2, str);
	str.Format("%d", inMedia.mProgramId);
	mProgramListCtrl.SetItemText(count, 3, str);

	// Save to the list too
	mPrgrmList.AddTail(inMedia);
}

void CVODClientDlg::OnButtonConnect() 
{
	CServerAddressDlg dlg;
	dlg.SetServerIP(&mServerIP);
	if (dlg.DoModal() == IDOK)
	{
		// Create command sender first
		mCommandManager.SetTargetIP(mServerIP);
		mCommandManager.SetTargetPort(SERVER_UDP_PORT);
		if (mCommandManager.CreateSender())
		{
			BOOL pass = SendTCPConnectRequest();
			// Currently, we can't assure we are connected to the server yet.
			// We must wait on listening TCP socket...
		//	mState = CONNECTED;
		//	ReflectStatus();
		}
	}
}

BOOL CVODClientDlg::SendTCPConnectRequest(void)
{
	char  buf[MAX_COMMAND_SIZE];
	long  len  = sizeof(Net_Command) + sizeof(Request_TCP_Connect);
	// Specify command id first
	long * pCmd = (long *) buf;
	*pCmd = Cmd_RequestTCPConnect;
	*pCmd = htonl(*pCmd);
	pCmd++;
	Request_TCP_Connect * pData = (Request_TCP_Connect *) pCmd;
	pData->client_ip  = mClientIP;
	pData->client_tcp = CLIENT_TCP_PORT;
	pData->client_udp = CLIENT_UDP_PORT;
	pData->my_hton();

	return mCommandManager.Send(buf, len);
}

void CVODClientDlg::OnButtonPlay() 
{
	if (mState == PAUSE)
	{
		SendMediaControlCommand(Cmd_RequestPlay);
		mPlayer.PlayGraph();
		mState = PLAY;
		ReflectStatus();
		return;
	}

	// It's the first time to play this program...
	int index = mProgramListCtrl.GetNextItem(-1, LVNI_SELECTED);
	if (index >= 0)
	{
		// If has no tcp connection, send a request to the server
		if (!mTCPListener.IsAcceptedValid())
		{
			SendTCPConnectRequest();
		}

		// Get the program ID
		CString pid = mProgramListCtrl.GetItemText(index, 3);
		mPlayingPID = atol(pid);

		// Config the media player
		CMediaInfo     media;
		POSITION pos = mPrgrmList.GetHeadPosition();
		while (pos)
		{
			media  = mPrgrmList.GetNext(pos);
			if (media.mProgramId == mPlayingPID)
			{
				break;
			}
		}
		mPlayer.SetMediaInfo(media);
		BOOL pass = mPlayer.Activate();
		if (pass)
		{
			// Config the source filter
			mPlayer.SetStreamType(media.mFileType);
			mPlayer.SetSize(media.mFileSize, media.mCheckOffset2);
			if (!mTCPListener.IsAcceptedValid())
			{
				Sleep(500);
			}
			mPlayer.SetStreamSocket(mTCPListener.GetAccepted());
			mPlayer.StartReceiving();
		}

		// Send a request to the server
		if (pass)
		{
			pass = SendMediaControlCommand(Cmd_RequestPlay);
		}

		// Wait for enough checking bytes received
		if (pass)
		{
			BOOL  canConnect = FALSE;
			long  counter    = 0;  // We never wait too long time
			while (!canConnect)
			{
				if (mPlayer.CanConnectToDownstream())
				{
					canConnect = TRUE;
				}
				else
				{
					Sleep(100);
					if (++counter > 200)
					{
						break;
					}
				}
			}
			if (canConnect)
			{
				pass = mPlayer.ConnectFilters();
				pass = mPlayer.PlayGraph();
				mState = PLAY;
				ReflectStatus();
			}
		}

		mProgramListCtrl.SetFocus();
	}
	else
	{
		AfxMessageBox("Please select a program first!");
	}
}

void CVODClientDlg::OnButtonPause() 
{
//	SendMediaControlCommand(Cmd_RequestPause);
	mPlayer.PauseGraph();

	mState = PAUSE;
	ReflectStatus();
	mProgramListCtrl.SetFocus();
}

void CVODClientDlg::OnButtonStop() 
{
	SendMediaControlCommand(Cmd_RequestStop);
	mPlayer.StopGraph();
	mPlayer.StopReceiving();

	mState = STOP;
	ReflectStatus();
	mProgramListCtrl.SetFocus();
}

BOOL CVODClientDlg::SendMediaControlCommand(long inCommand)
{
	char  buf[MAX_COMMAND_SIZE];
	long  len  = sizeof(Net_Command) + sizeof(Media_Control);
	// Specify command id first
	long * pCmd = (long *) buf;
	*pCmd = inCommand;
	*pCmd = htonl(*pCmd);
	pCmd++;
	Media_Control * pData = (Media_Control *) pCmd;
	pData->program_id = mPlayingPID;
	pData->my_hton();
	return mCommandManager.Send(buf, len);
}

BOOL CVODClientDlg::SendSimpleCommand(long inCommand)
{
	char  buf[MAX_COMMAND_SIZE];
	long  len  = sizeof(Net_Command);
	// Specify command id first
	long * pCmd = (long *) buf;
	*pCmd = inCommand;
	*pCmd = htonl(*pCmd);
	return mCommandManager.Send(buf, len);
}

// Get the program list from the media server
void CVODClientDlg::OnButtonRefresh() 
{
	mProgramListCtrl.DeleteAllItems();
	SendSimpleCommand(Cmd_RequestProgramList);
}

void CVODClientDlg::ReflectStatus(void)
{
	switch (mState)
	{
	case DISCONNECTED:
		mEditStatus.SetWindowText("Status: Disconnected.");
		mButtonConnect.EnableWindow(TRUE);
		mButtonRefresh.EnableWindow(FALSE);
		mButtonPlay.EnableWindow(FALSE);
		mButtonPause.EnableWindow(FALSE);
		mButtonStop.EnableWindow(FALSE);
		break;

	case CONNECTED:
		mEditStatus.SetWindowText("Status: Connected.");
		mButtonConnect.EnableWindow(FALSE);
		mButtonRefresh.EnableWindow(TRUE);
		mButtonPlay.EnableWindow(TRUE);
		mButtonPause.EnableWindow(FALSE);
		mButtonStop.EnableWindow(FALSE);
		break;

	case PLAY:
		mEditStatus.SetWindowText("Status: Playing...");
		mButtonConnect.EnableWindow(FALSE);
		mButtonRefresh.EnableWindow(FALSE);
		mButtonPlay.EnableWindow(FALSE);
		mButtonPause.EnableWindow(TRUE);
		mButtonStop.EnableWindow(TRUE);
		break;

	case PAUSE:
		mEditStatus.SetWindowText("Status: Paused.");
		mButtonConnect.EnableWindow(FALSE);
		mButtonRefresh.EnableWindow(FALSE);
		mButtonPlay.EnableWindow(TRUE);
		mButtonPause.EnableWindow(FALSE);
		mButtonStop.EnableWindow(TRUE);
		break;

	case STOP:
		mEditStatus.SetWindowText("Status: Stopped.");
		mButtonConnect.EnableWindow(FALSE);
		mButtonRefresh.EnableWindow(TRUE);
		mButtonPlay.EnableWindow(TRUE);
		mButtonPause.EnableWindow(FALSE);
		mButtonStop.EnableWindow(FALSE);
		break;
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -