【问题标题】:WebRTC for UWP, new RTCPeerConnection() doesn't complete execution用于 UWP 的 WebRTC,新的 RTCPeerConnection() 未完成执行
【发布时间】:2017-04-10 19:40:11
【问题描述】:

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

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

从更简单的开始,我想将 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 中的异步/线程编程缺乏经验,这让我很难确定错误在哪里。任何帮助将不胜感激。

【问题讨论】:

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


【解决方案1】:

我终于找到了问题,解决方法是半尴尬的,之前没有找到:)

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

 WebRTC.Initialize(this.Dispatcher);

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

【讨论】:

  • 您是否有机会将您的完整示例上传到某个地方?我不得不同意。 Chatterbox 对于初学者来说是一种复杂的方式。
  • 抱歉耽搁了,我没有看到你的评论。我将在今晚或明天将我的示例上传到我的 GitHub 或类似网站。
  • 好的,我的粗略示例现在可以在gitlab.ida.liu.se/lilbl552/UWP_WebRTC_NoSignalling 获得。如果您有任何问题,可以通过问题页面/邮件与我联系。
猜你喜欢
  • 2016-08-13
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
相关资源
最近更新 更多