Search code examples
c#winformsrtsplibvlconvif

How to stream RTSP camera feed in Windows Forms c# using Vlc.DotNet


I have followed this link's instruction to add the Vlc.DotNet libraries (.Core, .Core.Interops, .Forms and .Wpf) to my project's solution.

I also added the 3.0.0 version of the VideoLAN.LibVLC.Windows library.

I added a vlcControl to my form and this is the resulting Designer.cs:

// 
// vlcControl1
// 
this.vlcControl1.BackColor = System.Drawing.Color.Black;
this.vlcControl1.Location = new System.Drawing.Point(384, 357);
this.vlcControl1.Name = "vlcControl1";
this.vlcControl1.Size = new System.Drawing.Size(75, 23);
this.vlcControl1.Spu = -1;
this.vlcControl1.TabIndex = 6;
this.vlcControl1.Text = "vlcControl1";
this.vlcControl1.VlcLibDirectory = ((System.IO.DirectoryInfo)(resources.GetObject("vlcControl1.VlcLibDirectory")));
this.vlcControl1.VlcMediaplayerOptions = null;

I've added a dummy VlcLibDirectory to the properties so I can change it later.

The path to the x86 version of my vlcLib is: E:\testLouka\dansMaCamera2.0\dansMaCamera2.0\libvlc\win-x86

I tried to use the following code to get a video feed from a RTSP stream url:

public partial class Form1 : Form
{
   public Form1()
   {
       InitializeComponent();

       this.vlcControl1 = new VlcControl()
       {
           Name = "vlc1",
           Location = new Point(0, 0),
           Dock = DockStyle.Fill,
           VlcLibDirectory = new DirectoryInfo(Path.Combine("E:\\testLouka\\dansMaCamera2.0\\dansMaCamera2.0", "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64")),
           Spu = -1,
           VlcMediaplayerOptions = null,
           Enabled = true
       };
       string[] options = { ":network-caching=500" };

       vlcControl1.Play(new Uri(m_stream.URL), options);
   }
}

the m_stream.URL variable is returning a RTSP link looking like "rtsp://admin:[email protected]/Stream0"

My form shows up, but my vlcController doesn't show anything...

I looked at https://github.com/ZeBobo5/Vlc.DotNet's wiki, but I'm stuck...

What am I doing wrong here?


Solution

  • All you have to do is add a vlcControl to your form and add some code to its VlcLibDirectoryNeeded event.

    /// <summary>
    /// Looks for the vlc directory on the opening of the app
    /// Opens a dialog if the libvlc folder is not found for the user to pick the good one
    /// Folder for 32bits should be "libvlc\win-x86\" and "libvlc\win-x64\" for 64 bits
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void myVlcControl_VlcLibDirectoryNeeded(object sender, VlcLibDirectoryNeededEventArgs e)
    {
        var currentAssembly = Assembly.GetEntryAssembly();
        var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
    
        if (currentDirectory == null)
            return;
        if (IntPtr.Size == 4)
            e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x86\"));
        else
            e.VlcLibDirectory = new DirectoryInfo(Path.GetFullPath(@".\libvlc\win-x64\"));
    
        if (!e.VlcLibDirectory.Exists)
        {
            var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            folderBrowserDialog.Description = "Select Vlc libraries folder.";
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog.ShowNewFolderButton = true;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                e.VlcLibDirectory = new DirectoryInfo(folderBrowserDialog.SelectedPath);
            }
        }
    }
    

    Then, you can add a play button of some sort to your form and play any wanted rtsp stream!

    private void btnPlay_Click(object sender, EventArgs e)
    {
        myVlcControl.Play(MyStream.URL);//can test with rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov
    }
    

    If the stream link is ok, you should be able to streaming it on the VLC app under Media->Open Network Stream...