【发布时间】: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 中的异步/线程编程缺乏经验,这让我很难确定错误在哪里。任何帮助将不胜感激。
【问题讨论】:
-
我不允许添加到帖子中的其他链接:codelab 练习的完整代码codelabs.developers.google.com/codelabs/webrtc-web/…,我尝试过的 Chatterbox 示例github.com/Microsoft/WebRTC-universal-samples/tree/master/…
标签: c# uwp windows-10 webrtc windows-10-universal