在PI3上的UWP客户端中播放来自TCP / IP服务器的音频

问题描述 投票:0回答:1

我正在尝试制作一个uwp应用程序,它将成为客户端并将在PI3上运行。服务器是一个C#Winforms应用程序,它运行在我的Windows 10计算机上,我在这里找到:https://www.codeproject.com/Articles/482735/TCP-Audio-Streamer-and-Player-Voice-Chat-over-IP。服务器可以将麦克风设备的音频流传输到所有连接的客户端。虽然该项目有自己的客户端,但我可以在本地计算机上运行服务器和客户端。现在我想在UWP C#中构建一个类似的客户端应用程序。通过使用UWP StreamSocketActivity示例,我可以连接到服务器。但我不知道如何接收音频数据并在UWP客户端上播放。有人能帮我一把吗? Blow是运行服务器的屏幕截图,它有一个来自uwp客户端的连接:Client connects to the server

提前致谢!

c# uwp audio-streaming windows-iot-core-10 stream-socket-client
1个回答
0
投票

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

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

您可以在TcpProtocols.cs中找到代码。在UWP客户端应用程序中,您需要转换UWP的代码。这个document展示了如何在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包enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.