Search code examples
c#wcf

how to start a WCF voice chat application?


I'm looking to develop a WCF voice chat application.

I would like to use WCF service to build the application

|             Invite                | 

| --------------------------------> |

|               OK                  |

| <-------------------------------- |

|                                   |

| --------------------------------> |

|            Audio flow             |

| <-------------------------------- |

|               Bye                 | 

| --------------------------------> |

A                                   B

in other way the same application should have the listening service, so it receives calls and sends calls to the other same application on the other machine. (Without the need for an extra service host application to receives calls).

I know maybe i should use a specific protocols like sockets or other for transferring voice, but i will check about that later.

So what kind of WCF service should i use, and what type of binding would be the best for this purpose.


Solution

  • As the comment on your question says, UDP or other streaming protocols are usually preferred. But with WCF providing more abstraction, would be easier to do something like this. Having said that I have implemented a voice chatting feature on one of our previous software (the software was used by multiple clients in a LAN) with WCF and netTcpBinding, and it worked well (I tested it with 5 clients in a LAN), you couldn't really feel that WCF hindered the architecture. It will get interesting with the release of WCF 4.5 which will be supporting UDP transport.

    For something like this, you should really get the gist of duplex communication in WCF. I went about with doing in such a way that i had a SendVoice() operation contract on my service, everytime a client sent their asudio stream to the service, the SendVoice() method would iterate through the list of subscribers (connected clients) and invoke the SendVoiceCallback() on each client. You could 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's 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
                     }
                }
            }
    }
    

    Your clients would then call the method above periodically, in my implementation I set this to every 250 milliseconds and it worked perfectly well (obviously there was a small lag).

    In the code above IVoiceChatCallback is the callback contract. Callbacks enable the service to invoke some operation on the clients, so for an isntance the client will behave like a server. Asynchronous service calls will mean that your SendVoice() will publish the audio to every client asynchronously, rather than waiting for the previous stream to reach the client before sending the new stream.

    The code above is just an idea for you to get started. You should add some fault handling code to the service to check when clients are disconnected and remove them from your subscribers dictionary. Again You should become comfortable with asynchronous operation calls in WCF as well as callbacks. Also if you're looking to use this in LAN, then netTcpBinding is the way to go, and you should configure your service as to optimise its throughput and concurrency. Over the internet, you'd want to use a Http Binding that supports duplex.