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

📄 rdv2edemodlg.cpp

📁 这个程序包里有3个部分:直播服务器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	UINT	uiSourcePort = 0;
	int		iRetVal;
	DWORD	dwLastErr;
	PACKET_HEAD* pPacketHead = (PACKET_HEAD*)szBuf;
	DWORD	dwArg;
	
	while(1){
		memset(szBuf, 0, nBufLen);
		g_Mutex.Lock();
		m_Socket.Attach(m_hSocket);	
		iRetVal = m_Socket.IOCtl(FIONREAD, &dwArg);
		if(iRetVal){
			if(dwArg){
				iRetVal = m_Socket.ReceiveFrom(szBuf, nBufLen, strSourceAdd, uiSourcePort);
			}
			else{
				m_Socket.Detach();
				g_Mutex.Unlock();
				Sleep(10);
				continue;
			}
		}
		else{
			m_Socket.Detach();
			g_Mutex.Unlock();
			Sleep(10);
			continue;
		}

		m_Socket.Detach();
		if(iRetVal == SOCKET_ERROR){
			dwLastErr = GetLastError();
			g_Mutex.Unlock();
			if(dwLastErr == WSAEINTR){
				Sleep(10);
				continue;
			}
		}
		else if(iRetVal == 0){
			g_Mutex.Unlock();
			break;
		}
		g_Mutex.Unlock();
		if(iRetVal == sizeof(PACKET_HEAD) && pPacketHead->ucFlag == MT_KEEP_ALIVE){
			g_ulKeepAliveCntr = 0;
			Sleep(10);
			continue;
		}
		
		switch(pPacketHead->ucFlag){
		case MT_REGISTER:
			{//注册
			REG_RESULT	RegResult;
			if(g_bWorking || !m_bStartCompress){
				RegResult.Head.ulSeqNo= 0;
				RegResult.Head.ucFlag = MT_REG_RESULT;
				RegResult.ucRet       = 2;
				g_Mutex.Lock();
				m_Socket.Attach(m_hSocket);	
				m_Socket.SendTo(&RegResult, sizeof(REG_RESULT), uiSourcePort, (LPCTSTR)strSourceAdd);
				m_Socket.Detach();
				g_Mutex.Unlock();
				CTime curTime = CTime::GetCurrentTime();
				TRACE("\n[%d:%d:%d] %s:%d Register Fail\n", curTime.GetHour(), curTime.GetMinute(), curTime.GetSecond(), (LPCTSTR)strSourceAdd, uiSourcePort);
			}
			else{				
				g_ulSeqNo = 0;
				g_ulKeepAliveCntr = 0;
				RegResult.Head.ulSeqNo= g_ulSeqNo++;
				RegResult.Head.ucFlag = MT_REG_RESULT;
				RegResult.ucRet       = 1;
				RegResult.cFrameRate   = m_cFrameRate;
				RegResult.sWidth      = m_sWidth;
				RegResult.sHeight     = m_sHeight;				
				g_Mutex.Lock();
				m_Socket.Attach(m_hSocket);	
				//for(int i=0; i<1000; i++){
				m_Socket.SendTo(&RegResult, sizeof(REG_RESULT), uiSourcePort, (LPCTSTR)strSourceAdd);				
				//Sleep(10);
				//}
				m_Socket.Detach();
				g_Mutex.Unlock();
				CTime curTime = CTime::GetCurrentTime();
				TRACE("\n[%d:%d:%d] %s:%d Register Ok\n", curTime.GetHour(), curTime.GetMinute(), curTime.GetSecond(), (LPCTSTR)strSourceAdd, uiSourcePort);
				m_uiSourcePort = uiSourcePort;
				m_strSourceAdd = strSourceAdd;
				g_bWorking = TRUE;
			}
			}
			break;
		case MT_KEEP_ALIVE:
			{//联系消息
			g_ulKeepAliveCntr = 0;
			}
			break;
		default: break;
		}
	}
	delete [] szBuf;
	return 0;
}

BOOL CrdV2EDemoDlg::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

	// TODO: Add extra initialization here
	CoInitialize(NULL);
	char	szName[MAX_PATH];	
	rdV2SearchVideoDev();
	int iTotalDev = rdV2GetTotalVideoDevNumber();
	if(iTotalDev == 0){
		MessageBox("没有发现可用的视频输入设备!", "rdV2EDemo", MB_OK);
		EndDialog(IDCANCEL);
		return FALSE;
	}
	int iDevIndex = 0;
	if(iTotalDev > 1){
		int i,iRetVal;
		iDevIndex = -1;
		for(i=0; i<iTotalDev; i++){
			rdV2GetVideoDevInfo(i, szName);
			iRetVal = FindSubString("USB", szName);
			if(iRetVal > 0){
				iDevIndex = i; 
				break;
			}
		}
		if(iDevIndex == -1) iDevIndex = iTotalDev - 1;
	}
	m_iVideoIndex = iDevIndex;
	rdV2SetVideoSource(m_iVideoIndex);

	rdV2GetVideoDevInfo(iDevIndex, szName);
	CString strTemp = "Video Device: ";
	strTemp += szName;
	m_ctrlDevName.SetWindowText(strTemp);
	TRACE("<Video Device> %s\n", szName);
	rdV2SetVideoOwnerWnd(m_OverlayFrameWnd.m_hWnd);
	rdV2StartPreview();

	rdV2SearchAudioDev();
	iTotalDev = rdV2GetTotalAudioDevNumber();
	if(iTotalDev == 0){
		TRACE("没有发现可用的音频输入设备!\n");
	}
	else{
		rdV2GetAudioDevInfo(0, szName);
		rdV2SetAudioSource(0);
		TRACE("<Audio Device> %s\n", szName);
		m_iAudioIndex = 0;
	}

	m_bStartCompress = FALSE;

	g_MediaData.ifBlock = FALSE;
	g_MediaData.Create("G_MEDIA_DATA", 2*1024*1024);
	m_btnCapComp.EnableWindow(TRUE);
	m_btnStop.EnableWindow(FALSE);

	WSADATA wsaData;
	WSAStartup(0x202,&wsaData);

	int iRetVal = m_Socket.Create(RDV2_SERVER_PORT, SOCK_DGRAM);	
	if(!iRetVal){
		AfxMessageBox("Can not create socket!", MB_OK);
		return FALSE;
	}
	m_Socket.AsyncSelect(0);
	iRetVal=0;
	m_Socket.IOCtl(FIONBIO,(DWORD*)&iRetVal);
	m_hSocket = m_Socket.Detach();

	DWORD	dwThreadId;
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendThreadEntry, (LPVOID)this, 0, &dwThreadId);
	CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RecvThreadEntry, (LPVOID)this, 0, &dwThreadId);

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

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

void CrdV2EDemoDlg::OnButtonCapcomp() 
{
	// TODO: Add your control notification handler code here

	int iRetVal;

	//{{video & audio
	rdV2SetVideoSource(m_iVideoIndex);
	rdV2SetAudioSource(m_iAudioIndex);
	//}}

	//{{video only
	//rdV2SetVideoSource(m_iVideoIndex);
	//rdV2SetAudioSource(-1);
	//}}

	//{{audio only
	//rdV2SetVideoSource(-1);
	//rdV2SetAudioSource(m_iAudioIndex);
	//}}

	m_sWidth  = 352;
	m_sHeight = 288;
	m_cFrameRate = 25;

	rdV2SetVideoCallback(_VideoHook);
	rdV2SetAudioCallback(_AudioHook);
	//rdV2SetVideoResolution(320, 240);	//320 x 240	
	//rdV2SetVideoResolution(176, 144);	
	//rdV2SetVideoResolution(352, 288);	//352 x 288
	rdV2SetVideoResolution(m_sWidth, m_sHeight);

	rdV2SetVideoBitrate(300*1000);		//300K
	//rdV2SetFrameRate(30);				//每秒30帧
	rdV2SetFrameRate(m_cFrameRate);
	//rdV2SetFrameRate(10);
	rdV2SetVideoKeyFrameInterval(10);	//I帧间隔
	//iRetVal = rdV2StartCompress(0);	//禁止预览
	iRetVal = rdV2StartCompress(1);		//预览(无压缩)
	//iRetVal = rdV2StartCompress(2);	//预览(压缩后解码回放)

	m_bStartCompress = !iRetVal;

	m_btnCapComp.EnableWindow(FALSE);
	m_btnStop.EnableWindow(TRUE);
}

void CrdV2EDemoDlg::OnButtonStop() 
{
	// TODO: Add your control notification handler code here
	rdV2Stop();
	//rdV2StartPreview();
	m_btnCapComp.EnableWindow(TRUE);
	m_btnStop.EnableWindow(FALSE);
}

void CrdV2EDemoDlg::OnButtonEnumRes() 
{
	// TODO: Add your control notification handler code here
	int iWidth, iHeight;

	int iTotal = rdV2GetTotalResolution();
	for(int i=0; i<iTotal; i++){
		rdV2GetVideoResolution(i, &iWidth, &iHeight);
		TRACE("Support Resolution: %d x %d\n", iWidth, iHeight);
	}		
}


⌨️ 快捷键说明

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