#include "VLCVideoWidget.h" #include "VLCLoader.h" #include VLCVideoWidget::VLCVideoWidget(QWidget *parent) : QFrame(parent) { m_mediaPlayer = libvlc_media_player_new(VLCLoader::instance()->vlc(), VLCLoader::instance()->exception()); VLCLoader::instance()->checkError(); m_eventManager = libvlc_media_player_event_manager(m_mediaPlayer, VLCLoader::instance()->exception()); VLCLoader::instance()->checkError(); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerOpening, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerBuffering, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPlaying, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPaused, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerStopped, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerForward, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerBackward, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerEndReached, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerEncounteredError, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerTimeChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPositionChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerSeekableChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerPausableChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerTitleChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_attach(m_eventManager, libvlc_MediaPlayerSnapshotTaken, vlcCallbacks, this, VLCLoader::instance()->exception()); VLCLoader::instance()->checkError(); m_media = 0; setAttribute(Qt::WA_PaintOnScreen, true); } VLCVideoWidget::~VLCVideoWidget() { libvlc_event_detach(m_eventManager, libvlc_MediaPlayerOpening, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerBuffering, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPlaying, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPaused, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerStopped, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerForward, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerBackward, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerEndReached, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerEncounteredError, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerTimeChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPositionChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerSeekableChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerPausableChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerTitleChanged, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_event_detach(m_eventManager, libvlc_MediaPlayerSnapshotTaken, vlcCallbacks, this, VLCLoader::instance()->exception()); libvlc_media_player_stop(m_mediaPlayer, VLCLoader::instance()->exception()); libvlc_media_player_release(m_mediaPlayer); if (m_media) libvlc_media_release(m_media); } void VLCVideoWidget::vlcCallbacks(const libvlc_event_t* event, void* ptr) { VLCVideoWidget *self = reinterpret_cast(ptr); switch(event->type) { case libvlc_MediaPlayerOpening: self->emit opening(); break; case libvlc_MediaPlayerBuffering: self->emit buffering(); break; case libvlc_MediaPlayerPlaying: self->emit playing(); break; case libvlc_MediaPlayerPaused: self->emit paused(); break; case libvlc_MediaPlayerStopped: self->emit stopped(); break; case libvlc_MediaPlayerForward: self->emit forward(); break; case libvlc_MediaPlayerBackward: self->emit backward(); break; case libvlc_MediaPlayerEndReached: self->emit endReached(); break; case libvlc_MediaPlayerEncounteredError: self->emit error(); break; case libvlc_MediaPlayerTimeChanged: self->emit timeChanged(); break; case libvlc_MediaPlayerPositionChanged: self->emit positionChanged(); break; case libvlc_MediaPlayerSeekableChanged: self->emit seekableChanged(); break; case libvlc_MediaPlayerPausableChanged: self->emit pausableChanged(); break; case libvlc_MediaPlayerTitleChanged: self->emit titleChanged(); break; case libvlc_MediaPlayerSnapshotTaken: self->emit snapshotTaken(); break; } } /* the file has to be in one of the following formats: [file://]filename Plain media file http://ip:port/file HTTP URL ftp://ip:port/file FTP URL mms://ip:port/file MMS URL screen:// Screen capture [dvd://][device][@raw_device] DVD device [vcd://][device] VCD device [cdda://][device] Audio CD device udp:[[]@[][:]] */ bool VLCVideoWidget::setSource(const QString &source) { m_media = libvlc_media_new(VLCLoader::instance()->vlc(), source.toAscii(), VLCLoader::instance()->exception()); if (!VLCLoader::instance()->checkError()) return false; libvlc_media_player_set_media(m_mediaPlayer, m_media, VLCLoader::instance()->exception()); if (!VLCLoader::instance()->checkError()) return false; return true; } QString VLCVideoWidget::source() const { QString mrl(libvlc_media_get_mrl(m_media, VLCLoader::instance()->exception())); if (VLCLoader::instance()->checkError()) return mrl; return QString(); } bool VLCVideoWidget::play() { attachVideoDisplay(); libvlc_media_player_play(m_mediaPlayer, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::attachVideoDisplay() { if (!effectiveWinId()) return false; #if defined(Q_WS_WIN) libvlc_media_player_set_hwnd(m_mediaPlayer, effectiveWinId(), VLCLoader::instance()->exception()); #elif defined(Q_WS_MAC) libvlc_media_player_set_agl(m_mediaPlayer, effectiveWinId(), VLCLoader::instance()->exception()); #elif defined(Q_WS_X11) libvlc_media_player_set_xwindow(m_mediaPlayer, effectiveWinId(), VLCLoader::instance()->exception()); #endif return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::pause() { libvlc_media_player_pause(m_mediaPlayer, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::stop() { libvlc_media_player_stop(m_mediaPlayer, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::isPlaying() const { bool retVal = libvlc_media_player_is_playing(m_mediaPlayer, VLCLoader::instance()->exception()) == 1; return VLCLoader::instance()->checkError() && retVal; } qint64 VLCVideoWidget::length() const { qint64 retVal = libvlc_media_player_get_length(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } qint64 VLCVideoWidget::time() const { qint64 retVal = libvlc_media_player_get_time(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } bool VLCVideoWidget::setTime(qint64 time) { libvlc_media_player_set_time(m_mediaPlayer, time, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } float VLCVideoWidget::position() const { float retVal = libvlc_media_player_get_position(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1.0; } bool VLCVideoWidget::setPosition(float position) { libvlc_media_player_set_position(m_mediaPlayer, position, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } int VLCVideoWidget::chapter() const { int retVal = libvlc_media_player_get_chapter(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } bool VLCVideoWidget::setChapter(int chapter) { libvlc_media_player_set_chapter(m_mediaPlayer, chapter, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } int VLCVideoWidget::chapterCount() const { int retVal = libvlc_media_player_get_chapter_count(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } bool VLCVideoWidget::willPlay() const { bool retVal = libvlc_media_player_will_play(m_mediaPlayer, VLCLoader::instance()->exception()) == 1; return VLCLoader::instance()->checkError() && retVal; } int VLCVideoWidget::chapterCountForTitle(int title) const { int retVal = libvlc_media_player_get_chapter_count_for_title(m_mediaPlayer, title, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } int VLCVideoWidget::title() const { int retVal = libvlc_media_player_get_title(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } bool VLCVideoWidget::setTitle(int title) { libvlc_media_player_set_title(m_mediaPlayer, title, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } int VLCVideoWidget::titleCount() const { int retVal = libvlc_media_player_get_title_count(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1; } float VLCVideoWidget::fps() const { float retVal = libvlc_media_player_get_fps(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1.0; } libvlc_state_t VLCVideoWidget::state() const { libvlc_state_t retVal = libvlc_media_player_get_state(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return libvlc_Error; } bool VLCVideoWidget::moveToPreviousChapter() { libvlc_media_player_previous_chapter(m_mediaPlayer, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::moveToNextChapter() { libvlc_media_player_next_chapter(m_mediaPlayer, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } float VLCVideoWidget::rate() const { float retVal = libvlc_media_player_get_rate(m_mediaPlayer, VLCLoader::instance()->exception()); if(VLCLoader::instance()->checkError()) return retVal; return -1.0; } bool VLCVideoWidget::setRate(float rate) { libvlc_media_player_set_rate(m_mediaPlayer, rate, VLCLoader::instance()->exception()); return VLCLoader::instance()->checkError(); } bool VLCVideoWidget::isSeekable() const { bool retVal = libvlc_media_player_is_seekable(m_mediaPlayer, VLCLoader::instance()->exception()) == 1; return VLCLoader::instance()->checkError() && retVal; } bool VLCVideoWidget::canPause() const { bool retVal = libvlc_media_player_can_pause(m_mediaPlayer, VLCLoader::instance()->exception()) == 1; return VLCLoader::instance()->checkError() && retVal; } bool VLCVideoWidget::hasVideoOut() const { bool retVal = libvlc_media_player_has_vout(m_mediaPlayer, VLCLoader::instance()->exception()) == 1; return VLCLoader::instance()->checkError() && retVal; } QSize VLCVideoWidget::sizeHint() const { if (!hasVideoOut()) return QSize(640, 480); //HACK: fix this? return QSize(width(), height()); } int VLCVideoWidget::width() const { int width = libvlc_video_get_width(m_mediaPlayer, VLCLoader::instance()->exception()); if (!VLCLoader::instance()->checkError()) return -1; return width; } int VLCVideoWidget::height() const { int height = libvlc_video_get_height(m_mediaPlayer, VLCLoader::instance()->exception()); if (!VLCLoader::instance()->checkError()) return -1; return height; } QImage VLCVideoWidget::currentFrameImage() const { QTemporaryFile temp; if (!temp.open()) return QImage(); libvlc_video_take_snapshot(m_mediaPlayer, temp.fileName().toAscii(), width(), height(), VLCLoader::instance()->exception()); if (!VLCLoader::instance()->checkError()) return QImage(); return QImage(temp.fileName()); }