unit main;
interface
uses
Windows, Messages,variants, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp,vfw, StdCtrls;
type
SERVER_CTRL_MSG = record
strCommand:array [0..100] of char;
nContentUsedByte:dword;
strContent:array [0..1000] of char;
end;
type
video_data = record
bKeyFrame:boolean;
nSampleNum:dword;
nUsedSize:dword;
Buf:array [0..8180] of byte;
end;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure FormDestroy(Sender: TObject);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
private
procedure InitVideoCard();
//function FrameCallBack(HhWND:hwnd;lpVHdr
VIDEOHDR)
WORD;stdcall;
procedure InitNetWork();
procedure InitCompressor();
procedure UnInitCompressor();
procedure UnInitVideoCard();
procedure FillBitmapStruct();
procedure CompressFrame(lpVHdr
VIDEOHDR);
{ Private declarations }
public
{ Public declarations }
end;
function FrameCallBack(hWnd: HWND;
lpVHdr: PVIDEOHDR): DWORD;
stdcall;
var
ii:integer;
Form1: TForm1;
m_bConnect:boolean;
m_SampleNum:dword;
m_hWndCapture:hwnd;
m_OutFormatSize,m_OutBufferSize
Word;
m_OutActSize:Longint;
m_InInfo,m_OutInfo:BITMAPINFO;
m_CV:TCOMPVARS;
implementation
{$R *.dfm}
procedure TForm1.CompressFrame(lpVHdr: PVIDEOHDR);
var
bKeyFrame:boolean;
VideoData:VIDEO_DATA;
Buf:char;
begin
m_OutActSize:=m_InInfo.bmiHeader.biSizeImage;
buf:=char(ICSeqCompressFrame(@m_CV,0,lpVHdr.lpData,@bKeyFrame,@m_OutActSize));
if (m_bConnect = true) and (m_OutActSize<8180) then
begin
FillMemory(@VideoData,sizeof(VIDEO_DATA),0);
VideoData.bKeyFrame:=bKeyFrame;
movememory(@Buf,@VideoData.Buf,m_OutActSize);
VideoData.nSampleNum:=m_SampleNum;
VideoData.nUsedSize:=m_OutActSize;
serversocket1.Socket.SendBuf(videodata,sizeof(VIDEO_DATA));
m_SampleNum:=m_SampleNum+1;
end;
end;
procedure TForm1.FillBitmapStruct;
begin
m_InInfo.bmiHeader.biBitCount:=24;
m_InInfo.bmiHeader.biClrUsed:=0;
m_InInfo.bmiHeader.biClrImportant:=m_InInfo.bmiHeader.biClrUsed;
m_InInfo.bmiHeader.biCompression:=BI_RGB;
m_InInfo.bmiHeader.biHeight:=240;
m_InInfo.bmiHeader.biPlanes:=1;
m_InInfo.bmiHeader.biSize:=sizeof(BITMAPINFOHEADER);
m_InInfo.bmiHeader.biSizeImage:=0;
m_InInfo.bmiHeader.biWidth:=320;
m_InInfo.bmiHeader.biYPelsPerMeter:=0;
m_InInfo.bmiHeader.biXPelsPerMeter:=m_InInfo.bmiHeader.biYPelsPerMeter;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ii:=0;
m_bConnect:=FALSE;
m_SampleNum:=0;
FillBitmapStruct();
InitVideoCard();
InitCompressor();
InitNetWork();
end;
function FrameCallBack(hWnd: HWND;
lpVHdr: PVIDEOHDR): dword;stdcall;
begin
if (m_bConnect=TRUE) then
form1.CompressFrame(lpVHdr);
end;
procedure TForm1.InitCompressor;
begin
FillMemory(@m_CV,sizeof(TCOMPVARS),0);
m_CV.dwFlags:=ICMF_COMPVARS_VALID;
m_CV.cbSize:=sizeof(m_CV);
m_CV.cbSize:=0;
m_CV.fccHandler:=mmioFOURCC('d','i','v','x');
m_CV.fccType:=ICTYPE_VIDEO;
m_CV.hic:=ICOpen(ICTYPE_VIDEO,mmioFOURCC('d','i','v','x'),ICMODE_COMPRESS);
m_CV.lDataRate:=780;
m_CV.lFrame:=0;
m_CV.lKey:=15;
m_CV.lKeyCount:=0;
m_CV.lpbiIn:=Nil;
m_CV.lpBitsOut:=Nil;
m_CV.lpState:=Nil;
m_CV.lpBitsPrev:=Nil;
m_CV.lQ:=ICQUALITY_DEFAULT;
if (m_CV.hic<>0) then
begin
m_OutFormatSize:=ICCompressGetFormatSize(m_CV.hic,@m_InInfo);
//getmem(@m_pOutInfo,m_OutFormatSize);
//m_pOutInfo:=(BITMAPINFO *)new BYTE[m_OutFormatSize];
//ICCompressGetFormat(m_CV.hic,@m_InInfo,m_pOutInfo);
ICCompressGetFormat(m_CV.hic,@m_InInfo,@m_OutInfo);
m_OutBufferSize:=ICCompressGetSize(m_CV.hic,@m_InInfo,@m_OutInfo);
ICSeqCompressFrameStart(@m_CV,@m_InInfo);
end;
end;
procedure TForm1.InitNetWork;
begin
serversocket1.Active:=true;
end;
procedure TForm1.InitVideoCard;
var aa:THandle;
begin
m_hWndCapture:=capCreateCaptureWindow('Capture Window',WS_VISIBLE or WS_CHILD,0,0,320,240,form1.Handle,1);
capDriverConnect(m_hWndCapture,0);
//set the video format
capSetVideoFormat(m_hWndCapture,@m_InInfo,sizeof(BITMAPINFO));
capPreviewRate(m_hWndCapture,40);
// capDlgVideoFormat(this->m_hWndCapture);
capPreview(m_hWndCapture,TRUE);
capSetCallbackOnFrame(m_hWndCapture,FrameCallBack);
end;
procedure TForm1.UnInitCompressor;
begin
if (m_CV.hic <> NULL) then
begin
zeromemory(@m_OutInfo,0);
ICSeqCompressFrameEnd(@m_CV);
ICCompressorFree(@m_CV);
ICClose(m_CV.hic);
end;
end;
procedure TForm1.UnInitVideoCard;
begin
capSetCallbackOnFrame(m_hWndCapture,FrameCallBack);
capDriverDisconnect(m_hWndCapture);
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
ii:=ii+1;
label1.Caption:='User Connected,begin
transporting video image!'+inttostr(ii);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnInitCompressor();
UnInitVideoCard();
end;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
msg:SERVER_CTRL_MSG;
begin
FillMemory(@msg,sizeof(SERVER_CTRL_MSG),0);
strpcopy(msg.strCommand,'Set Bitmap Info');
//memcpy(msg.strContent,pDlg->m_pOutInfo,sizeof(BITMAPINFO));
//
movememory(@m_OutInfo,@msg.strContent,sizeof(BITMAPINFO));
//这个翻译的好像不对头
msg.nContentUsedByte:=sizeof(BITMAPINFO);
serversocket1.Socket.SendBuf(msg,sizeof(SERVER_CTRL_MSG));
FillMemory(@msg,sizeof(SERVER_CTRL_MSG),0);
strpcopy(msg.strCommand,'Set COMPVARS');
msg.nContentUsedByte:=sizeof(TCOMPVARS);
//memcpy(msg.strContent,&pDlg->m_CV,sizeof(COMPVARS));
serversocket1.Socket.SendBuf(msg,sizeof(SERVER_CTRL_MSG));
//afxDump<<pDlg->m_CV.fccType<<"/n";
//这个不懂
m_bConnect:=TRUE;
end;
end.
大家继续改,继续翻译。