summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/player/mediaapp.cpp85
-rw-r--r--examples/player/mediaapp.h59
-rw-r--r--examples/player/player.cpp168
-rw-r--r--examples/player/player.h8
4 files changed, 175 insertions, 145 deletions
diff --git a/examples/player/mediaapp.cpp b/examples/player/mediaapp.cpp
index 5809340..36b33f9 100644
--- a/examples/player/mediaapp.cpp
+++ b/examples/player/mediaapp.cpp
@@ -19,7 +19,8 @@
#include <QtCore/QTime>
#include "mediaapp.h"
-MediaApp::MediaApp(QWidget *parent) : QWidget(parent) {
+MediaApp::MediaApp(QWidget *parent) : QWidget(parent)
+{
player = new Player(this);
player->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
player->setAutoFillBackground(false);
@@ -31,7 +32,7 @@ MediaApp::MediaApp(QWidget *parent) : QWidget(parent) {
connect(&fsTimer, SIGNAL(timeout()), this, SLOT(hideControls()));
QVBoxLayout *appLayout = new QVBoxLayout;
- appLayout->setContentsMargins(0,0,0,0);
+ appLayout->setContentsMargins(0, 0, 0, 0);
createUI(appLayout);
setLayout(appLayout);
@@ -40,30 +41,36 @@ MediaApp::MediaApp(QWidget *parent) : QWidget(parent) {
resize(400, 400);
}
-MediaApp::~MediaApp() {
+MediaApp::~MediaApp()
+{
delete player;
}
-void MediaApp::open() {
+void MediaApp::open()
+{
player->stop();
QString fileName = QFileDialog::getOpenFileName(
- this, tr("Open a Movie"), baseDir);
+ this, tr("Open a Movie"), baseDir);
- if (!fileName.isEmpty())
+ if (!fileName.isEmpty()) {
openFile(fileName);
+ }
}
-void MediaApp::mouseMoveEvent(QMouseEvent* event) {
- if(isFullScreen()){
- if(!openButton->isVisible())
+void MediaApp::mouseMoveEvent(QMouseEvent* event)
+{
+ if (isFullScreen()) {
+ if (!openButton->isVisible()) {
showControls(true);
+ }
fsTimer.start(3000);
}
}
-void MediaApp::showControls(bool show) {
+void MediaApp::showControls(bool show)
+{
openButton->setVisible(show);
fullScreenButton->setVisible(show);
playButton->setVisible(show);
@@ -73,8 +80,9 @@ void MediaApp::showControls(bool show) {
posLabel->setVisible(show);
}
-void MediaApp::toggleFullScreen() {
- if(isFullScreen()){
+void MediaApp::toggleFullScreen()
+{
+ if (isFullScreen()) {
setMouseTracking(false);
fsTimer.stop();
showNormal();
@@ -85,57 +93,61 @@ void MediaApp::toggleFullScreen() {
}
}
-void MediaApp::openFile(const QString &fileName) {
+void MediaApp::openFile(const QString &fileName)
+{
baseDir = QFileInfo(fileName).path();
player->setUri(fileName);
player->play();
}
-void MediaApp::posChanged() {
+void MediaApp::posChanged()
+{
QTime length = player->length();
QTime curpos = player->position();
posLabel->setText(curpos.toString() + "/" + length.toString());
- if(length != QTime(0,0,0,0)){
- posSlider->setValue(curpos.msecsTo(QTime(0,0,0,0)) * 100 /
- length.msecsTo(QTime(0,0,0,0)));
+ if (length != QTime(0, 0, 0, 0)) {
+ posSlider->setValue(curpos.msecsTo(QTime(0, 0, 0, 0)) * 100 /
+ length.msecsTo(QTime(0, 0, 0, 0)));
}
- if(curpos != QTime(0,0,0,0)){
+ if (curpos != QTime(0, 0, 0, 0)) {
posLabel->setEnabled(true);
posSlider->setEnabled(true);
}
}
-void MediaApp::stateChanged() {
+void MediaApp::stateChanged()
+{
playButton->setEnabled(player->state() != QGst::StatePlaying);
pauseButton->setEnabled(player->state() == QGst::StatePlaying);
stopButton->setEnabled(player->state() != QGst::StateReady);
- if(player->state() == QGst::StateReady){
+ if (player->state() == QGst::StateReady) {
posLabel->setText("00:00:00/00:00:00");
posSlider->setValue(0);
}
}
-void MediaApp::setPos(int value) {
- quint64 length = -player->length().msecsTo(QTime(0,0,0,0));
- if(length != 0 && value > 0){
- QTime pos(0,0,0,0);
- pos = pos.addMSecs( length*value/100);
+void MediaApp::setPos(int value)
+{
+ quint64 length = -player->length().msecsTo(QTime(0, 0, 0, 0));
+ if (length != 0 && value > 0) {
+ QTime pos(0, 0, 0, 0);
+ pos = pos.addMSecs(length * value / 100);
player->setPosition(pos);
}
}
inline QToolButton* MediaApp::initButton(
- QStyle::StandardPixmap icon,
- const char *tip,
- QObject *dstobj, const char* slot_method,
- QLayout *layout)
+ QStyle::StandardPixmap icon,
+ const char *tip,
+ QObject *dstobj, const char* slot_method,
+ QLayout *layout)
{
QToolButton *button = new QToolButton;
button->setIcon(style()->standardIcon(icon));
- button->setIconSize(QSize(36,36));
+ button->setIconSize(QSize(36, 36));
button->setToolTip(tr(tip));
connect(button, SIGNAL(clicked()), dstobj, slot_method);
layout->addWidget(button);
@@ -143,7 +155,8 @@ inline QToolButton* MediaApp::initButton(
return button;
}
-void MediaApp::createUI(QBoxLayout *appLayout) {
+void MediaApp::createUI(QBoxLayout *appLayout)
+{
appLayout->addWidget(player);
posLabel = new QLabel();
@@ -164,19 +177,19 @@ void MediaApp::createUI(QBoxLayout *appLayout) {
btnLayout->addStretch();
openButton = initButton(
- QStyle::SP_DialogOpenButton, "Open File", this, SLOT(open()), btnLayout);
+ QStyle::SP_DialogOpenButton, "Open File", this, SLOT(open()), btnLayout);
playButton = initButton(
- QStyle::SP_MediaPlay, "Play", player, SLOT(play()), btnLayout);
+ QStyle::SP_MediaPlay, "Play", player, SLOT(play()), btnLayout);
pauseButton = initButton(
- QStyle::SP_MediaPause, "Pause", player, SLOT(pause()), btnLayout);
+ QStyle::SP_MediaPause, "Pause", player, SLOT(pause()), btnLayout);
stopButton = initButton(
- QStyle::SP_MediaStop, "Stop", player, SLOT(stop()), btnLayout);
+ QStyle::SP_MediaStop, "Stop", player, SLOT(stop()), btnLayout);
fullScreenButton = initButton(
- QStyle::SP_TitleBarMaxButton, "Fullscreen", this, SLOT(toggleFullScreen()), btnLayout);
+ QStyle::SP_TitleBarMaxButton, "Fullscreen", this, SLOT(toggleFullScreen()), btnLayout);
btnLayout->addStretch();
appLayout->addLayout(btnLayout);
diff --git a/examples/player/mediaapp.h b/examples/player/mediaapp.h
index 1ab236b..77e66a3 100644
--- a/examples/player/mediaapp.h
+++ b/examples/player/mediaapp.h
@@ -30,39 +30,42 @@ class QToolButton;
class QTimer;
QT_END_NAMESPACE
-class MediaApp : public QWidget {
+class MediaApp : public QWidget
+{
Q_OBJECT
- public:
- MediaApp(QWidget *parent = 0);
- ~MediaApp();
- void openFile(const QString &fileName);
+public:
+ MediaApp(QWidget *parent = 0);
+ ~MediaApp();
+ void openFile(const QString &fileName);
- private Q_SLOTS:
- void open();
- void toggleFullScreen();
- void stateChanged();
- void posChanged();
- void setPos(int);
- void hideControls(){ showControls(false); }
+private Q_SLOTS:
+ void open();
+ void toggleFullScreen();
+ void stateChanged();
+ void posChanged();
+ void setPos(int);
+ void hideControls() {
+ showControls(false);
+ }
- private:
- QToolButton *initButton(
- QStyle::StandardPixmap, const char* , QObject* , const char*, QLayout*);
- void createUI(QBoxLayout *);
- void showControls(bool);
- void mouseMoveEvent(QMouseEvent*);
+private:
+ QToolButton *initButton(
+ QStyle::StandardPixmap, const char* , QObject* , const char*, QLayout*);
+ void createUI(QBoxLayout *);
+ void showControls(bool);
+ void mouseMoveEvent(QMouseEvent*);
- QString baseDir;
- Player *player;
- QToolButton *openButton;
- QToolButton *fullScreenButton;
- QToolButton *playButton;
- QToolButton *pauseButton;
- QToolButton *stopButton;
- QSlider *posSlider;
- QLabel *posLabel;
- QTimer fsTimer;
+ QString baseDir;
+ Player *player;
+ QToolButton *openButton;
+ QToolButton *fullScreenButton;
+ QToolButton *playButton;
+ QToolButton *pauseButton;
+ QToolButton *stopButton;
+ QSlider *posSlider;
+ QLabel *posLabel;
+ QTimer fsTimer;
};
#endif
diff --git a/examples/player/player.cpp b/examples/player/player.cpp
index c8860fb..c6d766c 100644
--- a/examples/player/player.cpp
+++ b/examples/player/player.cpp
@@ -35,25 +35,27 @@
* a media file as the first command line argument and then constructs a pipeline
* that uses playbin2 to decode the stream. */
-Player::Player(QWidget *parent)
- : QGst::Ui::VideoWidget(parent) {
- connect(&m_positionTimer, SIGNAL(timeout()), this, SLOT(updatePosition()));
- setMouseTracking(true);
- }
+Player::Player(QWidget *parent)
+ : QGst::Ui::VideoWidget(parent)
+{
+ connect(&m_positionTimer, SIGNAL(timeout()), this, SLOT(updatePosition()));
+ setMouseTracking(true);
+}
-void Player::setUri(const QString & uri) {
+void Player::setUri(const QString & uri)
+{
QString realUri = uri;
- if(uri.indexOf("://") < 0){
- if(realUri[0] != '/'){
+ if (uri.indexOf("://") < 0) {
+ if (realUri[0] != '/') {
realUri = QDir::current().path() + "/" + realUri;
}
realUri = "file://" + realUri;
}
- if(!m_pipeline){
+ if (!m_pipeline) {
QGst::ElementPtr playbin = QGst::ElementFactory::make("playbin2");
- if(playbin) {
+ if (playbin) {
m_pipeline = playbin.dynamicCast<QGst::Pipeline>();
QGst::BusPtr bus = m_pipeline->bus();
QGlib::Signal::connect(bus, "message", this, &Player::busMessage);
@@ -61,49 +63,57 @@ void Player::setUri(const QString & uri) {
}
}
- if(m_pipeline){
+ if (m_pipeline) {
m_pipeline->setProperty("uri", realUri);
}
}
-Player::~Player() {
- if(m_pipeline)
+Player::~Player()
+{
+ if (m_pipeline) {
m_pipeline->setState(QGst::StateNull);
+ }
}
-void Player::updatePosition() {
+void Player::updatePosition()
+{
Q_EMIT positionChanged(position());
}
-void Player::play() {
- if(m_pipeline){
+void Player::play()
+{
+ if (m_pipeline) {
m_pipeline->setState(QGst::StatePlaying);
}
}
-void Player::pause( ){
- if(m_pipeline){
+void Player::pause()
+{
+ if (m_pipeline) {
m_pipeline->setState(QGst::StatePaused);
}
}
-void Player::stop() {
- if(m_pipeline){
+void Player::stop()
+{
+ if (m_pipeline) {
m_pipeline->setState(QGst::StateReady);
}
}
-void Player::setPosition(QTime pos) {
+void Player::setPosition(QTime pos)
+{
QGst::SeekEventPtr evt = QGst::SeekEvent::create(
- 1.0, QGst::FormatTime, QGst::SeekFlagFlush,
- QGst::SeekTypeSet, QGst::Clock::clockTimeFromTime(pos),
- QGst::SeekTypeNone, -1);
+ 1.0, QGst::FormatTime, QGst::SeekFlagFlush,
+ QGst::SeekTypeSet, QGst::Clock::clockTimeFromTime(pos),
+ QGst::SeekTypeNone, -1);
m_pipeline->sendEvent(evt);
}
-QTime Player::position() {
- if(m_pipeline){
+QTime Player::position()
+{
+ if (m_pipeline) {
QGst::PositionQueryPtr query = QGst::PositionQuery::create(QGst::FormatTime);
m_pipeline->query(query);
return QGst::Clock::timeFromClockTime(query->position());
@@ -111,8 +121,9 @@ QTime Player::position() {
return QTime(0, 0, 0, 0);
}
-QTime Player::length() {
- if(m_pipeline){
+QTime Player::length()
+{
+ if (m_pipeline) {
QGst::DurationQueryPtr query = QGst::DurationQuery::create(QGst::FormatTime);
m_pipeline->query(query);
return QGst::Clock::timeFromClockTime(query->duration());
@@ -120,68 +131,71 @@ QTime Player::length() {
return QTime(0, 0, 0, 0);
}
-QGst::State Player::state() {
+QGst::State Player::state()
+{
QGst::State state;
- if(!m_pipeline ||
- m_pipeline->getState(&state, NULL, 1e9) != QGst::StateChangeSuccess)
- {
+ if (!m_pipeline ||
+ m_pipeline->getState(&state, NULL, 1e9) != QGst::StateChangeSuccess) {
state = QGst::StateNull;
}
return state;
}
-void Player::handleStateChange(QGst::StateChangedMessagePtr scm) {
- switch(scm->newState()){
- case QGst::StatePlaying:
- Q_EMIT positionChanged(position());
- m_positionTimer.start(500);
- break;
- case QGst::StatePaused:
- if(scm->oldState() == QGst::StateReady){
- QGst::ElementPtr sink =
- m_pipeline->property("video-sink").get<QGst::ElementPtr>();
- if(sink){
- setVideoSink(sink);
- QGst::ChildProxyPtr proxy = sink.dynamicCast<QGst::ChildProxy>();
- if (proxy)
- proxy->childByIndex(0)->setProperty("force-aspect-ratio", true);
+void Player::handleStateChange(QGst::StateChangedMessagePtr scm)
+{
+ switch (scm->newState()) {
+ case QGst::StatePlaying:
+ Q_EMIT positionChanged(position());
+ m_positionTimer.start(500);
+ break;
+ case QGst::StatePaused:
+ if (scm->oldState() == QGst::StateReady) {
+ QGst::ElementPtr sink =
+ m_pipeline->property("video-sink").get<QGst::ElementPtr>();
+ if (sink) {
+ setVideoSink(sink);
+ QGst::ChildProxyPtr proxy = sink.dynamicCast<QGst::ChildProxy>();
+ if (proxy) {
+ proxy->childByIndex(0)->setProperty("force-aspect-ratio", true);
}
- } else
- m_positionTimer.stop();
- break;
- case QGst::StateReady:
- if(scm->oldState() == QGst::StatePaused)
- /* Remove the sink now to avoid inter-thread issues with Qt
- for the next time the pipeline goes to StatePlaying */
- setVideoSink(QGst::ElementPtr());
+ }
+ } else
+ m_positionTimer.stop();
+ break;
+ case QGst::StateReady:
+ if (scm->oldState() == QGst::StatePaused) {
+ /* Remove the sink now to avoid inter-thread issues with Qt
+ for the next time the pipeline goes to StatePlaying */
+ setVideoSink(QGst::ElementPtr());
+ }
break;
- default:
+ default:
break;
}
}
-void Player::busMessage(const QGst::MessagePtr & message) {
- switch(message->type()) {
- case QGst::MessageEos: //End of stream. We reached the end of the file.
- qDebug() << "got eos";
- m_pipeline->setState(QGst::StateReady);
- break;
- case QGst::MessageError: //Some error occurred.
- /*TODO: send a message to UI */
- break;
- case QGst::MessageStateChanged:
- if(QGlib::Type::fromInstance(message->source()).
- isA(QGlib::GetType<QGst::Pipeline>()))
- {
- QGst::StateChangedMessagePtr scm =
- message.dynamicCast<QGst::StateChangedMessage>();
- handleStateChange(scm);
- Q_EMIT stateChanged(scm->newState());
- }
+void Player::busMessage(const QGst::MessagePtr & message)
+{
+ switch (message->type()) {
+ case QGst::MessageEos: //End of stream. We reached the end of the file.
+ qDebug() << "got eos";
+ m_pipeline->setState(QGst::StateReady);
+ break;
+ case QGst::MessageError: //Some error occurred.
+ /*TODO: send a message to UI */
+ break;
+ case QGst::MessageStateChanged:
+ if (QGlib::Type::fromInstance(message->source()).
+ isA(QGlib::GetType<QGst::Pipeline>())) {
+ QGst::StateChangedMessagePtr scm =
+ message.dynamicCast<QGst::StateChangedMessage>();
+ handleStateChange(scm);
+ Q_EMIT stateChanged(scm->newState());
+ }
- break;
- default:
- break;
+ break;
+ default:
+ break;
}
}
diff --git a/examples/player/player.h b/examples/player/player.h
index ac442b0..8e680df 100644
--- a/examples/player/player.h
+++ b/examples/player/player.h
@@ -29,13 +29,13 @@
/* This is a simple example of a command-line player. It accepts the URI of
* an media file as the first command line argument and then constructs a pipeline
- * that uses playbin2 to decode the stream.
+ * that uses playbin2 to decode the stream.
* In the future this example will perhaps gain a simple GUI. */
class Player : public QGst::Ui::VideoWidget
{
Q_OBJECT;
- public:
+public:
Player(QWidget *parent = 0);
~Player();
@@ -46,7 +46,7 @@ class Player : public QGst::Ui::VideoWidget
/* Everything is compiled with QT_NO_KEYWORDS because otherwise Signals::emit wouldn't compile
* so use Q_SLOTS instead of "slots" */
- public Q_SLOTS:
+public Q_SLOTS:
void stop();
void play();
void pause();
@@ -57,7 +57,7 @@ Q_SIGNALS:
void positionChanged(QTime);
void stateChanged(QGst::State state);
- private:
+private:
void busMessage(const QGst::MessagePtr & message);
void elementAdded(const QGst::ElementPtr & element);
void handleStateChange(QGst::StateChangedMessagePtr);