Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a wpf MediaElement play when its Source is a https uri

In a wpf standalone application (.exe) I have included a MediaElement in the MainWindow

<Window x:Class="Media.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main Window" Height="350" Width="525">
    <Grid>
        <MediaElement x:Name="Player" Stretch="Uniform" LoadedBehavior="Manual" UnloadedBehavior="Stop"/>
    </Grid>
</Window>

and from the code behind I set its Source to any https Uri:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var source = new Uri("https://stream_which_can_be_opened_with_windows_media_player.com", UriKind.Absolute);
        Player.Source = source;
        Player.Play();
    }
}

When the Play() method is called a NullReferenceException is thrown instead of playing the media content. MediaElement is initialized, the NullReferenceException is thrown from the Play() method, see below.

The same Uri for the video can be opened in Windows Media Player (File->Open Url).

The issue seems to be in MediaPlayerState.OpenMedia method (an object which the MediaElement uses internally) which tries to check if appDeploymentUri retrieved from SecurityHelper.ExtractUriForClickOnceDeployedApp has the scheme HTTPS. The application is not deployed with ClickOnce (it has a standalone installer) and the appDeploymentUri is null, hence the NullReferenceException.

This is from PresentationFramework.dll, System.Windows.Media.MediaPlayerState.OpenMedia

    if (SecurityHelper.AreStringTypesEqual(uriToOpen.Scheme, Uri.UriSchemeHttps))
    {
        // target is HTTPS. Then, elevate ONLY if we are NOT coming from HTTPS (=XDomain HTTPS app to HTTPS media disallowed)

        //source of the issue
        Uri appDeploymentUri = SecurityHelper.ExtractUriForClickOnceDeployedApp();
        //appDeploymentUri is null
        if (!SecurityHelper.AreStringTypesEqual(appDeploymentUri.Scheme, Uri.UriSchemeHttps))

Does anyone have any about a workaround/solution to make it work?

like image 398
Cristi Moldovan Avatar asked Aug 01 '14 13:08

Cristi Moldovan


1 Answers

I have been working with MediaElement quite few times, and I can honestly say it's a piece of shit and has more bugs than any other WPF component I've encountered. Not only has it bugs, but it's lacking a lot of features that Silverlight has. HTTPS works with Silverlight.

I went through code and I did not see the way to change it. Perhaps there is some MAD reflection hack which would allow you to do it, but that's hacking and I don't recommend that. Ps, it seems like a genuine bug, perhaps let the Microsoft guys know about it.

The easiest solution would be to make a "memory webserver" using OWIN. You can then stream through http://localhost:1337 and wrap the underlying https:// content. The https content would still be safe though, since you are streaming it from "memory webserver" and no "real" webrequests are ever made. It should still be efficient & secure.

like image 131
Erti-Chris Eelmaa Avatar answered Oct 31 '22 15:10

Erti-Chris Eelmaa