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!

4

1 回答 1

0

如文章所述,用于传输音频数据的协议是自定义的。

注意!!!这是一个专有项目。您不能将我的服务器或客户端与任何其他标准化服务器或客户端一起使用。我不使用 RTCP 或 SDP 等标准。

您可以在TcpProtocols.cs中找到代码。在 UWP 客户端应用程序中,您需要为 UWP 转换代码。本文档展示了如何在 UWP 中构建基本的 TCP 套接字客户端。但是您还需要修改代码以不断地从服务器接收数据。以下代码可能对您有所帮助。

    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);
        }
    }

更新:

RTP 数据包 在此处输入图像描述

推荐并广泛使用实时音频 RTSP。实时流协议 (RTSP) 是一种网络控制协议,设计用于娱乐和通信系统,以控制流媒体服务器。该协议用于建立和控制端点之间的媒体会话。RTSP 有一些优点。在此解决方案中,您需要构建一个 RTSP 服务器,然后在您的 UWP 应用程序中使用VLC.MediaElement库或其他支持 Windows IoT Core 的库。但我不确定这个库是否支持 RTP。此外,本文档还显示了 Windows IoT Core 上支持的编解码器。

于 2019-02-20T05:34:47.390 回答