C# 用于UWP的WebRTC,新的RTPeerConnection()不';不能完成执行

C# 用于UWP的WebRTC,新的RTPeerConnection()不';不能完成执行,c#,uwp,windows-10,webrtc,windows-10-universal,C#,Uwp,Windows 10,Webrtc,Windows 10 Universal,我正在尝试创建一个使用WebRTC的通用Windows平台应用程序,但我的代码在第一个新的RTPeerConnection之后从未执行过 我一直在研究开放源码项目WebRTC for UWP(带有指向git repos的链接),并设法构建和运行ChatterBox VoIP客户端示例。由于我对UWP编程和WebRTC(以及.NET、C#和Windows编程)都是新手,所以我在上面提到的REPO中看到的示例对我来说太复杂了 首先,我想将WebRTC.org简化为一个用C#编写的UWP应用程序。原始

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

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

首先,我想将WebRTC.org简化为一个用C#编写的UWP应用程序。原始HTML/javascript创建了一个包含两个视频流的网页,一个是本地视频流,另一个是通过WebRTC发送的。但是,我的UWP代码甚至没有通过创建第一个RTPeerConnection

我正在使用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”);
    }
}
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;
     }
}
公共密封部分类主页面:第页
{
RTPEERCONNECTION_pc1;
公共主页()
{
this.InitializeComponent();
}
//单击“调用”按钮时执行的代码
私有异步void uxCall\u单击(对象发送方,路由目标)
{
/*GetDefaultList()返回列表,其中Stun/Turn服务器借用了ChatterBox示例*/
var config=new RTCConfiguration(){IceServers=GetDefaultList()};
pc1=新的RTPeerConnection(配置);
WriteLine(“从未达到此点”);
}
}
调试和打印结果表明,在创建新RTPeerConnection后,从未到达语句。我认为可能无法在主线程上创建新的RTPeerConnection,因此我更新了代码以在另一个线程上运行该代码

我的代码,第二版

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”);
    }
}
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;
     }
}
公共密封部分类主页面:第页
{
RTPEERCONNECTION_pc1;
公共主页()
{
this.InitializeComponent();
}
//单击“调用”按钮时执行的代码
私有异步void uxCall\u单击(对象发送方,路由目标)
{
var config=new RTCConfiguration(){IceServers=GetDefaultList()};
_pc1=等待CreatePeerConnection(配置);
WriteLine(“从未达到此点”);
}
专用异步任务CreatePeerConnection(RTCConfiguration配置)
{
RTPEERCONNECTION pc;
WriteLine(“创建对等连接”);
pc=等待任务。运行(()=>{
//此处已创建匿名内部函数的线程
var newpc=新的RTPeerConnection(配置);
WriteLine(“从未达到此点”);
返回newpc;
});
返回pc;
}
}
调试打印输出显示,在创建新RTPeerConnection后,代码未到达该行。调试表明,为匿名内部函数创建的线程从未被销毁。我曾尝试使用codelab练习中的空RTC配置,但没有任何区别


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

我终于找到了问题所在,而且解决方案是之前没有找到的,这让我有些尴尬:)

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

 WebRTC.Initialize(this.Dispatcher);

根据ChatterBox示例,它可以在Windows 10中使用null而不是dispatcher作为参数()。

我不允许添加到post:codelab练习的完整代码中的其他链接,我尝试过的ChatterBox示例您是否有机会将完整示例上传到某个地方?我不得不同意。Chatterbox对于初学者来说太复杂了。很抱歉耽搁了,我没有看到你的评论。我将在今晚或明天将我的示例上传到我的GitHub或类似网站。好的,我的粗略示例现在可以在。如果您有问题,可以通过问题页面/邮件与我联系。