本文介绍了UWP 的 WebRTC,新的 RTCPeerConnection() 没有完成执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m trying to create a Universal Windows Platform application that uses WebRTC, but my code never executes past the first new RTCPeerConnection.

我一直在看 UWP 的开源项目 WebRTC (博客文章 与 git repos 的链接)并设法构建和运行 ChatterBox VoIP 客户端示例.由于我对 UWP 编程和 WebRTC(以及一般的 .NET、C# 和 Windows 编程)都不熟悉,因此我在上面提到的存储库中查看的示例太复杂了,我无法遵循.

I have been looking at the open source project WebRTC for UWP (blog post with links to git repos) and managed to build and run the ChatterBox VoIP client example. Since I am new to both UWP programming and WebRTC (and .NET, C# and Windows programming in general) the examples I have been looking at in the repos mentioned above have been much too complex for me to follow.

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

To start with something simpler, I want to recreate the WebRTC.org minimalistic codelab exercise as a UWP application written in C#. The original HTML/javascript creates a webpage with two video streams, one that is the local videostream and one that is sent over WebRTC. But, my UWP code doesn’t even get past creating the first RTCPeerConnection.

我使用的是 Visual Studio 2015 并安装了适用于 UWP 的 Nuget WebRTC 包.

I am using Visual Studio 2015 and have installed the Nuget WebRTC package for UWP.

我的代码,第一个版本

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,所以我更新了代码以在另一个线程上运行该代码.

Debugging and printouts shows that the statement after the creation of the new RTCPeerConnection is never reached. I thought that maybe creating a new RTCPeerConnection couldn’t be done on the main thread, so I updated the code to run that code on another thread.

我的代码,第二个版本

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,但没有任何区别.

Debug printouts show that the code doesn’t reach the line after the creation of the new RTCPeerConnection. Debugging shows that the thread created for the anonymous inner function is never destroyed. I have tried to use an empty RTCConfiguration like in the codelab exercise, but it makes no difference.

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

My inexperience with WebRTC, UWP and asynchronous/thread programming in UWP makes is difficult for me to determine where the error is. Any help would be greatly appreciated.

推荐答案

我终于找到问题了,解决方案是半尴尬没有早点找到:)

I found the problem at last, and the solution is semi-embarassing not to have found earlier :)

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

There is a static method Initialize(CoreDispatcher dispatcher) that initializes WebRTC with dispatcher and worker thread (link to definition in the UWP WebRTC wrapper). The following statement before creating a new RTCPeerConnection solved the problem.

 WebRTC.Initialize(this.Dispatcher);

根据 ChatterBox 示例,它可以采用 null 而不是调度程序作为参数(代码示例)在 Windows 10 中.

According to the ChatterBox example it can take null instead of a dispatcher as a parameter (code example) in Windows 10.

这篇关于UWP 的 WebRTC,新的 RTCPeerConnection() 没有完成执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 17:35