Commit 7d1af9c7 authored by Casian Andrei's avatar Casian Andrei
Browse files

Ensure no video playback starts while video widget isn't ready

Should fix a bug when starting video playback with a large vlc window
appearing out of nowhere. That happened sometimes because playback was
starting when the video widget wasn't being shown yet and didn't have a
valid window ID.
parent 43eeb5f2
......@@ -26,7 +26,7 @@
#include <QtCore/QFile>
#include <QtCore/QMetaType>
#include <QtCore/QTimer>
#include <QtCore/QTimerEvent>
#include <QtCore/QUrl>
#include "vlc/vlc.h"
......@@ -94,6 +94,8 @@ MediaObject::MediaObject(QObject *parent)
m_seekable = false;
m_seekpoint = 0;
m_isVideoWidgetReady = false;
connect(this, SIGNAL(metaDataNeedsRefresh()), this, SLOT(updateMetaData()));
connect(this, SIGNAL(durationChanged(qint64)), this, SLOT(updateDuration(qint64)));
}
......@@ -114,7 +116,28 @@ MediaObject::~MediaObject()
void MediaObject::setVideoWidget(BaseWidget *widget)
{
this->m_videoWidget = widget;
// Get rid of any old video widget
if (m_videoWidget) {
disconnect(m_videoWidget, SIGNAL(readyForPlayback()));
if (m_vwReadyTimer.isActive())
m_vwReadyTimer.stop();
}
debug() << Q_FUNC_INFO << widget;
m_videoWidget = widget;
if (widget) {
m_isVideoWidgetReady = widget->isVisible();
if (!m_isVideoWidgetReady) {
// Widget not visible yet, must call show and wait for the window system to actually show it
debug() << Q_FUNC_INFO << "video widget not visible";
widget->show();
connect(widget, SIGNAL(readyForPlayback()), this, SLOT(videoWidgetReady()));
} else {
// Don't forget to configure VLC to use this widget
videoWidgetReady();
}
}
}
void MediaObject::play()
......@@ -129,8 +152,6 @@ void MediaObject::play()
// Play the file
playInternal();
}
emit playbackCommenced();
}
void MediaObject::seek(qint64 milliseconds)
......@@ -496,11 +517,15 @@ void MediaObject::unloadMedia()
}
}
void MediaObject::setVLCVideoWidget()
void MediaObject::videoWidgetReady()
{
// Nothing to do if there is no video widget
if (!m_videoWidget)
// Nothing to do if the video widget dissapeared from this media object's connections
if (!m_videoWidget) {
warning() << Q_FUNC_INFO << "No video widget to pass to vlc";
return;
}
m_isVideoWidgetReady = true;
// Get our media player to use our window
#if defined(Q_OS_MAC)
......@@ -512,9 +537,33 @@ void MediaObject::setVLCVideoWidget()
#endif
}
void MediaObject::timerEvent(QTimerEvent* event)
{
if (event->timerId() == m_vwReadyTimer.timerId()) {
// If the video widget dissapears, stop the timer
if (!m_videoWidget)
m_vwReadyTimer.stop();
// Finally, the video widget is ready, can call playInternal() again
if (m_isVideoWidgetReady) {
m_vwReadyTimer.stop();
playInternal();
}
}
}
void MediaObject::playInternal()
{
DEBUG_BLOCK;
// If a video widget is involved, must wait until it is shown
if (m_videoWidget && !m_isVideoWidgetReady) {
m_vwReadyTimer.start(100, this);
warning() << "Cannot play yet, video widget not shown, will try later";
return;
}
if (m_media) { // We are changing media, discard the old one
libvlc_media_release(m_media);
m_media = 0;
......@@ -569,9 +618,6 @@ void MediaObject::playInternal()
// This will reset the GUI
resetMediaController();
// Set up the widget id for libVLC if there is a video widget available
setVLCVideoWidget();
// Play
if (libvlc_media_player_play(m_player)) {
error() << "libVLC:" << LibVLC::errorMessage();
......@@ -583,6 +629,7 @@ void MediaObject::playInternal()
}
emit stateChanged(Phonon::PlayingState);
emit playbackCommenced();
}
void MediaObject::pause()
......@@ -600,6 +647,7 @@ void MediaObject::pause()
// Resume
libvlc_media_player_set_pause(m_player, 0);
emit stateChanged(Phonon::PlayingState);
emit playbackCommenced();
}
} else {
// Pause
......@@ -723,6 +771,7 @@ void MediaObject::eventCallback(const libvlc_event_t *event, void *data)
#endif
if (!that->m_hasVideo && i_first_time_media_player_time_changed < 15) {
debug() << "Looking for Video";
// Update metadata
that->updateMetaData();
......
......@@ -25,12 +25,12 @@
#define PHONON_VLC_MEDIAOBJECT_H
#include <QtCore/QObject>
#include <QtCore/QBasicTimer>
#include <phonon/mediaobjectinterface.h>
#include <phonon/addoninterface.h>
#include "mediacontroller.h"
#include <QtGui/QWidget>
#include "streamreader.h"
// for BaseWidget
......@@ -273,6 +273,9 @@ signals:
void tickInternal(qint64 time);
protected:
void timerEvent(QTimerEvent *event);
private slots:
/**
* Retrieve meta data of a file (i.e ARTIST, TITLE, ALBUM, etc...).
......@@ -306,6 +309,12 @@ private slots:
*/
void moveToNextSource();
/**
* Configures the VLC Media Player to draw the video on the desired widget. The actual function
* call depends on the platform.
*/
void videoWidgetReady();
private:
/**
......@@ -406,14 +415,6 @@ private:
*/
void loadStream();
/**
* Configures the VLC Media Player to draw the video on the desired widget. The actual function
* call depends on the platform.
*
* \see setVideoWidgetId()
*/
void setVLCVideoWidget();
void resume();
/**
......@@ -454,6 +455,9 @@ private:
bool m_hasVideo;
bool m_isScreen;
bool m_isVideoWidgetReady;
QBasicTimer m_vwReadyTimer;
bool m_seekable;
qint64 m_seekpoint;
......
......@@ -292,6 +292,13 @@ void VideoWidget::resizeEvent(QResizeEvent *event)
debug() << "resizeEvent" << event->size();
}
void VideoWidget::showEvent(QShowEvent* event)
{
QWidget::showEvent(event);
emit readyForPlayback();
}
void VideoWidget::setVideoSize(const QSize &size)
{
m_videoSize = size;
......
......@@ -204,6 +204,9 @@ public:
void setVisible(bool visible);
signals:
void readyForPlayback();
public slots:
void setNextFrame(const QByteArray &array, int width, int height);
......@@ -226,6 +229,9 @@ protected:
/* Overloading QWidget */
virtual void resizeEvent(QResizeEvent *event);
/* Overloading QWidget */
virtual void showEvent(QShowEvent *event);
private:
/**
* Sets whether filter adjust is active or not.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment