2 Copyright (C) 2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QCoreApplication>
20 #include <QtCore/QFile>
21 #include <QGlib/Signal>
22 #include <QGst/Global>
23 #include <QGst/Pipeline>
24 #include <QGst/ElementFactory>
25 #include <QGst/GhostPad>
26 #include <QGst/Structure>
28 #include <QGst/Message>
30 /* This is a simple example of a command-line audio player. It accepts the filename of
31 * an audio file as the first command line argument and then constructs a pipeline
32 * that uses decodebin2 to decode the audio stream and autoaudiosink to output it to
33 * the sound card. In the future this example will be expanded to handle video as well
34 * and perhaps it will gain a simple GUI too. */
39 Player(const QString & fileName);
43 void onBusSyncMessage(const QGst::BusPtr & bus, const QGst::MessagePtr & message);
44 void onNewDecodedPad(const QGst::BinPtr & decodebin, QGst::PadPtr newPad, bool isLast);
45 static QGst::BinPtr createAudioSinkBin();
47 /* This smart pointer is needed to keep a reference to the underlying GstPipeline object.
48 * Even if we are not going to use it, we must keep a reference to the pipeline,
49 * so that it remains in memory, together with all its child elements. If this
50 * reference is gone, the pipeline and all the elements will be destroyed.
51 * Note that we don't need to keep references to the individual elements, because
52 * when they are added in the pipeline, the pipeline keeps a reference on them. */
53 QGst::PipelinePtr m_pipeline;
56 Player::Player(const QString & fileName)
58 m_pipeline = QGst::Pipeline::create();
60 QGst::ElementPtr filesrc = QGst::ElementFactory::make("filesrc");
61 QGst::ElementPtr decodebin = QGst::ElementFactory::make("decodebin2");
63 filesrc->setProperty("location", fileName);
64 QGlib::Signal::connect(decodebin, "new-decoded-pad", this, &Player::onNewDecodedPad);
66 m_pipeline->add(filesrc);
67 m_pipeline->add(decodebin);
68 filesrc->link(decodebin);
70 QGst::BusPtr bus = m_pipeline->bus();
71 bus->enableSyncMessageEmission();
72 QGlib::Signal::connect(bus, "sync-message", this, &Player::onBusSyncMessage);
74 m_pipeline->setState(QGst::StatePlaying);
79 m_pipeline->setState(QGst::StateNull);
81 /* When m_pipeline is destructed, the last reference to our pipeline will be gone,
82 * and with it all the elements, buses, etc will be destroyed too. As a result,
83 * there is no need to cleanup here. */
86 void Player::onBusSyncMessage(const QGst::BusPtr & bus, const QGst::MessagePtr & message)
88 /* WARNING this slot gets called in a different thread */
90 switch(message->type()) {
91 case QGst::MessageEos: //End of stream. We reached the end of the file.
92 case QGst::MessageError: //Some error occurred.
93 /* QCoreApplication::quit is safe to be called in another thread.
94 * It will schedule the main event loop to exit, when execution
95 * in the main thread has reached the event loop. */
96 QCoreApplication::quit();
103 /* This method will be called every time a new "src" pad is available on the decodebin2 element.
104 * Here we have to check what kind of data this pad transfers (usually it is either "audio/x-raw-*"
105 * or "video/x-raw-*") and connect an appropriate sink that can handle this type of data. */
106 void Player::onNewDecodedPad(const QGst::BinPtr & decodebin, QGst::PadPtr newPad, bool isLast)
111 QGst::CapsPtr caps = newPad->caps();
112 QGst::SharedStructure structure = caps->structure(0);
114 /* The caps' first structure's name tells us what kind of data the pad transfers.
115 * Here we want to handle either audio/x-raw-int or audio/x-raw-float. Both types
116 * can be handled by the audioconvert element that is contained in the audioSinkBin,
117 * so there is no need to handle them separately */
118 if (structure.name().contains("audio/x-raw")) {
119 QGst::BinPtr audioSinkBin = createAudioSinkBin();
120 m_pipeline->add(audioSinkBin);
122 /* The newly created bin must go to the playing state in order to function.
123 * Here we tell it to synchronise its state with its parent, the pipeline,
124 * which is scheduled to go to the playing state. */
125 audioSinkBin->syncStateWithParent();
127 newPad->link(audioSinkBin->getStaticPad("sink"));
131 QGst::BinPtr Player::createAudioSinkBin()
133 QGst::BinPtr bin = QGst::Bin::create();
135 QGst::ElementPtr audioconvert = QGst::ElementFactory::make("audioconvert");
136 QGst::ElementPtr audiosink = QGst::ElementFactory::make("autoaudiosink");
138 bin->add(audioconvert);
140 audioconvert->link(audiosink);
142 /* Add a sink pad to the bin that proxies the sink pad of the audioconvert element */
143 bin->addPad(QGst::GhostPad::create(audioconvert->getStaticPad("sink"), "sink"));
148 static void sighandler(int code)
151 QCoreApplication::quit();
154 int main(int argc, char **argv)
156 QCoreApplication app(argc, argv);
157 QGst::init(&argc, &argv);
161 fileName = QFile::decodeName(argv[1]);
164 if (!QFile::exists(fileName)) {
165 std::cerr << "Usage: " << argv[0] << " fileToPlay" << std::endl;
169 Player *p = new Player(fileName);
171 signal(SIGINT, sighandler);
172 int result = app.exec();
174 delete p; // we must delete all gstreamer objects before calling QGst::deinit()