WPF+WCF Step by step Create Audio Chat room (iii): voice chat

Source: Internet
Author: User
Tags port number

In the previous article, I realized the function of text chatting and sharing whiteboard, in this article, I will implement the function of voice chat on the basis of the previous article. Voice chat is much more difficult than text chat and shared whiteboard.

The approximate process of implementation is:

1. A chat room member initiates a voice chat request to another member

2, this request will be sent to the WCF service side, WCF duplex notification was invited.

3, the invited person received notice, he can choose to accept or reject voice chat requests.

4, if rejected, will notify the requestor to reject voice chat

5. If agreed, the inviter and the invitees ' clients will have voice chat, at which point the client will open a thread that plays the sound and receives the sound. Here we use an open source Wave class library, which can be downloaded in http://www.lumisoft.ee/lswww/download/downloads/Examples/. The communication of sound is used in the UdpClient class. This class uses UDP to communicate with network services. The advantage of UDP is its ease of use and the ability to broadcast messages to multiple addresses at the same time. The UdpClient class provides a few simple ways to send and receive connectionless UDP datagrams in the blocking synchronization mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection before sending and receiving data. However, you can choose to establish a default remote host using one of the following two methods:

* Create an instance of the UdpClient class using the remote host name and port number as parameters.

* Create an instance of the UdpClient class, and then call the Connect method.

You can use any of the send methods provided in UdpClient to send data to a remote device. Use the Receive method to receive data from a remote host.

This article uses the Receive method to accept data from the client. It is then sent to the client via the Send method via the IP address stored in WCF.

Below I will implement this voice chat function on the basis of the previous article. First, add the class callmanager of sound management to the client, this class uses the open source Wave class library, the code is as follows:

public class CallManager
{
Private WaveIn _wavein;
Private Waveout _waveout;
Private IPEndPoint _serverendpoint;
Private Thread _playsound;
Private UdpClient _socket;
Public CallManager (IPEndPoint serverendpoint)
{
_serverendpoint = Serverendpoint;
}
public void Start ()
{
if (_wavein!= null | | | _waveout!= NULL)
{
throw new Exception ("Call is allready started");
}
int waveindevice = (Int32) Application.UserAppDataRegistry.GetValue ("WaveIn", 0);
int waveoutdevice = (Int32) Application.UserAppDataRegistry.GetValue ("Waveout", 0);
_socket = new UdpClient (0); Opens a random available port on all interfaces
_wavein = new WaveIn (Wavein.devices[waveindevice], 8000, 16, 1, 400);
_wavein.bufferfull + = new Bufferfullhandler (_wavein_bufferfull);
_wavein.start ();
_waveout = new Waveout (Waveout.devices[waveoutdevice], 8000, 16, 1);
_playsound = new Thread (new ThreadStart (PlaySound));
_playsound.isbackground = true;
_playsound.start ();
}
private void PlaySound ()
{
Try
{
while (true)
{
Lock (_socket)
{
if (_socket. Available!= 0)
{
IPEndPoint endpoint = new IPEndPoint (ipaddress.any, 0);
byte[] received = _socket. Receive (ref endpoint);
Todo:add codec
_waveout.play (Received, 0, received. Length);
}
}
Thread.Sleep (1);
}
}
catch (ThreadAbortException)
{
}
Catch
{
This. Stop ();
}
}
void _wavein_bufferfull (byte[] buffer)
{
Lock (_socket)
{
Todo:add codec
_socket. Send (buffer, buffer. Length, _serverendpoint);
}
}
public void Stop ()
{
if (_wavein!= null)
{
_wavein.dispose ();
}
if (_waveout!= null)
{
_waveout.dispose ();
}
if (_playsound.isalive)
{
_playsound.abort ();
}
if (_socket!= null)
{
_socket. Close ();
_socket = null;
}
}
}

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.