用于UWP的WebRTC,新的RTCPeerConnection()无法完成执行

时间:2017-04-10 19:40:12

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

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

我一直在寻找UWP的开源项目WebRTC(blog post,其中包含指向git repos的链接)并设法构建并运行ChatterBox VoIP客户端示例。由于我是UWP编程和WebRTC(以及.NET,C#和Windows编程)的新手,我在上面提到的repos中看到的例子对我来说太复杂了。

从简单的事情开始,我想重新创建WebRTC.org minimalistic 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练习中使用一个空的RTCC配置,但没有区别。

我对UWP中的WebRTC,UWP和异步/线程编程缺乏经验,我很难确定错误的位置。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

我终于找到了这个问题,解决方案是半尴尬的,以前没有发现:)

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

 WebRTC.Initialize(this.Dispatcher);

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