Using LibVLC in Visual Studio (C++) 2019

This forum is about all development around libVLC.
matthewdumas
New Cone
New Cone
Posts: 4
Joined: 19 May 2020 22:08

Using LibVLC in Visual Studio (C++) 2019

Postby matthewdumas » 19 May 2020 22:36

I recently decided to refresh my C++ skills a bit after not having needed to use them for a while with a project what will utilize LibVLC (something I wanted to learn more about), but I've run into some serious issues just getting a demo working.

I'm using Visual Studio/C++. I've downloaded the LibVLC tarball, and the VLC program. I copied the DLLs (libvlc, libvlccore) into libvlc_bin. I copied libvlc.lib, and libvlccore.lib into libvlc_lib. Finally, I copied the contents of vlc-3.0.10.tar.xz/vlc-3.0.10/include to libvlc_include.

My project directory looks like:
  • ConsoleApplication1
    • ConsoleApplication1
      • Debug
        libvlc_bin
        libvlc_include
        libvlc_lib
        plugins
        ...
      Debug
      ConsoleApplication1.sln
After setting it up like that, I added the libvlc_lib directory to the project properties in "Linker->Additional Library Directories", and libvlc.lib;libvlccore.lib to "Linker->Input->Additional Dependencies." I added libvlc_include to the Property "C/C++->Additional Include Directories".

I haven't figured out what to do with the bin directory yet. Things have moved around a bit, but I know it needs to be copied into the debug directory where the debug exe appears, so for the moment I've manually copied the DLLs to that directory.

With setup complete, and dlls copied to the debug directory, I followed the directions here:
https://wiki.videolan.org/LibVLC_Tutorial/#Windows

When I look at the code, the first issue is an error -- sleep() is undefined. I include Windows.h, and made a macro for it at line 6 (just in case it's used somewhere i can't see):

Code: Select all

#define sleep(x) Sleep(1000 * (x))
All looks good, so I hit the debug button, and everything compiles fine, and runs all the way to line 23 (added a comment below), where it does the following:
Exception thrown at 0x7AA6A6A9 (libvlc.dll) in ConsoleApplication1.exe: 0xC00000005: Access violation reading location 0x0000000C
The full code is below:

Code: Select all

#include <stdio.h> #include <stdlib.h> #include <vlc/vlc.h> #include <Windows.h> #define sleep(x) Sleep(1000 * (x)) int main(int argc, char* argv[]) { libvlc_instance_t* inst; libvlc_media_player_t* mp; libvlc_media_t* m; /* Load the VLC engine */ inst = libvlc_new(0, NULL); /* Create a new item */ //m = libvlc_media_new_location(inst, "https://file-examples.com/wp-content/uploads/2018/04/file_example_MOV_480_700kB.mov"); m = libvlc_media_new_path (inst, "C:/Repos/test.mpg"); /* Create a media player playing environement */ mp = libvlc_media_player_new_from_media(m); // This is line 23 /* No need to keep the media now */ libvlc_media_release(m); #if 0 /* This is a non working code that show how to hooks into a window, * if we have a window around */ libvlc_media_player_set_xwindow(mp, xid); /* or on windows */ libvlc_media_player_set_hwnd(mp, hwnd); /* or on mac os */ libvlc_media_player_set_nsobject(mp, view); #endif /* play the media_player */ libvlc_media_player_play(mp); sleep(10); /* Let it play a bit */ /* Stop playing */ libvlc_media_player_stop(mp); /* Free the media_player */ libvlc_media_player_release(mp); libvlc_release(inst); return 0; }
Both the link I tried, replacing the original, and the file path for the one on my PC are valid.

Any ideas? I feel like there's something simple I'm missing here.

Update
While playing around a bit more, I've come to realize that something is wrong with the files I'm using. I took the DLL and includes out of the official downloads, but I always get access violations. I found an older project online where someone gave simple examples using libVLC 2.x.x.x. I copied their lib, include, and dll files into the appropriate places and the example code I gave above now works.

matthewdumas
New Cone
New Cone
Posts: 4
Joined: 19 May 2020 22:08

Re: Using LibVLC in Visual Studio (C++) 2019

Postby matthewdumas » 28 May 2020 22:46

Just in case someone else is looking for the solution, I ended up switching to libvlcpp after spending several hours with trying to get something going.

Basically, you need to define ssize_t in order to get it to work with 3.x.x.

libvlcpp gets around this with this code:

Code: Select all

#ifdef _MSC_VER using ssize_t = long int; #endif
This is placed before includes in the common.hpp file.

In order to make libvlcpp work, with this added, I still needed to add more code to my precompiled header (pch.h by default in visual studio mfc project).

Code: Select all

typedef long int ssize_t;
Again, this is before the includes.

mangokm40
Cone that earned his stripes
Cone that earned his stripes
Posts: 130
Joined: 20 May 2010 20:00

Re: Using LibVLC in Visual Studio (C++) 2019

Postby mangokm40 » 02 Jun 2020 14:42

I'm a neophyte. I'm posting this in case it matters to you.

VS 2017 has a SSIZE_T (capital letters) defined in basetsd.h. It is defined as LONG_PTR, which is a long long in my x64 project.


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 16 guests