Search code examples
wpfsignalrsignalr-hublibvlcsharp

SignalR WPF server - How to trigger a player?


I am currently working on a standalone app that can handle communication from a mobile and also utilizes VLC player to output a stream. The way I do it is that output stream object is in the main window, but I receive requests via SignalR. The request is fairly simple - it's just a string. The problem is I have no clue on how to pass the string from a SignalR hub back to the MediaPlayer object. How do I either get a string passed to an object outside or make a mediaplayer "persist" inside the hub? For now my code concerning it looks like this:

The hub:


    [HubName("CommHub")]
    public class CommHub:Hub
    {
        VLCControl outputplayer = new VLCControl();
        public void RequestConnectionsList()            
        {
            var databasePath = "--dbpath here--";
            var db = new SQLiteConnection(databasePath);
            List<string> output = db.Table<VideoSources>().Select(p => p.Name).ToList();    //Gets names from the db
            Clients.Client(Context.ConnectionId).CamsInfo(output); //send available connection names to client
        }
        public void RequestOutStream(string requestedName) //this function is called but i have no idea how to make it work
        {
            outputplayer.playSelected(requestedName);
        }
    }

VLCControl:

class VLCControl
    {
        public Media rtsp;
        private const string VIDEO_URL = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov";
        private MediaPlayer player;
        public static string GetConfigurationString()       //using this method in mainwindow player as well
        {
            string address = Properties.Settings.Default.LocalAddress;
            string port = Properties.Settings.Default.LocalPort;
            string result=
                    ":sout=#duplicate" +
                    "{dst=display{noaudio}," +
                    "dst=rtp{mux=ts,dst=" + address +
                    ",port=" + port + ",sdp=rtsp://" + address + ":" + port + "/go.sdp}";
             return result;
        }
        public void playSelected(string inputAddress)
        {
            var databasePath = "D:\\Projects\\Sowa\\Sowa\\Serwer\\VideoSources.db";
            var db = new SQLiteConnection(databasePath);
            string input = db.Table<VideoSources>().FirstOrDefault(p => p.Name == "test").Address;
            db.Close();
            var rtsp = new Media(MainWindow._libvlc, input, FromType.FromLocation);
            rtsp.AddOption(VLCControl.GetConfigurationString());
            player.Stop();
            player.Play(new Media(MainWindow._libvlc, VIDEO_URL, FromType.FromLocation));
        }
    }

The players are definitely working - when i create a mediaplayer in mainwindow it does indeed output as expected.


Solution

  • I think your question can be rephrased as "How can I call a method on my UI from a SignalR Hub"

    For that, you have several options :

    • If you are using ASP.net core's SignalR, you can use dependency injection to inject either your window or an accessor to your window
    • You can get your main window with (MyWindow)Application.Current.MainWindow. What you do with it is up to you then
    • You can create a static class that will hold a reference to your component directly. This example assumes you have only one view at a time in your application.
    public static class ViewAccessor {
      public static MyView View { get; set; }
    }
    

    In your view constructor, after InitializeComponents:

    ViewAccessor.View = this;
    

    In your hub :

    ViewAccessor.View.Play(...);