How to use LibVLC with Qt 5 - c++

I'm trying to use LibVLC in a Qt 5 program to open a VLC instance and play a video.
The following code comes from https://wiki.videolan.org/LibVLC_Tutorial/
I'm using Linux.
.pro :
TEMPLATE = app
TARGET = projectLoic
INCLUDEPATH += . vlc
QT += widgets
# Input
HEADERS +=
SOURCES += main.cpp
LIBS +=-lvlc
main :
#include <vlc/vlc.h>
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, 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_path (inst, "/home/........mp3");
// Create a media player playing environement
mp = libvlc_media_player_new_from_media (m);
// play the media_player
libvlc_media_player_play (mp);
return app.exec();
}
The compilation is fine. But the program immediatly crashes when I build it (with Qt Creator). Any idea?
Many thanks

Many things could cause this crash. The best is to get VLC source code to trace back the issue. Passing the option '--verbose=2' when initializing libVLC can help as well.
In my case the cause of the crash was due to this bug in the ubuntu package of vlc:
https://bugs.launchpad.net/ubuntu/+source/vlc/+bug/1328466
When calling libvlc_new() vlc modules and their dependent libraries are loaded into memory. The qt module of LibVLC was searching for Qt4 shared objects instead of Qt5 (manually installed).
The solution was to rebuild the module cache which was outdated an pointing to Qt4 binaries. You can reset it on the command line:
sudo /usr/lib/vlc/vlc-cache-gen -f /usr/lib/vlc/plugins/
Or pass to vlc the option:
--reset-plugins-cache

I have never used this library, but Are you using exactly this code?
m = libvlc_media_new_path (inst, "/home/........mp3");
This path may be the problem.

What distribution of Linux are you using?
I ran into this same problem with Qt5 and LibVLC, and the primary cause (Ubuntu 12.04 LTS and 14.04 LTS), was that LibVLC was loading the qt4 interface plugin, which conflicted with Qt5. If you check your call stack, you'll most likely see that at a Qt4 library was being loaded, causing the crash.
If this is the problem, there are only 3 options (tested with LibVLC 2.2 and Qt5 on Ubuntu 12.04 and 14.04 LTS 64 bit).
The first (worst) is to delete the qt4 user interface plugin. You can test this is the problem by moving and running and then setting it back. Deleting will break your regular VLC player most likely.
Second option is to create a copy of the plugins directory and set that in your runtime path using VLC_PLUGIN_PATH, environment variable. but I've had trouble getting that to work without changing the original plugin path folder also (which will break your VLC player unless you modify your shortcuts, etc. to also set VLC_PLUGIN_PATH.
The third option, and what I ended up doing, was to custom build my own static LibVLC binary with a few edits to the source code so that it will not load the qt4 interface plugin installed with VLC. You can follow these steps to do this.
1) Download VLC Source Code for your platforms distribution.
http://download.videolan.org/pub/videolan/vlc/
Make sure you download the version matching your distribution. For
example, match the VLC version to what is installed with Ubuntu.
2) Extract source code to folder
3) Install dependencies for the OS distribution
sudo apt-get build-dep vlc
4) Modify src/modules/bank.c
Edit the module_InitDynamic function
Add the following code at the top of the function:
// HACK TO DISABLE QT4 PLUGIN
if(strstr(path, "qt4") != NULL)
return NULL;
// END HACK
3) From terminal
./bootstrap
./configure --disable-qt --disable-skins2 --enable-xcb --prefix=/home/$USER/vlc-custom_build_output_folder_name
./make
./make install
4) Copy/Save the resulting files in the install folder.
Then just link against this library instead.

Related

"Failed to setup resampler" when starting QAudioSink

I'm porting some QtMultimedia code from Qt 5.15 to 6.4.1. The following program, when built with Qt 6.4.1 on Windows:
int main (int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QAudioDevice device = QMediaDevices::defaultAudioOutput();
QAudioFormat format = device.preferredFormat();
QAudioSink *output = new QAudioSink(device, format);
output->start();
return a.exec();
}
Fails to start the audio output, printing the following message:
qt.multimedia.audiooutput: Failed to setup resampler
The equivalent code in Qt5 (using QAudioDeviceInfo and QAudioOutput) seems to run fine. What am I missing here?
Apparently, it's a bug in Qt 6.4.1 on Windows, where, as the user johnco3 discovered in that forum post, for some reason QAudioSink is looking for a DLL named "mfplat.dll.dll" when it should be looking for "mfplat.dll" (it adds an extra ".dll" suffix).
The correctly named version of this DLL lives in the Windows system directory (e.g. C:\Windows\System32\mfplat.dll), so there are a couple of workaround until the bug is fixed:
Go back to Qt 6.4.0, apparently it's a new issue in 6.4.1, or
Copy mfplat.dll to somewhere in the DLL path then rename it to "mfplat.dll.dll":
Either copy it to the application executable's directory and rename it there, or
Create some folder somewhere, copy and rename it there, then add that folder to the PATH environment variable.
It's a somewhat silly bug, but alas. At least the workaround exists and can be easily undone when the bug is eventually fixed.
See also:
https://bugreports.qt.io/browse/QTBUG-108383 (johnco3's bug report)
https://bugreports.qt.io/browse/QTBUG-108669 (a duplicate bug report; I filed it before I found any of this)

Invisible text in QTextEdit

I am learning Qt and running examples from Qt SDK 5.9.1. I run the code below and write inside QTextEdit but no text appears. Cursor moves as I write but no text is shown. Window title text is shown. I added addApplicationFont and setFont calls below I found from web to the sample but it didn't help.
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QFontDatabase::addApplicationFont("://Ubuntu-R.ttf");
app.setFont(QFont("Ubuntu", 11, QFont::Normal, false));
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
I am on Ubuntu 16.04 and run following commands on bash to make executable:
qmake -makefile
make
./part1
I want the app to use the default Ubuntu system font. I learned that Qt uses fontconfig for fonts but I don't know how to trace the issue.
Edit
I thought QFontDatabase::addApplicationFont("://Ubuntu-R.ttf") call referenced system font but instead it is referencing font app resource file. I don't have resource file so obviously it won't work.
.pro file is below(unmodified sample file):
QT += widgets
SOURCES = main.cpp
# install
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/tutorials/gettingStarted/gsQt/part1
INSTALLS += target
I tried to get system font using QFontDatabase but it didn't work:
app.setFont(QFontDatabase::systemFont(QFontDatabase::GeneralFont));
This doesn't do anything with any of enum values including QFontDatabase::GeneralFont
QFontDatabase database;
QStringList fam = database.families();
fam size is zero.
I will try to use embedded font next.
I don't know the exact reason of the problem but the reason was not configuring fontconfig dependency properly before building qt. I solved it by reconfiguring and recompiling qt again. You can find more details at qt forum.

Qt5: Preventing another instance of the application doesn't work anymore...!

I am using Qt5 on a Windows7 platform.
My application is some kind of TCP server listening on port 8002, so I only want one instance of it.
In order to prevent multiple instances of my application, I use(d) the code below (found here on StackOverflow):
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSharedMemory sharedMemory;
sharedMemory.setKey("TcpServer-Key");
if(sharedMemory.create(1) == false)
{
QMessageBox::warning(NULL, "Warning!", "Another instance already running!");
a.exit(); // exit already a process running
return 0;
}
...
Well, the code above used to work just fine, until I upgraded my Qt to 5.5.1.
Now, with Qt 5.5.1, I don't see the warning message-box anymore!... When I try to start another instance, the running app disappears/stops and a new app is started!!!
Please help, what should I do? But don't tell me to switch back to Qt 5.4.x :(
Remark: I forgot to mention that I set & used msvc2012 compiler during tests (not minGW, as I wasn't able to build log4cxx for it).
UPDATE: Could it be an issue related to the antivirus installed on that PC (at the office, i.e. McAfee)?... Now I'm at home (AVG antivirus and MinGW compiler and log4cxx removed) and I am unable to reproduce the above described issue :(
I finally discovered where the problem was... and it's not the antivirus to be blamed :)
When I upgraded the Qt (Creator v3.6.0) to the newest version (5.5.1), there is a setting in Tools->Options->Build&Run named [Stop app before building]... that was set to Current project or something. Hence, the Qt Creator was killing the old instance before launching a new one(!).
Setting this option to None solved the issue.
So, it seems the code was just fine, antivirus was fine, yet launching the app from within Qt Creator was somehow restricted to only one instance :)
I decided to share this, maybe it will be helpful for other folks as well.
Remark : I checked again and now I can confirm: That setting didn't exist before, at least not in Qt Creator v3.3.2.

How to debug Qt dll issues with cross compiled builds?

After cross-compiling Qt 5 applications (host: Fedora 19/64 bit, target: Windows 32 bit) I execute following steps for deploying the executable:
$ DEST=/windows/testdir
$ cp /usr/i686-w64-mingw32/sys-root/mingw/bin/*.dll $DEST
$ mkdir $DEST/platforms
$ cp /usr/i686-w64-mingw32/sys-root/mingw/lib/qt5/plugins/platforms/qwindows.dll\
$DEST/platforms
$ cp release/main.exe $DEST # the cross-compiled Qt5 binary
I test it on Windows like this:
say /windows is mounted on f:
start command prompt window
f:
cd testdir
main
And there I get:
Failed to load platform plugin "windows". Available platforms are:
Microsoft Visual C++ Runtime Library
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I don't really believe the first message because:
a) the above steps worked in the past (executed on the same Fedora 19 system)
b) the platforms directory is there as documented in the qt docs.
What changed is that now the application includes some PNGs/JPGs in dialogs (read via Qt's resource file system, as QIcons).
Thus, I've also copied some plugins:
$ cp -r /usr/i686-w64-mingw32/sys-root/mingw/lib/qt5/plugins $DEST
Which didn't help to resolve the above issue.
Conclusion
Is there a way to debug dynamic runtime linker issues like these?
Can I instruct it such that I somehow get output on which dll's the app/linker tries to load and where is does its lookups? (and why they fail ...)
For example something like this would be great:
ldd: main.exe -> load of foo.dll in work-dir failed (no such file)
ldd: main.exe -> load of bar.dll in work-dir/platforms failed (wrong file format)
ldd: main.exe -> load of baz.dll in work-dir successful
...
Compile steps
I used following steps for cross-compiling on Fedora 19:
$ mingw32-qmake-qt5 main.pro -o win32.mf
$ mingw32-make -f win32.mf
$ # -> binary is created in release/main.exe
Wine
I've looked at wine for testing purposes. It is helpful because it displays an error message when it can't find a DLL, e.g.:
$ wine $DEST/main.exe
err:module:import_dll Library libEGL.dll (which is needed by L"Z:\\usr\\i686-w64-mingw32\\sys-root\\mingw\\lib\\qt5\\plugins\\platforms\\qwindows.dll") not found
err:module:import_dll Library libjpeg-62.dll (which is needed by L"Z:\\usr\\i686-w64-mingw32\\sys-root\\mingw\\lib\\qt5\\plugins\\imageformats\\qjpeg.dll") not found
Interestingly, it directly finds the platforms library and needed plugin under Z:\\usr\\i686-w64-mingw32\\sys-root\\mingw\\lib\\qt5\\.
But when all needed DLLs from /usr/i686-w64-mingw32/sys-root/mingw/bin/*.dll are copied to $DEST, wine runs the same main.exe just fine - where on native windows (7) I get the above error boxes.
You can use tools like Dependency Walker for checking dependencies of a single DLL, Wine for quickly checking startup on the compile-host and Process Monitor to see which directories/files are accessed during runtime of the process.
It also makes sense to debug-output the library path from the Qt application, e.g.
int main(int argc, char **argv)
{
qDebug() << "Library paths: " << QApplication::libraryPaths();
QApplication app(argc, argv);
...
With that I get following output on native windows:
Library paths: ()
(To enable qDebug() statements - even with release binaries - you have to add CONFIG += console to your qmake project file.)
Looking at the Process Monitor output it seems that the binary does not try to open any plugins (platforms or other) in its current working directory (CWD) nor in its base directory.
When I extend the library path the binary finds all needed plugins in its CWD:
int main(int argc, char **argv)
{
QApplication::addLibraryPath(QDir::currentPath());
QApplication app(argc, argv);
...
I don't know if this qualifies as a work-around - perhaps one is supposed to do something like this. But the Qt documentation seems to suggest opposite:
To deploy the application, we must make sure that we copy the relevant Qt DLL (corresponding to the Qt modules used in the application) and the windows platform plugin as well as the executable to the same directory in the release subdirectory.
The complete deploy procedure is now:
$ cp /usr/i686-w64-mingw32/sys-root/mingw/bin/*.dll $DEST
# copying platforms, imageformats etc. plugin directories:
$ cp /usr/i686-w64-mingw32/sys-root/mingw/lib/qt5/plugins/* $DEST -r
$ cp release/main.exe $DEST
(Depending on what packages are installed on your compile-host you probably don't need to copy all DLLs - with wine it is easy to start a fixpoint iteration for all needed non-plugin dlls.)
Missing non-platform plugins don't necessarily abort the programs startup - e.g. without a jpeg plugin some icons are just not displayed.

Qt Creator + OpenCV: Program runs from .exe but not from editor

Well, I need to start working with OpenCV and as I'm used to working with QtCreator, I'm trying to make it all work together. I downloaded the latest OpenCV version, and compiled it with MinGW. Then, I created this little console project to try it out. Below is the .pro file:
QT += core
QT -= gui
TARGET = OpenCV_test4
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\Librerias\\opencv2.3.1\\release\\include
LIBS += -LC:\\Librerias\\opencv2.3.1\\release\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
Here is the main.cpp file:
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("OpenCV Window");
// show the image on window
cv::imshow("OpenCV Window", image);
// wait key for 5000 ms
cv::waitKey(5000);
return a.exec();
}
(I have tried this code with and without the QCoreApplication lines)
The deal is: It links and builds, and when runs from QtCreator only a terminal window called C:\QtSDK\QtCreator\bin\qtcreator_process_stub.exe appears with the line "Press RETURN to close this window..."
But, if I run the .exe from the project folder, it runs perfectly!! Why is QtCreator unable to launch the application? I found this really strange, and I would appreciate any hint about this. It's really not THAT important, but it is kind of a pain to have to run the .exe manually every time I change something to check how it works.
Thanks for your time :)
Additional Info:
I have tried both debug and release versions, the problem is the same in both of them.
Debugging does not work, it never stops at any breakpoint.
I'm running on Windows 7 Proffesional x64
SOLVED, I don't really know what I did, it suddenly worked and keeps working, I wish I could tell you how I fixed it but I have no idea, such a weird thing :(
Check Projects -> Run Settings -> Run in terminal. It have to be enabled, but seems to be disabled.
I have met same problem with QtCreator and OpenCL under Linux. Simple test program works after start from terminal and it does not work after start from the QtCreator.
I found the cause was hardcoded LD_LIBRARY_PATH in the project's run environment settings. I had dropped it to empty string and this had fixed issue.
I had the same issue with the following environment: Raspbian, Qt, openCV and a gui application.
old-ufo recommendation worked for me:
- First, enable "Run in terminal", which failed
- Then, disable "Run in terminal", which allowed me to correctly debug my app.
I understand that this is not scientific.