Lan-based point-to-point voice communication

Source: Internet
Author: User
Vckbase online help Journal No. 10

Lan-based point-to-point voice communication
Changsha Yan zhenwei Yan Fei Cai xuanping
Introduction
With the increasing popularity of computer networks, communication over networks becomes more and more important. As a result, a series of voice communication software such as NetMeeting, ipphone, mediaring, and voxphone have emerged, however, these software functions are complete and relatively independent, which is not conducive to integration into the software developed by ourselves. Sometimes we also hope to integrate such voice communication functions into our own software, especially when a LAN user is scattered in different rooms. This article provides a flexible and simple implementation method, using dialog box-based programming, hardware only needs a dual DMA channel sound card (currently most sound cards support dual DMA channels) and a headset, and the rest are all implemented by software programming. The program is compiled in Windows 98/2000, Visual C ++ 6.0, and runs well on Windows NT 100 M Ethernet.
Design Concept
To achieve point-to-point voice communication, the principle is very simple. As long as real-time voice collection, processing, and playback are realized for a single point, reliable transmission and receiving can be done at the same time, so that 2.1 connections can be called. For the former, it is more appropriate to use the Windows MDK low-level audio service, because the callback mechanism in the lower-level audio service provides us with great convenience. When the application continuously provides audio data to the device driver, the device driver controls the audio device to complete recording and playing operations in the background. Through the callback mechanism, we can also detect when a data block is used up and transmit the next data block in time to ensure sound continuity. With the real-time collection and playback functions on a single machine, the next step is to transmit voice data over the network. In terms of point-to-point network transmission, select the connection-oriented TCP protocol. The TCP transmission protocol automatically handles packet loss and delivery out-of-order problems, so that we don't have to worry about these problems. We only need to make good use of this connection, before collecting voice playback, You can transmit your voice to the network, and receive the voice from the network. This enables point-to-point voice communication. The structure diagram is as follows:
Implementation
I. Real-time voice collection, processing, and Playback
First, we need to introduce the windows low-layer waveform audio data block structure wavehdr. Its declaration is as follows:
Type struct {
Lpstr lpdata; // pointer to the locked Data Buffer
DWORD dwbufferlength; // data buffer size
DWORD dwbyterecorded; // specifies the data volume in the buffer during recording.
DWORD dwuser; // user data
DWORD dwflag; // a flag that provides the buffer information.
DWORD dwloops; // Number of loop playback times
Struct wavehdr_tag * lpnext; // Reserved
DWORD reserved; // Reserved
} Wavehdr;
Sound acquisition and playback are all in the operation of this audio data block structure, in fact, the main use is the first member variable lpdata, so we only need to allocate the buffer (memory) and then assign the buffer pointer to the member variable lpdata of the corresponding data block structure, so that when a buffer is filled up, that is, when an audio data block is filled up, you can process and play it in the message function through the message mechanism, after playing the video, you can use the message function to send the buffer to the audio device and input the driver to continue the collection and playback, after you allocate multiple buffers and data block structures at a time and assign them to the audio device input driver, you can fill in the buffer and then assign the empty buffer to the device input driver, without human intervention, it is completely controlled by windows. This is a simple and clever way to use a dynamic loop buffer to achieve real-time voice acquisition and playback. Steps:
1. Initialization
① Use waveingetnumdevs () and waveoutgetnumdevs () to view the waveform audio input and output devices of the current system;
② Set the member variables of the waveformatex structure in the format of 11025Hz, 16 bit, single channel and 22 K/S, or change them to other wave formats;
③ Use waveinopen (...) and waveoutopen (...) to call the wave_format_query parameter to check whether the waveform input device supports the specified format;
④ Call the callback_window parameter again using waveinopen (...) and waveoutopen (...) to open the waveform input device;
⑤ Allocate and lock the global memory to the audio data block and audio data buffer respectively;
6. initialize the member variables of the audio data block structure, mainly assigning each buffer pointer to the buffer pointer variable lpdata in the corresponding data block structure; Calling waveinprepareheader (...) and waveinaddbuffer (...) assign the audio data block to the driver of the input device;
7. Call the waveinstart (...) function to start recording.
2. Message operations
After the recording starts, the device driver sends the message mm_wim_data to the user window every time the sampled data is filled with data blocks. The message callback function onmmwimdata (...) process the sample data in the data block and send it to the output device for playback. the device driver sends out the message mm_wom_done every time the audio data block is played back, corresponding message callback function onmmwomdone (...) record the audio data and re-Send it to the input device after necessary preparation to prepare for receiving subsequent sampling data. In this way, the audio data block originally prepared for the input device is controlled by the message and used repeatedly between the input and output devices. Real-time acquisition, processing, and playback are achieved without human control.
When the call ends, you must disable the audio input device. At this time, the audio device driver will send the mm_wim_close message. You can use the onmmwimclose (...) function in the corresponding message function (..) to clear the audio data blocks assigned to the input and output devices.
Ii. Point-to-point voice transmission based on TCP protocol
The connection-oriented TCP protocol is used for sound transmission and receiving, and network programming is implemented using Windows Socket. However, the function interface for sending and receiving must be placed in onmmwimdata (...) first (...) function. Windows Socket should not be unfamiliar to people who have been engaged in network programming. Because we need to implement point-to-point communication, we have to combine the customer and server modes into one mode so that the server can be a customer, the customer can also act as a server so that both parties have the ability to call the other party and receive the call from the other party. This only requires adding a listening socket. Once the call connection is established successfully, a data stream is established between the two points. Even if both parties do not speak, each point continuously receives and sends data, the voice of one Party naturally passes the data stream to the other party, so the key issue is how to read the voice data stream, because the stream service provided by TCP does not guarantee the boundary, when the sender wants to send 4000 bytes at a time, the call statement send (sbuffer, 4000) does not guarantee that 4000 bytes can be sent. Similarly, the receiver prepares to receive the data sent at a time, the call statement receive (rbuffer, 4000) cannot guarantee that it can receive 4000 bytes. Therefore, the actual number of bytes sent and received at a time is any value between 1 and 4000, the worst case is that there is only one byte. On the contrary, if you use the send function to send a small amount of data continuously, for example, if you send 400 bytes at a time and send 10 times in a row, the receiver may receive the 4000 bytes at a time using the receive function, in order to achieve playback, we hope to call a sending function to send voice data of the buffer size. by calling a receiving function, we can accurately receive the voice data sent by the other party, therefore, a simple and practical method is to add a flag header for each data packet when data is sent using the TCP protocol:
This flag header contains a long (4-byte) voice data value and a flag string. in the program, you can set an offset for the two marks and an offset for the voice data, by reloading onreceive (...). Before receiving, the offset is set to 0. After receiving, the system checks whether the 4-byte voice data volume value is received and the string flag. If no value is received, the offset is used to control the accurate receipt of the strings. Then, the correctness of the string flag is verified. If both marks are correctly received, the real voice data is received based on the size of the received data, if you call receive (...) once in the onreceive Function (...) if this value is not received, the non-blocking mode is used to call asyncselect (...) the function continues to receive all the messages until they are received. In this way, the onreceive (...) is reloaded (...) after receiving the function, you can receive the data sent by the other party at a time. Similarly, we can use the onsend function of casyncsocket to load the data in a buffer zone at a time. In this way, you only need to put the interface of the sending and receiving function in the onmmwimdata (...) function to generate a constant rate of data stream for both the receiving and sending, thus realizing the transmission and playback of network voice.
3. interface and other functions
The interface and other functions should also be designed:
1. set the "Search for neighbors" item to check who is online and use ListBox to display it. Select a neighbor in ListBox and the neighbor editing box will display the neighbor for easy call, the main function used for "Finding neighbors" is wnetopenenum (...), wnetenumresource (...), wnetcloseenum (...).
2. the program is based on the dialog box. After running the program, it is not displayed. directly put it into the system tray until you double-click the tray icon or select the menu to run the program. However, you can monitor whether there is call access at any time, once the connection is established, the voice processing function is enabled immediately.
3. Each running operation writes or modifies the netphone entry in the runservicesonce key of the Registry, enabling the program to run automatically after it is started.
4. added the microphone and headset volume adjustment function.
5.in contrast, the music program only supports.

1997-2000 vckbase. com All Rights Reserved.

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.