?? avclass.h
字號:
#pragma once
#include "winsock2.h"
//UDP支持
#include "..\UDP\UDP.h"
//音頻支持
#include "WaveIn.h"
#include "waveout.h"
//G726支持
#include "g726.h"
//視頻支持
#include "Gdiplus.h"
using namespace Gdiplus;
#define VideoData_Size 1440 //每塊視頻數(shù)據(jù)包的大小
#define Video_Width 320 //視頻寬度
#define Video_Height 240 //視頻長度
#define AudioData_Size 960 //每塊音頻數(shù)據(jù)包的大小
#define Compr_AudioData_Size 120 //壓縮后音頻塊的大小
//音頻輸入輸出變量
CWaveIn *g_pIn;
CWaveOut *g_pOut;
char pin[AudioData_Size],pout[Compr_AudioData_Size];
char wave_data[AudioData_Size];
//UDP變量
CUDP_CE m_CEUdp;
//視頻輸入變量
GdiplusStartupInput m_gdiPlusInPut;
ULONG_PTR m_gdiPlusToken;
char video_data[Video_Width*Video_Height];
int index;//視頻數(shù)據(jù)當(dāng)前索引
class AVClass
{
private:
public:
//=====================================================================
// 語法格式: void InitAV(CWnd * p)
// 實(shí)現(xiàn)功能: 初始化音頻和視頻,用于錄音、播放音頻,以及播放視頻
// 參數(shù): p為窗口類指針
// 返回值: 無
//=====================================================================
void InitAV(CWnd * p,int local_port,CString remote_ip,int remote_port)
{
//-------------------------UDP連接--------------------------//
m_CEUdp.m_OnUdpRecv = OnUdpCERecv;
DWORD nResult = m_CEUdp.Open(p,local_port,remote_ip,remote_port);
if (nResult <=0)
{
AfxMessageBox(_T("打開端口失敗"));
return;
}
//-------------------------音頻--------------------------//
g_pOut = new CWaveOut();
g_pIn = new CWaveIn();
g_pOut->StartPlay();
g_pIn->StartRec(OnRecording,(DWORD)p);
//-------------------------視頻--------------------------//
GdiplusStartup( &m_gdiPlusToken, &m_gdiPlusInPut, NULL ); //初始化GDI+
memset(video_data,0,Video_Width*Video_Height);
index=0;
}
//=====================================================================
// 語法格式: void FreeAV()
// 實(shí)現(xiàn)功能: 釋放音頻、視頻
// 參數(shù): 無
// 返回值: 無
//=====================================================================
void FreeAV()
{
//-------------------------音頻--------------------------//
g_pOut->StopPlay();
g_pIn->StopRec();
delete g_pOut;
delete g_pIn;
//-------------------------視頻--------------------------//
GdiplusShutdown(m_gdiPlusToken); //銷毀GDI+
//------------------------UDP--------------------------//
m_CEUdp.Close();
}
//=====================================================================
// 語法格式: void RecAndPlay(WPARAM wParam,LPARAM lParam,HWND hwnd)
// 實(shí)現(xiàn)功能: 接收網(wǎng)絡(luò)傳來的音頻,以及播放
// 參數(shù): wParam,表示數(shù)據(jù);lParam,表示數(shù)據(jù)長度;hwnd,表示顯示視頻的窗口句柄
// 返回值: 無
//=====================================================================
static void CALLBACK OnUdpCERecv(CWnd *pWnd,char* buf,int nLen,sockaddr * addr)
{
/*測試收到的數(shù)據(jù)大小
CString tmp;
tmp.Format(L"%d",nLen);
MessageBox(0,tmp,0,0);
return;*/
//-------------------------如果是音頻數(shù)據(jù)--------------------------//
if(nLen==Compr_AudioData_Size)
{
g726_Decode(buf,(unsigned char*)wave_data);
g_pOut->Play(wave_data,AudioData_Size);
return;
}
//-------------------------如果是視頻數(shù)據(jù)--------------------------//
if(nLen==VideoData_Size)//完整的視頻數(shù)據(jù)塊
{
for(int i=0;i<nLen;i++)
{
video_data[index]=buf[i];
index++;
}
return;
}
//視頻數(shù)據(jù)塊的最后一塊
for(int i=0;i<nLen;i++)
{
video_data[index]=buf[i];
index++;
}
//如果JPEG圖像特別大,則肯定是出錯,則拋棄
if(index>Video_Width*Video_Height)
{
//MessageBox(0,"緩沖區(qū)出錯","錯誤信息",0);
return;
}
try{
IPicture *pPic;
IStream *pStm ;
//分配全局存儲空間
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,index);
LPVOID pvData=NULL ;
//鎖定分配內(nèi)存塊
pvData=GlobalLock(hGlobal);
//復(fù)制數(shù)據(jù)包video_data到pvData
memcpy(pvData,video_data,index);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
ULARGE_INTEGER pSeek;
LARGE_INTEGER dlibMove ={ 0 } ;
pStm->Seek(dlibMove,STREAM_SEEK_SET ,&pSeek);
// Sleep(15);
//裝入圖形文件
if(FAILED(OleLoadPicture(pStm,index,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{//附:如果video_data這個數(shù)組包含的圖像有錯,則OleLoadPicture 容易產(chǎn)生讀寫內(nèi)存錯誤
// pPic->Release();
// pStm->Release();
return ;
}
Image img(pStm,0);
Graphics mGraphics(GetDC(pWnd->m_hWnd));
mGraphics.DrawImage(&img, 0, 0, Video_Width, Video_Height);
img.~Image();//會出錯
mGraphics.~Graphics();
pPic->Release();
pStm->Release();
}
catch(CException * e)
{}
memset(video_data,0,Video_Width*Video_Height);
index=0;
}
//=====================================================================
// 語法格式: static void OnRecording(char *data,int length,DWORD userdata)
// 實(shí)現(xiàn)功能: 釋放音頻
// 參數(shù): data表示數(shù)據(jù),length表示數(shù)據(jù)長度,userdata暫時(shí)沒用
// 返回值: 無
//=====================================================================
static void OnRecording(char *data,int length,DWORD userdata)
{
memcpy(pin,g_pIn->buffer,AudioData_Size);
g726_Encode((unsigned char*)pin,pout);
m_CEUdp.SendData(pout,Compr_AudioData_Size);
}
};
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -