Wince-based network audio and video communication

Wince network audio and video communication

This article refers to the address: http://

Wince network audio and video communication


The lower computer is mini2440, its operating system is WINCE 5.0, you need to insert USB camera on the lower computer (Zhongxing Micro 301, drive is 15 minutes limited edition), and insert the microphone headset;

The host computer is an ordinary PC and needs to be inserted into a microphone headset;

The development environment of the upper computer and the lower computer is Visual Studio 2005.

Next, talk about the function of this stuff, and how it works:

Network video transmission, first connect the mini2440 with the USB camera, capture the image through the USB camera (320 x 240, 15fps), then send it to the PC through the UDP protocol, and display it on the PC screen at 15fps.

Network audio transmission, first insert a microphone headset on both the mini2440 and the PC, both of which are recorded at the same time, encoded with G.726, and then sent to the other party through the UDP protocol, while at the same time recording, also from the other party The compressed package is decoded with G.726 and then played back in the headphones.

Finally, talk about this version compared to previous improvements:

Change the previous RTP to UDP. Practice has proved that in this system, the efficiency of using UDP will be better than RTP. After all, it does not need to use RTP traffic monitoring and other advanced functions, only communication can be done;

The previous version is based on 100M network card. Now it supports 10M network card. On the machine with 100M network card, you can directly send JPEG pictures. However, in 10M network card, the maximum data packet is 1440 bits per time, so it is compatible with 10M network card. It is necessary to split the image into multiple 1440-sized data packets and send them separately.

Network audio and video communication

Let's talk about the key source code:

The following is the code for the WINCE part.

#pragma once

#include "winsock2.h"

//RTP support

#include "..UDPUDP.h"

//audio support

#include "WaveIn.h"

#include "WaveOut.h"

//G726 support

#include "g726.h"

//Camera support

#include ".video\zc030xlib.h"

#define Video_Width 320 //Video Width

#define Video_Height 240 //Video length

#define AudioData_Size 960 //The size of each audio packet

#define Compr_AudioData_Size 120 //The size of the audio block after compression

//audio input and output variables

CWaveIn *g_pIn;

CWaveOut *g_pOut;

Char pin[AudioData_Size], pout[Compr_AudioData_Size];

Char waveout[AudioData_Size];

//Camera input variable

DWORD dwSize;

DWORD dwJpg;

DWORD dwRtnSize[2];/* 0 - for bmp, 1 - for jpeg */

LPBYTE lpFrameBuffer;

LPBYTE lpJpgBuffer ;

/ / Control variables

Bool isCameraEnabled;

//UDP

CUDP_CE m_CEUdp;

Class AVClass

{

Public:

//======================================================== ======================

// Syntax: void InitAV(CWnd * p)

// Implementation function: Initialize audio and video for recording, playing audio, and playing video

// Parameters: p is the window class pointer

// return value: none

//======================================================== ======================

Void InitAV(CWnd * p,int local_port,CString remote_ip,int remote_port)

{

//----------------------- Initialize UDP----------------------- //

m_CEUdp.m_OnUdpRecv = OnUdpCERecv;

DWORD nResult = m_CEUdp.Open(p,local_port,remote_ip,remote_port);

If (nResult <<=0)

{

AfxMessageBox (_T ("open port failed"));

Return;

}

//------------------------ Control Variables ---------------------- -//

isCameraEnabled=false;

//-------------------------video---------------------- ----//

Int i = capInitCamera();

dwSize = 320 * 240 * 3;

dwJpg = 40960;

lpFrameBuffer = (LPBYTE) malloc (dwSize);

lpJpgBuffer = (LPBYTE) malloc (dwJpg);

If(i"=0)

{

//::MessageBox(NULL, L "Init camera error", L "Notice", 0);

Goto video_error; / / error, free up space

}

If (0 != capSetVideoFormat(0, VIDEO_PALETTE_RGB24, VIDEO_SIZE_SIF))

{

//::MessageBox(NULL, L"SetVideoFormat error", L "Notice", 0);

Goto video_error; / / error, free up space

}

If (capStartCamera(0) != 0)

{

//::MessageBox(NULL, L "StartCamera error", L "Notice", 0);

capStopCamera(0);

Goto video_error; / / error, free up space

}

/ / No error, video (control state), audio settings

isCameraEnabled=true;

Goto audio;

Video_error:

Free (lpFrameBuffer);

lpFrameBuffer = NULL;

Free (lpJpgBuffer);

lpJpgBuffer = NULL;

//-------------------------Audio---------------------- ----//

Audio:

g_pOut = new CWaveOut();

g_pIn = new CWaveIn();

g_pOut-》StartPlay();

g_pIn-》StartRec(OnRecCapAndSend,(DWORD)p);

}

//======================================================== ======================

// Syntax: void FreeAV()

/ / Realize the function: release audio, video

// Parameters: None

// return value: none

//======================================================== ======================

Void FreeAV()

{

//-------------------------video---------------------- ----//

If(isCameraEnabled)

capStopCamera(0);

//-------------------------Audio---------------------- ----//

g_pOut-"StopPlay();

g_pIn-"StopRec();

Delete g_pOut;

Delete g_pIn;

//------------------------UDP----------------------- -//

m_CEUdp.Close();

}

//======================================================== ======================

// Syntax: void RecAndPlay(WPARAM wParam, LPARAM lParam)

/ / Realize the function: Receive audio from the network, and play

// Parameters: wParam for data; lParam for data length

// return value: none

//======================================================== ======================

Static void CALLBACK OnUdpCERecv(CWnd * pWnd, char* buf, int nLen, sockaddr * addr)

{

g726_Decode(buf,(unsigned char*)waveout);

g_pOut-"Play(waveout,AudioData_Size);

}

//======================================================== ======================

// Syntax: static void OnRecCapAndSend(char *data,int length,DWORD userdata)

/ / Realize the function: Record, video and send

/ / Parameters: data for data, length for data length, userdata is temporarily useless

// return value: none

//============================================================== =======================

Static void OnRecCapAndSend(char *data,int length,DWORD userdata)

{

//-------------------------Audio---------------------- ----//

Memcpy(pin,g_pIn-"buffer,AudioData_Size);

g726_Encode((unsigned char*)pin,pout);

m_CEUdp.SendData(pout,Compr_AudioData_Size);

//-------------------------video---------------------- ----//

If(isCameraEnabled==false)//If the program can't use the camera

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;

}

}

}

}

};

Here are the key codes for the PC section.

#pragma once

#include "winsock2.h"

//UDP support

#include "..UDPUDP.h"

//audio support

#include "WaveIn.h"

#include "waveout.h"

//G726 support

#include "g726.h"

//Video support

#include "Gdiplus.h"

Using namespace Gdiplus;

#define VideoData_Size 1440 //The size of each video packet

#define Video_Width 320 //Video Width

#define Video_Height 240 //Video length

#define AudioData_Size 960 //The size of each audio packet

#define Compr_AudioData_Size 120 //The size of the audio block after compression

//audio input and output variables

CWaveIn *g_pIn;

CWaveOut *g_pOut;

Char pin[AudioData_Size], pout[Compr_AudioData_Size];

Char wave_data[AudioData_Size];

//UDP variable

CUDP_CE m_CEUdp;

/ / Video input variables

GdiplusStartupInput m_gdiPlusInPut;

ULONG_PTR m_gdiPlusToken;

Char video_data[Video_Width*Video_Height];

Int index;//video data current index

Class AVClass

{

Private:

Public:

//======================================================== ======================

// Syntax: void InitAV(CWnd * p)

// Implementation function: Initialize audio and video for recording, playing audio, and playing video

// Parameters: p is the window class pointer

// return value: none

//======================================================== ======================

Void InitAV(CWnd * p,int local_port,CString remote_ip,int remote_port)

{

//------------------------- UDP connection --------------------- -----//

m_CEUdp.m_OnUdpRecv = OnUdpCERecv;

DWORD nResult = m_CEUdp.Open(p,local_port,remote_ip,remote_port);

If (nResult <<=0)

{

AfxMessageBox (_T ("open port failed"));

Return;

}

//-------------------------Audio---------------------- ----//

g_pOut = new CWaveOut();

g_pIn = new CWaveIn();

g_pOut-》StartPlay();

g_pIn-》StartRec(OnRecording,(DWORD)p);

//-------------------------video---------------------- ----//

GdiplusStartup( &m_gdiPlusToken, &m_gdiPlusInPut, NULL ); //Initialize GDI+

Memset(video_data,0,Video_Width*Video_Height);

Index=0;

}

//======================================================== ======================

// Syntax: void FreeAV()

/ / Realize the function: release audio, video

// Parameters: None

// return value: none

//======================================================== ======================

Void FreeAV()

{

//-------------------------Audio---------------------- ----//

g_pOut-"StopPlay();

g_pIn-"StopRec();

Delete g_pOut;

Delete g_pIn;

//-------------------------video---------------------- ----//

GdiplusShutdown(m_gdiPlusToken); //Destroy GDI+

//------------------------UDP----------------------- ---//

m_CEUdp.Close();

}

//======================================================== ======================

// Syntax: void RecAndPlay(WPARAM wParam, LPARAM lParam, HWND hwnd)

/ / Realize the function: Receive audio from the network, and play

// Parameters: wParam for data; lParam for data length; hwnd for window handle for displaying video

// return value: none

//======================================================== ======================

Static void CALLBACK OnUdpCERecv(CWnd *pWnd,char* buf,int nLen,sockaddr * addr)

{

/* test the size of the received data

CString tmp;

tmp.Format(L"%d", nLen);

MessageBox(0,tmp,0,0);

Return;*/

//------------------------- If it is audio data ------------------- -------//

If(nLen==Compr_AudioData_Size)

{

g726_Decode(buf,(unsigned char*)wave_data);

g_pOut-"Play(wave_data,AudioData_Size);

Return;

}

//------------------------- If it is video data ------------------- -------//

If(nLen==VideoData_Size)//complete video data block

{

For(int i=0;i"nLen;i++)

{

Video_data[index]=buf[i];

Index++;

}

Return;

}

//The last piece of the video data block

For(int i=0;i"nLen;i++)

{

Video_data[index]=buf[i];

Index++;

}

/ / If the JPEG image is particularly large, it is definitely an error, then discard

If(index)Video_Width*Video_Height)

{

//MessageBox (0, "buffer error", "error message", 0);

Return;

}

Try{

IPicture *pPic;

IStream *pStm ;

/ / allocate global storage space

HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,index);

LPVOID pvData=NULL ;

/ / Lock the allocation of memory blocks

pvData=GlobalLock(hGlobal);

/ / Copy the packet video_data to 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);

/ / Load the graphic file

If(FAILED(OleLoadPicture(pStm,index,TRUE,IID_IPicture,(LPVOID*)&pPic)))

{/ / Appendix: If the video_data array contains an image, OleLoadPicture is easy to generate read and write memory errors

// 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();// will error

mGraphics.~Graphics();

pPic-"Release();

pStm-"Release();

}

Catch(CException * e)

{}

Memset(video_data,0,Video_Width*Video_Height);

Index=0;

}

//======================================================== ======================

// Syntax: static void OnRecording(char *data,int length,DWORD userdata)

// Implementation function: Release audio

/ / Parameters: data for data, length for data length, userdata is temporarily useless

// return value: none

//======================================================== ======================

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);

}

};

Offshore communication cable

Name:0.6/1kV low smoke halogen free offshore communication cable

Model:S1 RFOU(I), S2 RFOU(C), S3 BFOU(I), S4 BFOU(C)

S1/S5 RFOU(I), S2/S6 RFOU(C), S3/S7 BFOU(I), S4/S8 BFOU(C)

Specification:(1~37 Pairs)×(0.75~2.5)mm2,  (1~37 Triples)×(0.75~2.5)mm2

Executive Standards:IEC60092-350, NEK606

Application:The cable is mainly intended for instrumentation, communication, control, and alarm system of offshore units, The codes S1/S5, S2/S6, S3/S7, S4/S8 meets the mud resistant requirements in NEK606.

Flame Retardant Marine Communication Cable

4 Core Cable,Flame Retardant Marine Communication Cable,Offshore Communication Cable Flame Retardant,Halogen Free Flame Retardant Shipboard Cable

Jiangsu Jiangyang Special Cable Co,.Ltd. , http://www.jymarinecable.com

This entry was posted in on