kakone / VLC.MediaElement

MediaElement clone powered by VLC
GNU Lesser General Public License v2.1
68 stars 33 forks source link

"Could not create swapChain" in Xamarin Forms UWP project #49

Closed lisardggY closed 6 years ago

lisardggY commented 6 years ago

I'm trying to initialize a MediaElement in a Xamarin Forms app, as a custom renderer for a custom control on UWP. I create the instance and set a source, but when it comes down to rendering, I get the following unhandled COMException:

The application made a call that is invalid. Either the parameters of the call or the state of some object was incorrect. Enable the D3D debug layer in order to see details via debug messages.

Could not create swapChain

at libVLCX.Instance..ctor(IList`1 argv, SwapChainPanel panel) at VLC.MediaElement.d178.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at VLC.MediaElement.<>cDisplayClass166_0.<b3>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.b6_0(Object state) at System.Threading.WinRTSynchronizationContext.Invoker.InvokeCore()

Has it been tested as a Xamarin Forms app?

lisardggY commented 6 years ago

Additional data:

kakone commented 6 years ago

I didn't test it in Xamarin Forms yet (and I don't know how Xamarin Forms rendering works). The VLC media element control uses a SwapChainPanel for D3D11 rendering. Perhaps it is not compatible with Xamarin Forms, I will try to look at it quickly.

lisardggY commented 6 years ago

The Xamarin.Forms controls are basically placeholders, and the CustomRenderers are per-platform code that is generated in the actual app - so regardless of Xamarin, this is still simply a UWP app, but perhaps structured a bit differently.

Additionally, I've tried compiling the control from sources and referencing it directly (rather than from NuGet) and I'm getting more specific errors now . Specifically, it seems there's a FileNotFoundException thrown here:

at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD) at libVLCX.Instance..ctor(IList`1 argv, SwapChainPanel panel) at VLC.MediaElement.d178.MoveNext() in c:\projects\VLC.MediaElement\VLCMediaElement\MediaElement.cs:line 465 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at VLC.MediaElement.<>cDisplayClass166_0.<b__3>d.MoveNext() in c:\projects\VLC.MediaElement\VLCMediaElement\MediaElement.cs:line 371

Since the only thing in the InitAsync that seems to care about files is the --keystore-file parameter, I'll try to see if the result of ApplicationData.Current.LocalFolder.Path and KeyStoreFilename are null for some reason.

lisardggY commented 6 years ago

And indeed, the file VLC_MediaElement_KeyStore doesn't exist under the LocalFolder.Path. Is it supposed to be generated by something?

lisardggY commented 6 years ago

Ok, that appears to have been a false lead. I had a broken reference to the C++ Runtime components. Now that I fixed it, I get the same "could not created swapChain" error again.

kakone commented 6 years ago

keystore is the a path where keys (like username, password, ...) will be stored if needed. It will not be used in your case. As you guessed, you need the the libVLCX NuGet package and the Microsoft Visual C++ 2013 Runtime Package for Windows Universal extension. It is automatically added by the NuGet package.

kakone commented 6 years ago

OK, I understood the problem. In Xamarin Forms, the ViewRenderer has no size when is added to the visual tree. But the SwapChainPanel must have a width and height when InitAsync is called. If I deffer the call of InitAsync method in UpdateSizeAsync, it works great.

lisardggY commented 6 years ago

Ah, wonderful. Once you push the changes, I'll pull and see if it fixes it on my end as well.

kakone commented 6 years ago

The version 0.9.21 should fix the problem.

lisardggY commented 6 years ago

And we have video! Fantastic. Thanks for the quick fix!

JunielKatarn commented 6 years ago

Hi, @lisardggY. I'm also trying to use VLC.MediaElement in a Xamarin.Forms UWP app, but the mediaElement.MediaPlayer property stays null (likely, it never gets initialized).

Do you have any pointers on how to set up the MediaElement correctly?

[Update] Found a solution. Deferred calling the Xamarin LoadApplication method as an event handler after Load finishes.