How to start WCF voice chat application?

I want to develop a WCF voice chat application.

I would like to use the WCF service to create an application

| Invite | | --------------------------------> | | OK | | <-------------------------------- | | | | --------------------------------> | | Audio flow | | <-------------------------------- | | Bye | | --------------------------------> | AB 

in another case, the same application must have a listening service, so it receives calls and sends calls to another application on another computer. (No need to use the additional service recipient application to receive calls).

I know, maybe I should use certain protocols, such as sockets or others for voice, but later I will take care of that.

So which WCF service should I use, and what type of binding would be best for this purpose.

+3
source share
1 answer

As stated in the commentary on your question, UDP or other streaming protocols are usually preferred. But with WCF providing a lot of abstraction, it would be easier to do something like this. Having said that I implemented the voice chat feature on one of our previous programs (the software was used by several clients on the local network) with WCF and netTcpBinding , and it worked well (I tested it with 5 clients on the local network), you couldn’t feel that WCF is an obstacle to architecture. This will be interesting with the release of WCF 4.5, which will support UDP transport.

For something like this, you really should get the duplex entity in WCF. I continued to make it so that I had a contract for SendVoice() on my service, every time a client sent their asudio stream to a service, the SendVoice() method SendVoice() over the list of subscribers (connected clients) and call SendVoiceCallback() to every customer. You can start with something like this:

 public void SendVoice(byte[] audio) { //Keep a list of connected clients in a dictionary called subscribers //lock your subscribers list so that it not modified when you're in the middle of sending the stream lock (subscribers) { //send the received voice stream to each client foreach (var _subscriber in subscribers) { if (OperationContext.Current.GetCallbackChannel<IVoiceChatCallback>() == subscriber.Key) { //if the person who sent the video is the current subscriber then don't send the video to THAT subscriber continue; } try { //Send the received stream to the client asynchronously _subscriber.Key.BeginOnVoiceSendCallback(audio, onNotifyCompletedVoiceSend, _subscriber.Key); } catch (Exception) { //fault handling } } } } 

Then your clients periodically called the method above, in my implementation I installed this every 250 milliseconds, and it worked fine (obviously, there was a slight lag).

The IVoiceChatCallback code IVoiceChatCallback has a callback contract. Callbacks allow the service to invoke some operation on clients, so for ISntance, the client will behave like a server. Asynchronous service calls mean that your SendVoice() will publish audio for each client asynchronously, rather than waiting for the previous thread to reach the client before sending a new thread.

The above code is just an idea for you to get started. You must add an error handling code to the service to check if clients are disconnected and remove them from your subscribers dictionary. Again, you should get comfortable with asynchronous calls in WCF, as well as with callbacks. Also, if you want to use this on a local network, then netTcpBinding is the way to go, and you must configure your service to optimize its bandwidth and concurrency. Over the Internet, you want to use Http bindings that support duplex.

+2
source

All Articles