play audio from TCP/IP server in UWP client on PI3

632 Views Asked by At

I'm trying to make an uwp app which will be a client and will run on PI3. The server is a C# Winforms app, that runs on my Windows 10 computer, which I've found here: https://www.codeproject.com/Articles/482735/TCP-Audio-Streamer-and-Player-Voice-Chat-over-IP. The server can stream audio from microphone device to all the connected clients. Although the project has its own client and I can run both server and client on my local machine. Now I want to build a similar client app in UWP C#. By using the UWP StreamSocketActivity sample, I can connect to the server. But I don't know how to receive the audio data and play it on UWP client. Could anyone give me a hand? Blow is the screenshot of running server which has one connection from uwp client: Client connects to the server

Thanks in advance!

1

There are 1 best solutions below

5
Michael Xu On

As mentioned in the article, the protocol used to transfer the audio data is customized.

Note !!! This is a proprietary project. You can't use my servers or clients with any other standardized servers or clients. I don't use standards like RTCP or SDP.

You can find the code in TcpProtocols.cs. In UWP client app, you need to convert the code for UWP. This document shows how to build a basic TCP socket client in UWP. But you also need to modify the code for receive data from server continuously. Following code may be helpful for you.

    private async void StartClient()
    {
        try
        {
            // Create the StreamSocket and establish a connection to the echo server.
            using (var streamSocket = new Windows.Networking.Sockets.StreamSocket())
            {
                // The server hostname that we will be establishing a connection to. In this example, the server and client are in the same process.
                var hostName = new Windows.Networking.HostName(TxtHostName.Text);

                await streamSocket.ConnectAsync(hostName, TxtPortNumber.Text);

                while(true)
                {
                    using (var reader = new DataReader(streamSocket.InputStream))
                    {
                        reader.InputStreamOptions = InputStreamOptions.Partial;

                        uint numAudioBytes = await reader.LoadAsync(reader.UnconsumedBufferLength);
                        byte[] audioBytes = new byte[numAudioBytes];
                        reader.ReadBytes(audioBytes);

                        //Parse data to RTP packet
                        audioBytes = Convert_Protocol_LH(audioBytes);

                        var pcmStream = audioBytes.AsBuffer().AsStream().AsRandomAccessStream();
                        MediaElementForAudio.SetSource(pcmStream, "audio/x-wav");
                        MediaElementForAudio.Play();
                    }
                }                    
            }
        }
        catch (Exception ex)
        {
            Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
        }
    }

UPDATE:

RTP Packet enter image description here

RTSP for living audio is recommended and wide used.The Real Time Streaming Protocol (RTSP) is a network control protocol designed for use in entertainment and communications systems to control streaming media servers. The protocol is used for establishing and controlling media sessions between end points. There are some advantages of RTSP. In this solution, you need to build a RTSP server and then use VLC.MediaElement library or other library which support on Windows IoT Core in your UWP app. But i am not sure this library supports RTP. In addition, this document shows supported codecs on Windows IoT Core.