diff --git a/Common/3dParty/libvlc/libvlc.pri b/Common/3dParty/libvlc/libvlc.pri index 21804dea2c..f20ce9cec4 100644 --- a/Common/3dParty/libvlc/libvlc.pri +++ b/Common/3dParty/libvlc/libvlc.pri @@ -18,10 +18,12 @@ apple_silicon:greaterThan(QT_MAJOR_VERSION, 5) { INCLUDEPATH += $$PWD HEADERS += \ + $$PWD/vlcinstance.h \ $$PWD/vlcplayer.h \ $$PWD/vlcmedia.h \ $$PWD/libvlc_base.h SOURCES += \ + $$PWD/vlcinstance.cpp \ $$PWD/vlcplayer.cpp \ $$PWD/vlcmedia.cpp diff --git a/Common/3dParty/libvlc/test/main.cpp b/Common/3dParty/libvlc/test/main.cpp index 58f9c3e613..fe3a0aab0e 100644 --- a/Common/3dParty/libvlc/test/main.cpp +++ b/Common/3dParty/libvlc/test/main.cpp @@ -5,6 +5,8 @@ int main(int argc, char* argv[]) { QApplication app(argc, argv); + // set lowest verbosity level + CVlcInstance::setVerbosityLevel(0); Mwindow player; player.show(); diff --git a/Common/3dParty/libvlc/test/player.cpp b/Common/3dParty/libvlc/test/player.cpp index d70b4345d3..a23bdbde51 100644 --- a/Common/3dParty/libvlc/test/player.cpp +++ b/Common/3dParty/libvlc/test/player.cpp @@ -116,7 +116,7 @@ void Mwindow::openFile() QString sFile = QFileDialog::getOpenFileName(this, tr("Load a file")); /* Create a new Media */ - CVlcMedia* pMedia = new CVlcMedia(vlcPlayer->m_pVlcInstance, sFile, false); + CVlcMedia* pMedia = new CVlcMedia(GetVlcInstance(), sFile, false); /* Open media and start playback */ vlcPlayer->open(pMedia); diff --git a/Common/3dParty/libvlc/vlcinstance.cpp b/Common/3dParty/libvlc/vlcinstance.cpp new file mode 100644 index 0000000000..4f36e07268 --- /dev/null +++ b/Common/3dParty/libvlc/vlcinstance.cpp @@ -0,0 +1,59 @@ +#include "vlcinstance.h" + +#include + +unsigned CVlcInstance::m_nVerbose = 0; + +CVlcInstance::CVlcInstance() +{ + const char *argv[2]; + int argc = (m_nVerbose == 0 ? 1 : 2); + char verbose[2]; + + if (m_nVerbose > 0) + { + argv[0] = "--verbose"; + sprintf(verbose, "%d", m_nVerbose); + argv[1] = verbose; + } + else + { + argv[0] = "--quiet"; + } + + m_pVlcInstance = libvlc_new(argc, argv); + + // error if libvlc instantiation was unsuccessful + if (m_pVlcInstance == nullptr) + { + std::cerr << "Could not init libVLC" << std::endl; + exit(1); + } +} + +CVlcInstance::~CVlcInstance() +{ + libvlc_release(m_pVlcInstance); +} + +void CVlcInstance::setVerbosityLevel(unsigned nVerbose) +{ + // verbosity level can be only 0, 1 or 2 + m_nVerbose = std::min(nVerbose, 2u); +} + +CVlcInstance& CVlcInstance::get() +{ + static CVlcInstance oVlcInstance; + return oVlcInstance; +} + +libvlc_instance_t* CVlcInstance::getVlcInstance() +{ + return m_pVlcInstance; +} + +libvlc_instance_t* GetVlcInstance() +{ + return CVlcInstance::get().getVlcInstance(); +} diff --git a/Common/3dParty/libvlc/vlcinstance.h b/Common/3dParty/libvlc/vlcinstance.h new file mode 100644 index 0000000000..882e0e3699 --- /dev/null +++ b/Common/3dParty/libvlc/vlcinstance.h @@ -0,0 +1,32 @@ +#ifndef VLC_INSTANCE_H +#define VLC_INSTANCE_H + +#include "libvlc_base.h" + +// Singleton wrapper around libvlc_instance_t +class CVlcInstance +{ +private: + CVlcInstance(); + ~CVlcInstance(); + +public: + /* NOTE: + * setVerbosityLevel() must be called before first get(). + * If get() was already called once, all subsequent setVerbosityLevel() won't do anything. + */ + static void setVerbosityLevel(unsigned nVerbose); + static CVlcInstance& get(); + libvlc_instance_t* getVlcInstance(); + +public: + static unsigned m_nVerbose; + +private: + libvlc_instance_t* m_pVlcInstance; +}; + +// shorthand for CVlcInstance::get().getVlcInstance() +libvlc_instance_t* GetVlcInstance(); + +#endif // VLC_INSTANCE_H diff --git a/Common/3dParty/libvlc/vlcplayer.cpp b/Common/3dParty/libvlc/vlcplayer.cpp index 8b350b0964..c46a3f2bb8 100644 --- a/Common/3dParty/libvlc/vlcplayer.cpp +++ b/Common/3dParty/libvlc/vlcplayer.cpp @@ -4,16 +4,8 @@ CVlcPlayer::CVlcPlayer(QWidget* parent) : QWidget(parent) { - // initialize libVLC - m_pVlcInstance = libvlc_new(0, NULL); - // error if libvlc instantiation was unsuccessful - if (m_pVlcInstance == nullptr) - { - std::cerr << "Could not init libVLC" << std::endl; - exit(1); - } // initialize vlc media player - m_pVlcPlayer = libvlc_media_player_new(m_pVlcInstance); + m_pVlcPlayer = libvlc_media_player_new(GetVlcInstance()); // disable event handling by vlc internals libvlc_video_set_mouse_input(m_pVlcPlayer, false); libvlc_video_set_key_input(m_pVlcPlayer, false); @@ -32,9 +24,7 @@ CVlcPlayer::CVlcPlayer(QWidget* parent) : QWidget(parent) CVlcPlayer::~CVlcPlayer() { if (m_pVlcPlayer) - libvlc_media_player_release(m_pVlcPlayer); - if (m_pVlcInstance) - libvlc_release(m_pVlcInstance); + libvlc_media_player_release(m_pVlcPlayer); } void CVlcPlayer::onStateChanged(const libvlc_event_t* pEvent, void* pData) diff --git a/Common/3dParty/libvlc/vlcplayer.h b/Common/3dParty/libvlc/vlcplayer.h index be79c4e6bd..1158d51787 100644 --- a/Common/3dParty/libvlc/vlcplayer.h +++ b/Common/3dParty/libvlc/vlcplayer.h @@ -3,6 +3,7 @@ #include "libvlc_base.h" #include "vlcmedia.h" +#include "vlcinstance.h" #include @@ -39,7 +40,6 @@ signals: void positionChanged(float fNewPos); public: - libvlc_instance_t* m_pVlcInstance; libvlc_media_player_t* m_pVlcPlayer; libvlc_event_manager_t* m_pEventManager; };