0

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.

Ahacz
  • 33
  • 1
  • 4
  • I didn't understand your question. What are you trying to achieve and what doesn't work? What question can a LibVLCSharp developer answer? – cube45 Jan 17 '20 at 22:29
  • I m trying to change stream sources on client request. I don't know how to make a mediaplayer object persist in signalr hub or how to get information outside of the hub (trigger an event that would be executed on an object in mainwindow class) – Ahacz Jan 18 '20 at 13:01
  • OK so you have an architectural issue that goes beyond the use of LibVLCSharp. To simplify your question, you could have asked how to call something on my main window from the Hub. – cube45 Jan 19 '20 at 08:57

1 Answers1

0

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(...);
cube45
  • 3,429
  • 2
  • 24
  • 35
  • Thank you so much for your patience and helping me out even though I didn't specify my problem right. I got my Main Window through Application.Current, then used a dispatcher to solve threading issue. – Ahacz Jan 20 '20 at 00:59