Add a new "player" example: a simple command-line audio player.
[qtgstreamer:qtgstreamer.git] / examples / player / main.cpp
1 /*
2     Copyright (C) 2010  George Kiagiadakis <kiagiadakis.george@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #include <iostream>
18 #include <signal.h>
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>
27 #include <QGst/Bus>
28 #include <QGst/Message>
29
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. */
35
36 class Player
37 {
38 public:
39     Player(const QString & fileName);
40     ~Player();
41
42 private:
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();
46
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;
54 };
55
56 Player::Player(const QString & fileName)
57 {
58     m_pipeline = QGst::Pipeline::create();
59
60     QGst::ElementPtr filesrc = QGst::ElementFactory::make("filesrc");
61     QGst::ElementPtr decodebin = QGst::ElementFactory::make("decodebin2");
62
63     filesrc->setProperty("location", fileName);
64     QGlib::Signal::connect(decodebin, "new-decoded-pad", this, &Player::onNewDecodedPad);
65
66     m_pipeline->add(filesrc);
67     m_pipeline->add(decodebin);
68     filesrc->link(decodebin);
69
70     QGst::BusPtr bus = m_pipeline->bus();
71     bus->enableSyncMessageEmission();
72     QGlib::Signal::connect(bus, "sync-message", this, &Player::onBusSyncMessage);
73
74     m_pipeline->setState(QGst::StatePlaying);
75 }
76
77 Player::~Player()
78 {
79     m_pipeline->setState(QGst::StateNull);
80
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. */
84 }
85
86 void Player::onBusSyncMessage(const QGst::BusPtr & bus, const QGst::MessagePtr & message)
87 {
88     /* WARNING this slot gets called in a different thread */
89     Q_UNUSED(bus);
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();
97         break;
98     default:
99         break;
100     }
101 }
102
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)
107 {
108     Q_UNUSED(decodebin);
109     Q_UNUSED(isLast);
110
111     QGst::CapsPtr caps = newPad->caps();
112     QGst::SharedStructure structure = caps->structure(0);
113
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);
121
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();
126
127         newPad->link(audioSinkBin->getStaticPad("sink"));
128     }
129 }
130
131 QGst::BinPtr Player::createAudioSinkBin()
132 {
133     QGst::BinPtr bin = QGst::Bin::create();
134
135     QGst::ElementPtr audioconvert = QGst::ElementFactory::make("audioconvert");
136     QGst::ElementPtr audiosink = QGst::ElementFactory::make("autoaudiosink");
137
138     bin->add(audioconvert);
139     bin->add(audiosink);
140     audioconvert->link(audiosink);
141
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"));
144
145     return bin;
146 }
147
148 static void sighandler(int code)
149 {
150     Q_UNUSED(code);
151     QCoreApplication::quit();
152 }
153
154 int main(int argc, char **argv)
155 {
156     QCoreApplication app(argc, argv);
157     QGst::init(&argc, &argv);
158
159     QString fileName;
160     if (argc > 1) {
161         fileName = QFile::decodeName(argv[1]);
162     }
163
164     if (!QFile::exists(fileName)) {
165         std::cerr << "Usage: " << argv[0] << " fileToPlay" << std::endl;
166         return 1;
167     }
168
169     Player *p = new Player(fileName);
170
171     signal(SIGINT, sighandler);
172     int result = app.exec();
173
174     delete p; // we must delete all gstreamer objects before calling QGst::deinit()
175     QGst::deinit();
176
177     return result;
178 }