📄 talk2medlg.cpp
字号:
// Talk2MeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Talk2Me.h"
#include "Talk2MeDlg.h"
#include ".\talk2medlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTalk2MeDlg dialog
CTalk2MeDlg::CTalk2MeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTalk2MeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTalk2MeDlg)
// 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 CTalk2MeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTalk2MeDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTalk2MeDlg, CDialog)
//{{AFX_MSG_MAP(CTalk2MeDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_MESSAGE(WM_WAVE_DATA_ARRIVE,OnWaveDataArrivedMsg)
ON_MESSAGE(WM_NET_DATA_ARRIVED,OnNetDataArrivedMsg)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_START_TALKING, OnBnClickedStartTalking)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTalk2MeDlg message handlers
BOOL CTalk2MeDlg::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
//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 CTalk2MeDlg::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 CTalk2MeDlg::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 CTalk2MeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTalk2MeDlg::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 CTalk2MeDlg::OnCancel()
{
CDialog::OnCancel();
DestroyWindow();
}
void CTalk2MeDlg::OnOK()
{
CDialog::OnOK();
DestroyWindow();
}
void CTalk2MeDlg::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 CTalk2MeDlg::OnWaveDataArrive(char *data,int length,DWORD userdata)
{
CTalk2MeDlg *pWnd=(CTalk2MeDlg *)userdata;
::SendMessage(pWnd->m_hWnd,WM_WAVE_DATA_ARRIVE,(WPARAM)data,LPARAM(length));
}
LONG CTalk2MeDlg::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 CTalk2MeDlg::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 CTalk2MeDlg::OnBnClickedStartTalking()
{
CButton* pBtn=(CButton *)GetDlgItem(IDC_START_TALKING);
if(pBtn->GetCheck())
{
//取得对方IP
GetDlgItemText(IDC_EDIT_IP,ChatIp);
//开始聊天
intIP = inet_addr(ChatIp);
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;
}
pBtn->SetWindowText("停止对话");
}
else
{
//停止聊天
intIP = inet_addr(ChatIp);
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;
}
pBtn->SetWindowText("开始对话");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -