Develop video chat software using Delphi

Source: Internet
Author: User
Tags bmp image

Abstract: currently, online video chat software, video conferencing software, and visual IP Phone software can be seen everywhere. Do you want to do it yourself? In fact, this type of software is built with video and network. If you are familiar with video capture and network transmission technology, it's hard for you. Microsoft provides software developers with a vfw sdk (Video for Windows SDK) dedicated for Video capturing. It is very easy to use it for Video capturing. As for network transmission, delphi provides more than N network components that you can use. This article describes in detail how to use Delphi to develop video chat software, and provides two program examples.

Keywords: Delphi VFW Video conferencing Video chat Video for Windows

I. Introduction

We know that the key technology of video chat software is video collection and real-time transmission to chat software online users. For Video collection, here we use a Microsoft software package on digital Video VFW (Video for Windows ). I believe many people are familiar with it. VFW enables applications to obtain digital video clips from traditional analog video sources through digital devices, A key idea of VFW is that dedicated hardware is not required during playback. To solve the problem of large data volumes in digital videos, data needs to be compressed, while VFW introduces AVI file standards. This standard does not specify how to capture, compress, and play videos, only specify how videos and audios should be stored on hard disks and stored alternately in AVI files. With VFW, developers can send messages or set attributes to capture, play, and edit video clips. When you install VFW, the installer automatically installs the components required for video configuration, such as the device driver and video compression program. VFW consists of six modules. VFW function module:

AVICAP. DLL contains the video capture function, which provides an Advanced Interface for I/O processing of AVI files and video and audio device drivers.

MSVIDEO. DLL contains a set of special DrawDib functions to process video operations on the screen.

MCIAVI. DRV includes the driver for the MCI command interpreter for VFW

AVIFILE. DLL contains higher commands provided by standard multimedia I/O (mmio) functions to access. AVI files.

The ICM compression manager, which is used to manage the video compression/Decompression encoding/Decoder (Codec)

ACM audio compression manager, which provides services similar to ICM and is suitable for Waveform audio.

For video transmission, we use UDP for transmission. Because UDP transmission speed is fast, TCP is connection-oriented. When establishing a connection, both parties must undergo three handshakes to ensure reliable data transmission, FTP, telnet, and so on are based on TCP. UDP is for non-connection purposes, and sending information does not need to be confirmed by the other party. However, this speed is faster than TCP, but data may be lost, such as SMTP and tftp are based on UDP. In addition, UDP also supports broadcast and UDP broadcast. One is directed broadcast. For example, if your network segment is 192.168.0.X, you can send it to 192.168.0.255. The other is limited broadcast. The broadcast address is 255.255.255.255.

2. Develop the video chat software

2.1 create a capture window to capture videos

Before capturing a video, you must create a capturing window and perform all the capturing and setting operations on this basis. The capture window can be created using the "CapCreateCaptureWindow" function of the AVICap window class. The window style can be set to the WSCHILD and WS_VISIBLE parameters.

With the capture window, we can capture video streams and audio streams to an AVI file; dynamically connect to or disconnect from the video and audio input devices; displays the input video stream in real time in Overlay or Preview mode, sets the capture rate, displays the dialog box that controls the video source, video format, and video compression, and creates, saves, or loads the color palette, copy the image and related color palette to the clipboard and save the captured single-frame image to a BMP file.

2.2 capture the associations between windows and drivers

Only one capturing window cannot work. It must be associated with a device to obtain the video signal. The CapDriverConnect function associates the capture window with its device driver.

2.3 set the attributes of a video device

By setting each member variable of the TcaptureParms structure variable, you can control the sampling frequency, interrupt sampling buttons, and status behaviors of the device. After setting the TcaptureParms structure variable, you can use the letter CapCaptureSetSetup to make the setting take effect. You can also use CapPreviewScale and CapPreviewRate to set the preview ratio and speed, or use the default value of the device.

2.4 open Preview

You can use the CapOverlay function to select whether to preview in Overlay mode to reduce system resource usage and accelerate video display. Use CapPreview to start the preview function. Then, you can view the image from the camera on the screen.

2.5 use the capture window callback function

You can create a basic video capturing program in the first four steps. If you want to process the video data captured from the device, use the capture window callback function, for example, you can obtain video data at a frame or stream.

2.6 transmit video streams

The callback function can be used to obtain the first frame of data. We use network technology to send the data to other machines, and other machines will display the received data.

2.7 receive video

When receiving UDP data and displaying the received data, you can see the video from a distance.

3. Use Delphi to compile program code

Microsoft's vfw sdk only has VC and VB versions, and does not have Delphi versions. However, you can find the VFW. PAS file on the Internet. The FW. PAS file declares various functions and variables in the DLL call. (Note: The VFW. PAS file is provided in the source code)

The following describes how to develop an online video chat software with Delphi7. The chat software is divided into two programs: one is the video capture program and the other is the UDP broadcast video chat software server, the other is the video chat software client that receives the video data from the UDP broadcast program.

3.1 create a video chat software server

3.1.1 create a new project named Project1.dpr and add VFW. PAS to USE

 

 

3.1.2 place a Tpanel control on Form1 to display videos. Then place two Tbutton controls, one caption is "start", and the other Name is "stop". Place a UDP component. Here the IdUDPClient of indy is used to transmit the video, as shown in:

 

3.1.3 define global variables

CapWnd: THandle; // defines the capture window handle.
CapParms: TcaptureParms; // structure variable used to set device Properties
BMP info: TBitmapInfo; // BMP Image Information

3.1.4 encode the Event code
 
Start button code:

CapWnd: = capCreateCaptureWindow ('My Windows', WS_VISIBLE or WS_CHILD, // window style 0, // X coordinate 0, // Y coordinate panel1.Width, // window width panel1.Height, // window height panel1.handle, // window handle 0); // usually 0
If CapWnd = 0 then exit; // defines the frame capture callback function.
CapSetCallbackOnFrame (CapWnd, FrameCallBack );
CapParms. dwRequestMicroSecPerFrame: = 1;
CapParms. fLimitEnabled: = FALSE;
CapParms. fCaptureAudio: = FALSE;
CapParms. fMCIControl: = FALSE;
CapParms. fYield: = TRUE;
CapParms. vKeyAbort: = VK_ESCAPE;
CapParms. fAbortLeftMouse: = False;
CapParms. fAbortRightMouse: = FALSE; // make the settings take effect
CapCaptureSetSetup (capWnd, @ CapParms, sizeof (TCAPTUREPARMS ));
CapPreviewRate (capWnd, 33); // you can specify the video preview frequency.
CapCaptureSequenceNoFile (capWnd); // if you want to capture a video stream, use a function to specify that the file is not generated. Otherwise, the AVI file CapDriverConnect (CapWnd, 0) is automatically generated ); // connect the camera device. The second parameter is a serial number. When multiple display drivers are installed in the system, the values are 0 to the total number. If multiple cameras exist, then it is 0-> 1-> 2.
CapGetVideoFormat (capWnd, @ BMP info, sizeof (TBitmapInfo); // get the video image data Header
CapPreviewScale (capWnd, TRUE); // whether to scale
CapOverlay (capWnd, true); // specifies whether overlay mode is used. true indicates use; otherwise, falseCapPreview (capWnd, true) indicates use );

Callback Function Code:

Var hd: Thandle;
Jpg: tsf-image;
MemStream: TMemoryStream;
Bitmap: TBitmap;
Begin // display data in Image,
Bitmap: = TBitmap. Create;
Bitmap. Width: = BMP info. bmiHeader. biWidth;
// New size of Bitmap
Bitmap. Height: = BMP info. bmiHeader. biHeight;
Hd: = DrawDibOpen; DrawDibDraw (hd, Bitmap. canvas. handle, 0, 0 ,_
BMP info. BmiHeader. biwidth, BMP info. bmiheader. biheight ,_
@ BMP info. bmiHeader, lpVHdr ^. lpData, 0, 0, BMP info. bmiHeader. biWidth ,_
BMP info. bmiHeader. biheight, 0 );
DrawDibClose (hd); // send data
MemStream: = TMemoryStream. Create;
Jpg: = tsf-image. Create;
Jpg. Assign (Bitmap );
Jpg. CompressionQuality: = 10; // jpg compression quality
Jpg. effecneeded;
Jpg. Compress;
Jpg. SaveToStream (memStream );
Jpg. Free; // The UDP packet size is limited. If the part is exceeded, it is not transmitted and can be sent several times.
Form1.IdUDPClient1. BroadcastEnabled: = true; // Broadcast Function
If memStream. Size> Form1.IdUDPClient1. BufferSize then
// Broadcast to the 192.168.0.X CIDR Block, port 9001
Form1.IdUDPClient1. sendBuffer ('2017. 168.0.255 ', 9001, memStream. memory ^, Form1.IdUDPClient1. bufferSize) else Form1.IdUDPClient1. sendBuffer ('2017. 168.0.255 ', 9001, memStream. memory ^, memStream. size );
MemStream. Free;
Bitmap. Free;
End;

Stop code:

CapCaptureAbort (CapWnd); // stop capturing capDriverDisconnect (CapWnd); // disconnect the capture window from the drive

Complete video chat software server code:

Unit Unit1;
Interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, VFW, IdBaseComponent, IdComponent, IdUDPBase, IdUDPClient, jpeg; type TForm1 = class (TForm) Panel1: TPanel;
Button1: TButton;
Button2: TButton;
IdUDPClient1: TIdUDPClient;
Procedure Button1Click (Sender: TObject );
Procedure Button2Click (Sender: TObject );
Private {Private declarations}
Public {Public declarations}
End;

Var Form1: TForm1;
CapWnd: THandle; // defines the capture window handle.
CapParms: TcaptureParms; // structure variable used to set device Properties
BMP info: TBitmapInfo; // BMP Image Information
Implementation {$ R *. dfm}
Function FrameCallBack (hWnd: HWND; lpVHdr: PVIDEOHDR): LongInt; stdcall;
Var hd: Thandle;
Jpg: tsf-image;
MemStream: TMemoryStream;
Bitmap: TBitmap;
Begin // display data in Image,
Bitmap: = TBitmap. Create;
Bitmap. Width: = BMP info. bmiHeader. biWidth;
// New size of Bitmap
Bitmap. Height: = BMP info. bmiHeader. biHeight;
Hd: = DrawDibOpen;
DrawDibDraw (hd, Bitmap. canvas. handle, 0, 0, BMP info. BmiHeader. biwidth, BMP info ._
Bmiheader. biheight, @ BMP info. bmiHeader ,_
LpVHdr ^. lpData, 0, 0, BMP info. bmiHeader. biWidth, BMP info. bmiHeader. biheight, 0 );
DrawDibClose (hd );
// Send data
MemStream: = TMemoryStream. Create;
Jpg: = tsf-image. Create; jpg. Assign (Bitmap );
Jpg. CompressionQuality: = 10;
// Jpg compression quality
Jpg. effecneeded;
Jpg. Compress;
Jpg. SaveToStream (memStream );
Jpg. Free;
// Because the UDP packet size limit exists, if the part is exceeded, it is not transmitted and can be sent several times
Form1.IdUDPClient1. BroadcastEnabled: = true; // Broadcast Function
If memStream. Size> Form1.IdUDPClient1. BufferSize then
// Broadcast to the 192.168.0.X CIDR Block, port 9001
Form1.IdUDPClient1. SendBuffer ('192. 168.0.255 ', 192, memStream. Memory ^, Form1.IdUDPClient1. BufferSize)
Else
Form1.IdUDPClient1. SendBuffer ('192. 168.0.255 ', 192, memStream. Memory ^, memStream. Size );
MemStream. Free;
Bitmap. Free;
End;
Procedure TForm1.Button1Click (Sender: TObject );
Begin
CapWnd: = capCreateCaptureWindow ('My Windows', WS_VISIBLE or WS_CHILD, // window style
0, // X coordinate
0, // Y coordinate
Panel1.Width, // window width
Panel1.Height, // window height
Panel1.handle, // window handle
0); // usually 0
If CapWnd = 0 then exit; // defines the frame capture callback function.
CapSetCallbackOnFrame (CapWnd, FrameCallBack );
CapParms. dwRequestMicroSecPerFrame: = 1;
CapParms. fLimitEnabled: = FALSE;
CapParms. fCaptureAudio: = FALSE;
CapParms. fMCIControl: = FALSE;
CapParms. fYield: = TRUE;
CapParms. vKeyAbort: = VK_ESCAPE;
CapParms. fAbortLeftMouse: = False;
CapParms. fAbortRightMouse: = FALSE; // make the settings take effect
CapCaptureSetSetup (capWnd, @ CapParms, sizeof (TCAPTUREPARMS ));
CapPreviewRate (capWnd, 33); // you can specify the video preview frequency.
CapCaptureSequenceNoFile (capWnd); // if you want to capture a video stream, use a function to specify not to generate a file. Otherwise, an AVI file is automatically generated.
CapDriverConnect (CapWnd, 0); // connect to the camera device. The second parameter is the serial number. When multiple display drivers are installed in the system, the values are 0 to the total number of cameras. If there are multiple cameras, the values are 0-> 1-> 2 capGetVideoFormat (capWnd, @ BMP info, sizeof (TBitmapInfo )); // obtain the video image data Header
CapPreviewScale (capWnd, TRUE); // whether to scale
CapOverlay (capWnd, true); // specifies whether overlay mode is used. If true is used, false is CapPreview (capWnd, true); end; procedure TForm1.Button2Click (Sender: TObject );
Begin captureabort (CapWnd); // stop capturing
CapDriverDisconnect (CapWnd); // disconnect the capture window from the drive
End;
End.

3.2 Create a video chat client

 

 

3.2.1 create a project named Project2.dpr

3.2.2 place an image control on the Form2 program window, which is used to receive the image content, and a Tbutton control with the caption as "receive" and a UDPServer component, here, the IdUDPServer of indy is used to receive Network Videos, as shown in:

Code of the receive button:

IdUDPServer1.DefaultPort: = 9001; // The receiving port IdUDPServer1.Active: = true; // Enabled

UDPRead Event code of IdUDPServer1:

Var jpg: Tsung image; begin try jpg: = Tsung image. Create; jpg. LoadFromStream (Adata); Image1.Picture. Bitmap. Assign (jpg); jpg. Free; interval tend; end;

The video is compressed to JPG for transmission. The jpeg unit is used on the server side and the receiver side, so jpeg must be added to the use.

Complete client code of the video chat software:

Unit Unit2;
Interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, ExtCtrls, jpeg, IdSocketHandle;
Type TForm1 = class (TForm) Image1: TImage;
IdUDPServer1: TIdUDPServer;
Button1: TButton;
Procedure Button1Click (Sender: TObject );
Procedure IdUDPServer1UDPRead (Sender: TObject;
AData: TStream; ABinding: TIdSocketHandle );
Private {Private declarations}
Public
{
Public declarations
}
End;
Var Form1: TForm1;
Implementation {$ R *. dfm} procedure TForm1.Button1Click (Sender: TObject );
Begin
IdUDPServer1.DefaultPort: = 9001; // receiving port
IdUDPServer1.Active: = true; // Enabled
End;
Procedure TForm1.IdUDPServer1UDPRead (Sender: TObject; AData: TStream; ABinding: TIdSocketHandle );
Var jpg: tsf-image;
Begin
Try
Jpg: = tsf-image. Create;
Jpg. LoadFromStream (Adata );
Image1.Picture. Bitmap. Assign (jpg );
Jpg. Free;
Except
End;
End;
End.

Now, the program code is finished. Run the video chat software server program on the machine and start video transmission at. On the network (the network segment is 192.168.0.X. Set the IP address based on your network. I will test it in the LAN) any machine running the video chat software client can receive the video.

If the content of the video to be received is clear, you can set jpg. compressionQuality: = 10; (this value can be from 1 to 100. The larger the value, the clearer the image. Of course, the slower the transmission speed. The clearer the image, the larger the data packet, if the UDP packet limit is exceeded, the image is incomplete)

Video chat software server

Chat Software Client

Iv. Conclusion

Here, I will share some of my experiences and code with you. Please don't throw your eggs. I have really made a lot of effort! After reading this article, I believe you can also develop an online video chat software, or a video chat software similar to MSN, QQ, or ephone, with online video, you can perform visual communication with your family thousands of miles away. The above sample program still needs to be improved. For example, video compression can be performed with other video compression encoders. Here we only talk about transmitting images without sound, you can transfer audio and video only after you change it. If you are interested, try it yourself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.