WebRTC for UWP, new RTCPeerConnection () does not complete execution

I'm trying to build a Universal Windows Platform app that uses WebRTC, but my code never runs after the first new RTCPeerConnection.

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

To start with something simpler, I want to recreate the minimalist codelab exercise WebRTC.org as a UWP app written in C #. The original HTML / javascript creates a web page with two video streams, one of which is a local video stream and one that is sent via WebRTC. But my UWP code doesn't even have time to create the first RTCPeerConnection.

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

My code, first version

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");
    }
}

      

Debugging and printouts show that the assertion is never reached after creating a new RTCPeerConnection. I thought that maybe creating a new RTCPeerConnection could not be done on the main thread, so I updated the code to run this code on a different thread.

My code, second version

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;
     }
}

      

Debug printouts show that the code does not reach the line after creating a new RTCPeerConnection. Debugging shows that a thread created for an anonymous inner function is never destroyed. I've tried using an empty RTCConfiguration as in the codelab exercise, but it doesn't make any difference.

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

+3


source to share


1 answer


I finally found the problem and the solution is half-mixed not to find it sooner :)

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



 WebRTC.Initialize(this.Dispatcher);

      

As per the ChatterBox example, it can be null instead of the dispatcher as a parameter ( sample code ) on Windows 10.

+3


source







All Articles