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

📄 pockettalk2medlg.cpp

📁 G.726源代码
💻 CPP
字号:
// PocketTalk2MeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PocketTalk2Me.h"
#include "PocketTalk2MeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPocketTalk2MeDlg dialog

CPocketTalk2MeDlg::CPocketTalk2MeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPocketTalk2MeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPocketTalk2MeDlg)
		// 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);
}

void CPocketTalk2MeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPocketTalk2MeDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPocketTalk2MeDlg, CDialog)
	//{{AFX_MSG_MAP(CPocketTalk2MeDlg)
	ON_WM_DESTROY()
	ON_MESSAGE(WM_WAVE_DATA_ARRIVED,OnWaveDataArrivedMsg)
	ON_MESSAGE(WM_NET_DATA_ARRIVED,OnNetDataArrivedMsg)
	ON_BN_CLICKED(IDC_START_TALKING, OnStartTalking)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPocketTalk2MeDlg message handlers

BOOL CPocketTalk2MeDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	//1.状态
	m_bTalking=FALSE;
	ChatIp="";
	//2.初始化RTP
	InitRTP();
	//3.初始化音频
	g_pOut = new CWaveOut();
	g_pIn = new CWaveIn();
	g_pOut->StartPlay();
	g_pIn->StartRec(OnWaveDataArrive,(DWORD)this);	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CPocketTalk2MeDlg::OnOK() 
{	
	CDialog::OnOK();
	DestroyWindow();
}

void CPocketTalk2MeDlg::OnCancel() 
{	
	CDialog::OnCancel();
	DestroyWindow();
}

void CPocketTalk2MeDlg::OnDestroy() 
{
	CDialog::OnDestroy();

	//关闭音频
	g_pOut->StopPlay();
	g_pIn->StopRec();
	delete g_pOut;
	delete g_pIn;

	//关闭RTP
	m_sessData.rtpReceive.Destroy();
	m_sessData.rtpSend.Destroy();

	WSACleanup();
		
}

void CPocketTalk2MeDlg::InitRTP()
{
	RTPSessionParams sessParams;
	RTPUDPv4TransmissionParams transParams;

	WORD wVersionRequested;
	WSADATA wsaData;
	
	wVersionRequested=MAKEWORD(2,2);
	
	if(WSAStartup(wVersionRequested,&wsaData)!=0)
	{
		return;
	}

	/////////////////////////////////////////////////////////////////
	//建立RTP的数据发送
	sessParams.SetOwnTimestampUnit(1.0 / 30.0); //30 video frames per second
	sessParams.SetUsePollThread(1); //background thread to call virtual callbacks - set by default, but just to be sure
	sessParams.SetMaximumPacketSize(MAX_PACKET_SIZE);
	transParams.SetPortbase(PORT_BASE);
	m_sessData.rtpSend.Create(sessParams, &transParams);
	/////////////////////////////////////////////////////////////////

	/////////////////////////////////////////////////////////////////
	//建立RTP的数据接收
	sessParams.SetOwnTimestampUnit(1.0 / 30.0); //30 video frames per second
	sessParams.SetUsePollThread(1); //background thread to call virtual callbacks - set by default, but just to be sure
	sessParams.SetMaximumPacketSize(MAX_PACKET_SIZE);
	transParams.SetPortbase(PORT_DATA);
	m_sessData.rtpReceive.dlg=this;
	m_sessData.rtpReceive.Create(sessParams, &transParams);
	/////////////////////////////////////////////////////////////////
}

void CPocketTalk2MeDlg::OnWaveDataArrive(char *data,int length,DWORD userdata)
{
	CPocketTalk2MeDlg *pWnd=(CPocketTalk2MeDlg *)userdata;
	::SendMessage(pWnd->m_hWnd,WM_WAVE_DATA_ARRIVED,(WPARAM)data,LPARAM(length));
}

LONG CPocketTalk2MeDlg::OnWaveDataArrivedMsg(WPARAM wParam,LPARAM lParam)
{
	//处理收到的音频数据
	int length=(int)lParam;
	char *data=(char*)wParam;

	memcpy(pin,g_pIn->buffer,960);
	
	
	//G726压缩
	//对音频帧进行编码	
	g726_Encode((unsigned char*)pin,pout);

	m_sessData.rtpSend.SendPacket(pout,120, 0, false, 10UL);


	return 0;
}

LONG CPocketTalk2MeDlg::OnNetDataArrivedMsg(WPARAM wParam,LPARAM lParam)
{
	char* data;
	int length;

	data=(char*)wParam;
	length=(int)lParam;

	//G726解压
	//对音频帧进行解码
	g726_Decode(data,(unsigned char*)waveout);
	//播放声音
	g_pOut->Play(waveout,960);

	return 1;
}

void CPocketTalk2MeDlg::OnStartTalking() 
{
	int i;
	char* temp;

	if(!m_bTalking)
	{
		//取得对方IP
		GetDlgItemText(IDC_EDIT_IP,ChatIp);
		temp=new char [ChatIp.GetLength()];
		for(i=0;i<ChatIp.GetLength();i++)
		{
			temp[i]=ChatIp.GetAt(i);
		}
		//开始聊天
		intIP = inet_addr(temp);
		delete temp;
		if(intIP != INADDR_NONE)
		{
			intIP = ntohl(intIP); //put in host byte order
			rtpAddr=new RTPIPv4Address(intIP,PORT_DATA);
			m_sessData.rtpSend.AddDestination(*rtpAddr);
			delete rtpAddr;
		}
		SetDlgItemText(IDC_START_TALKING,_T("停止对话"));
		m_bTalking=TRUE;
	}
	else
	{
		//取得对方IP
		GetDlgItemText(IDC_EDIT_IP,ChatIp);
		temp=new char [ChatIp.GetLength()];
		for(i=0;i<ChatIp.GetLength();i++)
		{
			temp[i]=ChatIp.GetAt(i);
		}
		//停止聊天
		intIP = inet_addr(temp);
		delete temp;
		if(intIP != INADDR_NONE)
		{
			intIP = ntohl(intIP); //put in host byte order
			rtpAddr=new RTPIPv4Address(intIP,PORT_DATA);
			m_sessData.rtpSend.DeleteDestination(*rtpAddr);
			delete rtpAddr;
		}
		SetDlgItemText(IDC_START_TALKING,_T("开始对话"));
		m_bTalking=FALSE;
	}
}

⌨️ 快捷键说明

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