基于wince的網(wǎng)絡(luò)音視頻通信
{
//-------------------------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圖像特別大,則肯定是出錯(cuò),則拋棄
if(index》Video_Width*Video_Height)
{
//MessageBox(0,“緩沖區(qū)出錯(cuò)”,“錯(cuò)誤信息”,0);
return;
}
try{
IPicture *pPic;
IStream *pStm ;
//分配全局存儲(chǔ)空間
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這個(gè)數(shù)組包含的圖像有錯(cuò),則OleLoadPicture 容易產(chǎn)生讀寫內(nèi)存錯(cuò)誤
// 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();//會(huì)出錯(cuò)
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);
}
};
本文引用地址:http://m.butianyuan.cn/article/166249.htm
評(píng)論