0

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!

Community
  • 1
  • 1

1 Answers1

0

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.

Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Thank you for reply. Using your server, I can connect to server instance from uwp app. When connected the server sends config data to the client, then the data is sent. What I'm trying is to discover the rpt packet as you recommend but still no luck because in uwp app I don't know when the data is completed. I can see in your receive_LH there is a delegate to DataComplete. – user3678813 Feb 22 '19 at 02:54
  • @user3678813, the max length of data buffer in client is 10000(bytes), when the data is received , the client need to parse the data to RTP packet. In the packet, the header length is 12, the audio data(PCM) is after the header. So when the audio data received, the client will call DataComplete to play audio. – Michael Xu Feb 22 '19 at 05:54
  • So let say on uwp app, when the audio data received in bytes, I need to call WinSound.RTPPacket rtp = new WinSound.RTPPacket(bytes). Then the audio data will be store in rtp.data (no need to call the Receive_LH method). After that I need to convert rtp.data to linearbytes and the add it to the mediaPlayerElement source. – user3678813 Feb 22 '19 at 07:35
  • Also I found an other project from you: https://www.codeproject.com/Articles/394890/Play-or-Capture-Audio-Sound-Send-and-Receive-as-Mu. I'm interested in using the multicaststreamer as the server and uwp app as clients. Just trying to understand the multicastreceiver and player. Sorry if I narrow you cause I'm quite new to streaming data over network. – user3678813 Feb 22 '19 at 07:47
  • @user3678813, i have updated my response.Hope that is helpful for you. – Michael Xu Feb 22 '19 at 08:56