基于wince的網(wǎng)絡音視頻通信
if (0 != capSetVideoFormat(0, VIDEO_PALETTE_RGB24, VIDEO_SIZE_SIF))
{
//::MessageBox(NULL, L“SetVideoFormat error ”, L“Notice”, 0);
goto video_error;//出錯,釋放空間
}
if (capStartCamera(0) != 0)
{
//::MessageBox(NULL, L“StartCamera error ”, L“Notice”, 0);
capStopCamera(0);
goto video_error;//出錯,釋放空間
}
//沒出錯,進行視頻(控制狀態(tài))、音頻設置
isCameraEnabled=true;
goto audio;
video_error:
free (lpFrameBuffer);
lpFrameBuffer = NULL;
free (lpJpgBuffer);
lpJpgBuffer = NULL;
//-------------------------音頻--------------------------//
audio:
g_pOut = new CWaveOut();
g_pIn = new CWaveIn();
g_pOut-》StartPlay();
g_pIn-》StartRec(OnRecCapAndSend,(DWORD)p);
}
//=====================================================================
// 語法格式: void FreeAV()
// 實現(xiàn)功能: 釋放音頻、視頻
// 參數(shù): 無
// 返回值: 無
//=====================================================================
void FreeAV()
{
//-------------------------視頻--------------------------//
if(isCameraEnabled)
capStopCamera(0);
//-------------------------音頻--------------------------//
g_pOut-》StopPlay();
g_pIn-》StopRec();
delete g_pOut;
delete g_pIn;
//------------------------UDP------------------------//
m_CEUdp.Close();
}
//=====================================================================
// 語法格式: void RecAndPlay(WPARAM wParam,LPARAM lParam)
// 實現(xiàn)功能: 接收網(wǎng)絡傳來的音頻,以及播放
// 參數(shù): wParam,表示數(shù)據(jù);lParam,表示數(shù)據(jù)長度
// 返回值: 無
//=====================================================================
static void CALLBACK OnUdpCERecv(CWnd * pWnd,char* buf,int nLen,sockaddr * addr)
{
g726_Decode(buf,(unsigned char*)waveout);
g_pOut-》Play(waveout,AudioData_Size);
}
//=====================================================================
// 語法格式: static void OnRecCapAndSend(char *data,int length,DWORD userdata)
// 實現(xiàn)功能: 錄音,攝像并且發(fā)送
// 參數(shù): data表示數(shù)據(jù),length表示數(shù)據(jù)長度,userdata暫時沒用
// 返回值: 無
//=====================================f================================
static void OnRecCapAndSend(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);
//-------------------------視頻--------------------------//
if(isCameraEnabled==false)//如果程序不能用攝像頭
return;
Sleep(15);
int index=0;
memset(lpFrameBuffer, 0, dwSize);
memset(lpJpgBuffer, 0, dwJpg);
dwRtnSize[0] = dwRtnSize[1] = 0;
if (capGetPicture(index, lpFrameBuffer, dwSize, lpJpgBuffer, dwJpg, dwRtnSize) == 0)
{
/// m_CEUdp.SendData((const char *)lpJpgBuffer,dwRtnSize[1]);
char tmp[1440];
int tmp_i=0;
for(int i=0;i《dwRtnSize[1];i++)
{
tmp[tmp_i]=lpJpgBuffer[i];
tmp_i++;
if(tmp_i==1440)
{
m_CEUdp.SendData(tmp,1440);
tmp_i=0;
}
else if(i==dwRtnSize[1]-1)
{
m_CEUdp.SendData(tmp,dwRtnSize[1]-(dwRtnSize[1]/1440)*1440);
tmp_i=0;
}
}
}
}
};
以下是PC部分的關鍵代碼
#pragma once
#include “winsock2.h”
//UDP支持
#include “。.UDPUDP.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ù)當前索引
class AVClass
{
private:
public:
//=====================================================================
// 語法格式: void InitAV(CWnd * p)
// 實現(xiàn)功能: 初始化音頻和視頻,用于錄音、播放音頻,以及播放視頻
// 參數(shù): p為窗口類指針
// 返回值: 無
//=====================================================================
void InitAV(CWnd * p,int local_port,CString remote_ip,int remote_port)
評論