c# - UWP 的 WebRTC,新的 RTCPeerConnection() 没有完成执行

标签 c# uwp windows-10 webrtc windows-10-universal

我正在尝试创建一个使用 WebRTC 的通用 Windows 平台应用程序,但我的代码从未执行过第一个新的 RTCPeerConnection。

我一直在关注 UWP 的开源项目 WebRTC(blog post 以及指向 git 存储库的链接)并设法构建和运行 ChatterBox VoIP 客户端示例。由于我对 UWP 编程和 WebRTC(以及一般的 .NET、C# 和 Windows 编程)都是新手,所以我在上面提到的存储库中查看的示例太复杂了,我无法理解。

从更简单的事情开始,我想重新创建 WebRTC.org 简约 codelab exercise作为用 C# 编写的 UWP 应用程序。原始 HTML/javascript 创建一个包含两个视频流的网页,一个是本地视频流,一个是通过 WebRTC 发送的。但是,我的 UWP 代码甚至没有通过创建第一个 RTCPeerConnection。

我正在使用 Visual Studio 2015 并安装了适用于 UWP 的 Nuget WebRTC 包。

我的代码,第一个版本

public sealed partial class MainPage : Page
{
    RTCPeerConnection _pc1;
    public MainPage()
    {
        this.InitializeComponent();
    }
    // Code that is executed when ‘Call’ button is clicked
    private async void uxCall_Click(object sender, RoutedEventArgs e)
    {
        /* GetDefaultList() returns List<RTCIceServer>, with Stun/Turn-servers borrowed from the ChatterBox-example */
        var config = new RTCConfiguration() { IceServers = GetDefaultList() };
        pc1 = new RTCPeerConnection(config);
        Debug.WriteLine(“Never reaches this point”);
    }
}

调试和打印输出显示创建新 RTCPeerConnection 后的语句永远不会到达。我认为可能无法在主线程上创建新的 RTCPeerConnection,因此我更新了代码以在另一个线程上运行该代码。

我的代码,第二个版本

public sealed partial class MainPage : Page
{
    RTCPeerConnection _pc1;
    public MainPage()
    {
         this.InitializeComponent();
    }
    // Code that is executed when ‘Call’ button is clicked
    private async void uxCall_Click(object sender, RoutedEventArgs e)
    {
         var config = new RTCConfiguration() { IceServers = GetDefaultList() };
         _pc1 = await CreatePeerConnection(config);
         Debug.WriteLine(“Never reaches this point”);
    }

    private async Task<RTCPeerConnection> CreatePeerConnection(RTCConfiguration config)
    {
         RTCPeerConnection pc;
         Debug.WriteLine("Creating peer connection.");
         pc = await Task.Run(() => {
               // A thread for the anonymous inner function has been created here
               var newpc = new RTCPeerConnection(config);
               Debug.WriteLine("Never reaches this point");
               return newpc;
         });
         return pc;
     }
}

调试打印输出显示代码在创建新的 RTCPeerConnection 后没有到达该行。调试显示为匿名内部函数创建的线程永远不会被销毁。我试过像在 Codelab 练习中那样使用空的 RTCConfiguration,但这没有任何区别。

我对 WebRTC、UWP 和 UWP 中的异步/线程编程经验不足,这让我很难确定错误出在哪里。任何帮助将不胜感激。

最佳答案

我终于找到了问题所在,之前没有找到解决方案有点尴尬:)

有一个静态方法 Initialize(CoreDispatcher dispatcher) 使用调度程序和工作线程初始化 WebRTC(链接到 UWP WebRTC 包装器中的 definition)。在创建新的 RTCPeerConnection 之前的以下语句解决了这个问题。

 WebRTC.Initialize(this.Dispatcher);

根据 ChatterBox 示例,它可以在 Windows 10 中使用 null 而不是调度程序作为参数 ( code example )。

关于c# - UWP 的 WebRTC,新的 RTCPeerConnection() 没有完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43331677/

相关文章:

c# - 使用 Windows 10 的 crystalreportviewer

c# - 如何修复发现的不兼容的 SQL Server 版本?

c# - 单击标题时在 DataGridView 中获取 "Index was out of range"异常

android - Xamarin 表单 : How to open an app from another app?

c# - 使用 Azure 移动服务进行身份验证

c# - 如何通过 NFC 标签启动我的应用程序?

c# - LINQ 的嵌套组

c# - 如何在.NET 中读取包含 Unicode 的 xml 文本文件,然后将其保存到数据库中?

javascript - UWP JS : How to get Culture info

c# - 如何删除通用 Windows 平台 (Windows 10) 应用程序中的 InkCanvas 笔画?