mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
Separate class CVlcPlayer with vlc player logic
This commit is contained in:
@ -14,3 +14,10 @@ core_windows:vs2019 {
|
||||
apple_silicon:greaterThan(QT_MAJOR_VERSION, 5) {
|
||||
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.15
|
||||
}
|
||||
|
||||
INCLUDEPATH += $$PWD
|
||||
|
||||
HEADERS += $$PWD/vlcplayer.h \
|
||||
$$PWD/libvlc_base.h
|
||||
|
||||
SOURCES += $$PWD/vlcplayer.cpp
|
||||
|
||||
8
Common/3dParty/libvlc/libvlc_base.h
Normal file
8
Common/3dParty/libvlc/libvlc_base.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef LIBVLC_BASE_H
|
||||
#define LIBVLC_BASE_H
|
||||
|
||||
typedef long int ssize_t;
|
||||
|
||||
#include <vlc/vlc.h>
|
||||
|
||||
#endif // LIBVLC_BASE_H
|
||||
@ -1,23 +1,8 @@
|
||||
#include <QApplication>
|
||||
#include "player.h"
|
||||
|
||||
#ifdef Q_WS_X11
|
||||
#include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
#ifdef QT_STATIC
|
||||
# include <QtPlugin>
|
||||
# ifdef _WIN32
|
||||
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
#ifdef Q_WS_X11
|
||||
XInitThreads();
|
||||
#endif
|
||||
|
||||
QApplication app(argc, argv);
|
||||
|
||||
Mwindow player;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
#include "player.h"
|
||||
#include <vlc/vlc.h>
|
||||
|
||||
#define qtu(i) ((i).toUtf8().constData())
|
||||
|
||||
@ -14,17 +13,8 @@
|
||||
|
||||
Mwindow::Mwindow()
|
||||
{
|
||||
vlcPlayer = NULL;
|
||||
|
||||
/* Initialize libVLC */
|
||||
vlcInstance = libvlc_new(0, NULL);
|
||||
|
||||
/* Complain in case of broken installation */
|
||||
if (vlcInstance == NULL)
|
||||
{
|
||||
QMessageBox::critical(this, "Qt libVLC player", "Could not init libVLC");
|
||||
exit(1);
|
||||
}
|
||||
// create vlc player
|
||||
vlcPlayer = new CVlcPlayer();
|
||||
|
||||
/* Interface initialization */
|
||||
initUI();
|
||||
@ -32,9 +22,7 @@ Mwindow::Mwindow()
|
||||
|
||||
Mwindow::~Mwindow()
|
||||
{
|
||||
/* Release libVLC instance on quit */
|
||||
if (vlcInstance)
|
||||
libvlc_release(vlcInstance);
|
||||
delete vlcPlayer;
|
||||
}
|
||||
|
||||
void Mwindow::initUI()
|
||||
@ -118,6 +106,9 @@ void Mwindow::initUI()
|
||||
centralWidget->setLayout(layout2);
|
||||
setCentralWidget(centralWidget);
|
||||
resize(600, 400);
|
||||
|
||||
/* Integrate the video in the interface */
|
||||
vlcPlayer->integrateIntoWidget(videoWidget);
|
||||
}
|
||||
|
||||
void Mwindow::openFile()
|
||||
@ -126,32 +117,18 @@ void Mwindow::openFile()
|
||||
/* The basic file-select box */
|
||||
QUrl url = QFileDialog::getOpenFileUrl(this, tr("Load a file"));
|
||||
|
||||
/* Stop if something is playing */
|
||||
if (vlcPlayer && libvlc_media_player_is_playing(vlcPlayer))
|
||||
stop();
|
||||
|
||||
/* Create a new Media */
|
||||
libvlc_media_t *vlcMedia = libvlc_media_new_location(vlcInstance, qtu(url.toString(QUrl::FullyEncoded)));
|
||||
libvlc_media_t *vlcMedia = libvlc_media_new_location(vlcPlayer->getVlcInstance(), qtu(url.toString(QUrl::FullyEncoded)));
|
||||
if (!vlcMedia)
|
||||
return;
|
||||
|
||||
/* Create a new libvlc player */
|
||||
vlcPlayer = libvlc_media_player_new_from_media(vlcMedia);
|
||||
vlcPlayer->open(vlcMedia);
|
||||
|
||||
/* Release the media */
|
||||
libvlc_media_release(vlcMedia);
|
||||
|
||||
/* Integrate the video in the interface */
|
||||
#if defined(Q_OS_MAC)
|
||||
libvlc_media_player_set_nsobject(vlcPlayer, (void *)videoWidget->winId());
|
||||
#elif defined(Q_OS_UNIX)
|
||||
libvlc_media_player_set_xwindow(vlcPlayer, videoWidget->winId());
|
||||
#elif defined(Q_OS_WIN)
|
||||
libvlc_media_player_set_hwnd(vlcPlayer, (HWND)videoWidget->winId());
|
||||
#endif
|
||||
|
||||
/* And start playback */
|
||||
libvlc_media_player_play(vlcPlayer);
|
||||
vlcPlayer->play();
|
||||
|
||||
/* Update playback button */
|
||||
playBut->setText("Pause");
|
||||
@ -162,16 +139,16 @@ void Mwindow::play()
|
||||
if (!vlcPlayer)
|
||||
return;
|
||||
|
||||
if (libvlc_media_player_is_playing(vlcPlayer))
|
||||
if (libvlc_media_player_is_playing(vlcPlayer->m_pVlcPlayer))
|
||||
{
|
||||
/* Pause */
|
||||
libvlc_media_player_pause(vlcPlayer);
|
||||
libvlc_media_player_pause(vlcPlayer->m_pVlcPlayer);
|
||||
playBut->setText("Play");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Play again */
|
||||
libvlc_media_player_play(vlcPlayer);
|
||||
libvlc_media_player_play(vlcPlayer->m_pVlcPlayer);
|
||||
playBut->setText("Pause");
|
||||
}
|
||||
}
|
||||
@ -179,17 +156,14 @@ void Mwindow::play()
|
||||
int Mwindow::changeVolume(int vol)
|
||||
{ /* Called on volume slider change */
|
||||
|
||||
if (vlcPlayer)
|
||||
return libvlc_audio_set_volume(vlcPlayer, vol);
|
||||
|
||||
vlcPlayer->setVolume(vol);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Mwindow::changePosition(int pos)
|
||||
{ /* Called on position slider change */
|
||||
|
||||
if (vlcPlayer)
|
||||
libvlc_media_player_set_position(vlcPlayer, (float)pos / 1000.0);
|
||||
libvlc_media_player_set_position(vlcPlayer->m_pVlcPlayer, (float)pos / 1000.0);
|
||||
}
|
||||
|
||||
void Mwindow::updateInterface()
|
||||
@ -199,11 +173,11 @@ void Mwindow::updateInterface()
|
||||
return;
|
||||
|
||||
/* update the timeline */
|
||||
float pos = libvlc_media_player_get_position(vlcPlayer);
|
||||
float pos = libvlc_media_player_get_position(vlcPlayer->m_pVlcPlayer);
|
||||
slider->setValue((int)(pos * 1000.0));
|
||||
|
||||
/* Stop the media */
|
||||
if (libvlc_media_player_get_state(vlcPlayer) == libvlc_Ended)
|
||||
if (vlcPlayer->getState() == libvlc_Ended)
|
||||
this->stop();
|
||||
}
|
||||
|
||||
@ -212,13 +186,9 @@ void Mwindow::stop()
|
||||
if (vlcPlayer)
|
||||
{
|
||||
/* stop the media player */
|
||||
libvlc_media_player_stop(vlcPlayer);
|
||||
|
||||
/* release the media player */
|
||||
libvlc_media_player_release(vlcPlayer);
|
||||
vlcPlayer->stop();
|
||||
|
||||
/* Reset application values */
|
||||
vlcPlayer = NULL;
|
||||
slider->setValue(0);
|
||||
playBut->setText("Play");
|
||||
}
|
||||
|
||||
@ -1,14 +1,12 @@
|
||||
#ifndef PLAYER
|
||||
#define PLAYER
|
||||
|
||||
typedef long int ssize_t;
|
||||
#include "../vlcplayer.h"
|
||||
|
||||
#include <QtGui>
|
||||
#include <QMainWindow>
|
||||
#include <QPushButton>
|
||||
#include <QSlider>
|
||||
#include <QWidget>
|
||||
#include <vlc/vlc.h>
|
||||
|
||||
class Mwindow : public QMainWindow
|
||||
{
|
||||
@ -39,8 +37,7 @@ private:
|
||||
QSlider *slider;
|
||||
QWidget *videoWidget;
|
||||
|
||||
libvlc_instance_t *vlcInstance;
|
||||
libvlc_media_player_t *vlcPlayer;
|
||||
CVlcPlayer* vlcPlayer;
|
||||
|
||||
void initUI();
|
||||
};
|
||||
|
||||
116
Common/3dParty/libvlc/vlcplayer.cpp
Normal file
116
Common/3dParty/libvlc/vlcplayer.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include "vlcplayer.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
CVlcPlayer::CVlcPlayer()
|
||||
{
|
||||
// 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);
|
||||
// get event manager
|
||||
m_pEventManager = libvlc_media_player_event_manager(m_pVlcPlayer);
|
||||
|
||||
// attach event listeners
|
||||
for (int nEvent = static_cast<int>(libvlc_MediaPlayerNothingSpecial); nEvent <= static_cast<int>(libvlc_MediaPlayerEncounteredError); nEvent++)
|
||||
{
|
||||
libvlc_event_attach(m_pEventManager, nEvent, onStateChanged, this);
|
||||
}
|
||||
libvlc_event_attach(m_pEventManager, libvlc_MediaPlayerTimeChanged , onTimeChanged, this);
|
||||
}
|
||||
|
||||
CVlcPlayer::~CVlcPlayer()
|
||||
{
|
||||
if (m_pVlcInstance)
|
||||
libvlc_release(m_pVlcInstance);
|
||||
if (m_pVlcPlayer)
|
||||
libvlc_media_player_release(m_pVlcPlayer);
|
||||
}
|
||||
|
||||
void CVlcPlayer::onStateChanged(const libvlc_event_t* pEvent, void* pData)
|
||||
{
|
||||
CVlcPlayer* pVlcPlayer = reinterpret_cast<CVlcPlayer*>(pData);
|
||||
emit pVlcPlayer->stateChanged(pVlcPlayer->getState());
|
||||
}
|
||||
|
||||
void CVlcPlayer::onTimeChanged(const libvlc_event_t* pEvent, void* pData)
|
||||
{
|
||||
CVlcPlayer* pVlcPlayer = reinterpret_cast<CVlcPlayer*>(pData);
|
||||
emit pVlcPlayer->timeChanged(pEvent->u.media_player_time_changed.new_time);
|
||||
}
|
||||
|
||||
void CVlcPlayer::integrateIntoWidget(QWidget* pWidget)
|
||||
{
|
||||
#if defined(_MAC)
|
||||
libvlc_media_player_set_nsobject(m_pVlcPlayer, reinterpret_cast<void*>(pWidget->winId()));
|
||||
#elif defined(_LINUX)
|
||||
libvlc_media_player_set_xwindow(m_pVlcPlayer, pWidget->winId());
|
||||
#elif defined(_WIN32)
|
||||
libvlc_media_player_set_hwnd(m_pVlcPlayer, reinterpret_cast<HWND>(pWidget->winId()));
|
||||
#endif
|
||||
}
|
||||
|
||||
void CVlcPlayer::open(libvlc_media_t* pMedia)
|
||||
{
|
||||
// stop playing old media
|
||||
if (libvlc_media_player_is_playing(m_pVlcPlayer))
|
||||
stop();
|
||||
|
||||
// set new media
|
||||
libvlc_media_player_set_media(m_pVlcPlayer, pMedia);
|
||||
}
|
||||
|
||||
void CVlcPlayer::pause()
|
||||
{
|
||||
// set on pause if currently playing
|
||||
if (libvlc_media_player_is_playing(m_pVlcPlayer))
|
||||
{
|
||||
libvlc_media_player_pause(m_pVlcPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
void CVlcPlayer::play()
|
||||
{
|
||||
// play if not playing already
|
||||
if (!libvlc_media_player_is_playing(m_pVlcPlayer))
|
||||
{
|
||||
libvlc_media_player_play(m_pVlcPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
void CVlcPlayer::setVolume(int nVolume)
|
||||
{
|
||||
libvlc_audio_set_volume(m_pVlcPlayer, nVolume);
|
||||
}
|
||||
|
||||
void CVlcPlayer::setTime(int nTime)
|
||||
{
|
||||
libvlc_media_player_set_time(m_pVlcPlayer, nTime);
|
||||
}
|
||||
|
||||
bool CVlcPlayer::isAudio()
|
||||
{
|
||||
return !libvlc_media_player_has_vout(m_pVlcPlayer);
|
||||
}
|
||||
|
||||
libvlc_state_t CVlcPlayer::getState()
|
||||
{
|
||||
return libvlc_media_player_get_state(m_pVlcPlayer);
|
||||
}
|
||||
|
||||
libvlc_instance_t* CVlcPlayer::getVlcInstance()
|
||||
{
|
||||
return m_pVlcInstance;
|
||||
}
|
||||
|
||||
void CVlcPlayer::stop()
|
||||
{
|
||||
// stop the media player
|
||||
libvlc_media_player_stop(m_pVlcPlayer);
|
||||
}
|
||||
44
Common/3dParty/libvlc/vlcplayer.h
Normal file
44
Common/3dParty/libvlc/vlcplayer.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef VLC_PLAYER_H
|
||||
#define VLC_PLAYER_H
|
||||
|
||||
#include "libvlc_base.h"
|
||||
|
||||
#include "QWidget"
|
||||
|
||||
class CVlcPlayer : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CVlcPlayer();
|
||||
virtual ~CVlcPlayer();
|
||||
|
||||
public:
|
||||
static void onStateChanged(const libvlc_event_t *pEvent, void *pData);
|
||||
static void onTimeChanged(const libvlc_event_t *pEvent, void *pData);
|
||||
|
||||
public:
|
||||
void integrateIntoWidget(QWidget* pWidget);
|
||||
void open(libvlc_media_t* pMedia);
|
||||
void pause();
|
||||
void play();
|
||||
void stop();
|
||||
|
||||
void setVolume(int nVolume);
|
||||
void setTime(int nTime);
|
||||
|
||||
bool isAudio();
|
||||
libvlc_state_t getState();
|
||||
libvlc_instance_t* getVlcInstance();
|
||||
|
||||
signals:
|
||||
void stateChanged(libvlc_state_t newState);
|
||||
void timeChanged(libvlc_time_t nNewTime);
|
||||
|
||||
public:
|
||||
libvlc_instance_t* m_pVlcInstance;
|
||||
libvlc_media_player_t* m_pVlcPlayer;
|
||||
libvlc_event_manager_t* m_pEventManager;
|
||||
};
|
||||
|
||||
#endif // VLC_PLAYER_H
|
||||
Reference in New Issue
Block a user