File: main.cpp

package info (click to toggle)
qt-gstreamer 0.10.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 2,016 kB
  • sloc: cpp: 17,352; yacc: 156; lex: 125; makefile: 23
file content (290 lines) | stat: -rw-r--r-- 11,268 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
    Copyright (C) 2011 Collabora Ltd.
      @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
    In this example, the pipeline consists of two parts, one for the audio session
    and one for the video session. Each session part is identical to the other in
    structure, the only difference being the encoder/decoder elements and the
    sources/sinks of the audio/video content.

    Here is a figure that shows the video session part of the pipeline.

.------------.   .-------.    .-------.      .----------.     .-------.
|videotestsrc|   |h264enc|    |h264pay|      |          |     |udpsink|      RTP SEND
|          src->sink    src->sink   src->send_rtp send_rtp->sink      |
'------------'   '-------'    '-------'      |          |     '-------'
                                             | rtpbin   |
                              .-------.      |          |     .---------.   .-------.   .-------------.
                RTP RECV      |udpsrc |      |          |     |h264depay|   |h264dec|   |autovideosink|
                              |      src->recv_rtp recv_rtp->sink     src->sink   src->sink           |
                              '-------'      |          |     '---------'   '-------'   '-------------'
                                             |          |
                                             |          |     .-------.
                                             |          |     |udpsink|  RTCP
                                             |    send_rtcp->sink     | sync=false
                              .-------.      |          |     '-------' async=false
                    RTCP      |udpsrc |      |          |
                              |     src->recv_rtcp      |
                              '-------'      |          |
                                             '----------'

    In the two sessions, the gstrtpbin element is common and the sessions are
    distinguished from the number at the end of rtpbin's pad names.
*/

#include "ui_voip.h"

#include <QtGui/QApplication>
#include <QtGui/QDialog>

#include <QGlib/Connect>
#include <QGlib/Error>
#include <QGst/Init>
#include <QGst/Pipeline>
#include <QGst/ElementFactory>
#include <QGst/Pad>
#include <QGst/Structure>
#include <QGst/Bus>
#include <QGst/Message>

class VoipExample : public QDialog
{
    Q_OBJECT
public:
    explicit VoipExample(QWidget *parent = 0);
    virtual ~VoipExample();

private Q_SLOTS:
    void on_startCallButton_clicked();
    void on_endCallButton_clicked();

    //keep the rtcp ports in sync with the rtp ports on the UI
    void on_audioRtpSrcSpinBox_valueChanged(int value) { m_ui.audioRtcpSrcSpinBox->setValue(value + 1); }
    void on_audioRtpDestSpinBox_valueChanged(int value) { m_ui.audioRtcpDestSpinBox->setValue(value + 1); }
    void on_videoRtpSrcSpinBox_valueChanged(int value) { m_ui.videoRtcpSrcSpinBox->setValue(value + 1); }
    void on_videoRtpDestSpinBox_valueChanged(int value) { m_ui.videoRtcpDestSpinBox->setValue(value + 1); }

private:
    void onRtpBinPadAdded(const QGst::PadPtr & pad);
    void onBusErrorMessage(const QGst::MessagePtr & msg);

private:
    Ui::VoipExample m_ui;
    QGst::PipelinePtr m_pipeline;
};

VoipExample::VoipExample(QWidget *parent)
    : QDialog(parent)
{
    m_ui.setupUi(this);
}

VoipExample::~VoipExample()
{
    //cleanup if the pipeline is still active
    if (m_pipeline) {
        on_endCallButton_clicked();
    }
}

void VoipExample::on_startCallButton_clicked()
{
    m_pipeline = QGst::Pipeline::create();

    QGst::ElementPtr rtpbin = QGst::ElementFactory::make("gstrtpbin");
    if (!rtpbin) {
        qFatal("Failed to create gstrtpbin");
    }
    m_pipeline->add(rtpbin);

    //audio content
    if (m_ui.audioGroupBox->isChecked()) {
        //sending side
        QGst::ElementPtr audiosrc;
        try {
            audiosrc = QGst::Bin::fromDescription(
                "autoaudiosrc ! queue ! audioconvert ! audiorate ! audio/x-raw-int,rate=8000 "
                "! speexenc ! rtpspeexpay"
            );
        } catch (const QGlib::Error & error) {
            qCritical() << error;
            qFatal("One ore more required elements are missing. Aborting...");
        }
        m_pipeline->add(audiosrc);
        audiosrc->link(rtpbin, "send_rtp_sink_1");

        QGst::ElementPtr rtpudpsink = QGst::ElementFactory::make("udpsink");
        if (!rtpudpsink) {
            qFatal("Failed to create udpsink. Aborting...");
        }
        rtpudpsink->setProperty("host", m_ui.remoteHostLineEdit->text());
        rtpudpsink->setProperty("port", m_ui.audioRtpDestSpinBox->value());
        m_pipeline->add(rtpudpsink);
        rtpbin->link("send_rtp_src_1", rtpudpsink);

        //receiving side
        QGst::ElementPtr rtpudpsrc = QGst::ElementFactory::make("udpsrc");
        if (!rtpudpsrc) {
            qFatal("Failed to create udpsrc. Aborting...");
        }
        rtpudpsrc->setProperty("port", m_ui.audioRtpSrcSpinBox->value());
        rtpudpsrc->setProperty("caps", QGst::Caps::fromString("application/x-rtp,"
                                                              "media=(string)audio,"
                                                              "clock-rate=(int)8000,"
                                                              "encoding-name=(string)SPEEX"));
        m_pipeline->add(rtpudpsrc);
        rtpudpsrc->link(rtpbin, "recv_rtp_sink_1");

        //recv sink will be added when the pad becomes available

        //rtcp connection
        QGst::ElementPtr rtcpudpsrc = QGst::ElementFactory::make("udpsrc");
        rtcpudpsrc->setProperty("port", m_ui.audioRtcpSrcSpinBox->value());
        m_pipeline->add(rtcpudpsrc);
        rtcpudpsrc->link(rtpbin, "recv_rtcp_sink_1");

        QGst::ElementPtr rtcpudpsink = QGst::ElementFactory::make("udpsink");
        rtcpudpsink->setProperty("host", m_ui.remoteHostLineEdit->text());
        rtcpudpsink->setProperty("port", m_ui.audioRtcpDestSpinBox->value());
        rtcpudpsink->setProperty("sync", false);
        rtcpudpsink->setProperty("async", false);
        m_pipeline->add(rtcpudpsink);
        rtpbin->link("send_rtcp_src_1", rtcpudpsink);
    }

    //video content
    if (m_ui.videoGroupBox->isChecked()) {
        //sending side
        QGst::ElementPtr videosrc;
        try {
            videosrc = QGst::Bin::fromDescription(
                "videotestsrc is-live=true ! video/x-raw-yuv,width=320,height=240,framerate=15/1 "
                "! x264enc tune=zerolatency byte-stream=true bitrate=300 ! rtph264pay"
            );
        } catch (const QGlib::Error & error) {
            qCritical() << error;
            qFatal("One ore more required elements are missing. Aborting...");
        }
        m_pipeline->add(videosrc);
        videosrc->link(rtpbin, "send_rtp_sink_2");

        QGst::ElementPtr rtpudpsink = QGst::ElementFactory::make("udpsink");
        if (!rtpudpsink) {
            qFatal("Failed to create udpsink. Aborting...");
        }
        rtpudpsink->setProperty("host", m_ui.remoteHostLineEdit->text());
        rtpudpsink->setProperty("port", m_ui.videoRtpDestSpinBox->value());
        m_pipeline->add(rtpudpsink);
        rtpbin->link("send_rtp_src_2", rtpudpsink);

        //receiving side
        QGst::ElementPtr rtpudpsrc = QGst::ElementFactory::make("udpsrc");
        if (!rtpudpsrc) {
            qFatal("Failed to create udpsrc. Aborting...");
        }
        rtpudpsrc->setProperty("port", m_ui.videoRtpSrcSpinBox->value());
        rtpudpsrc->setProperty("caps", QGst::Caps::fromString("application/x-rtp,"
                                                              "media=(string)video,"
                                                              "clock-rate=(int)90000,"
                                                              "encoding-name=(string)H264"));
        m_pipeline->add(rtpudpsrc);
        rtpudpsrc->link(rtpbin, "recv_rtp_sink_2");

        //recv sink will be added when the pad becomes available

        //rtcp connection
        QGst::ElementPtr rtcpudpsrc = QGst::ElementFactory::make("udpsrc");
        rtcpudpsrc->setProperty("port", m_ui.videoRtcpSrcSpinBox->value());
        m_pipeline->add(rtcpudpsrc);
        rtcpudpsrc->link(rtpbin, "recv_rtcp_sink_2");

        QGst::ElementPtr rtcpudpsink = QGst::ElementFactory::make("udpsink");
        rtcpudpsink->setProperty("host", m_ui.remoteHostLineEdit->text());
        rtcpudpsink->setProperty("port", m_ui.videoRtcpDestSpinBox->value());
        rtcpudpsink->setProperty("sync", false);
        rtcpudpsink->setProperty("async", false);
        m_pipeline->add(rtcpudpsink);
        rtpbin->link("send_rtcp_src_2", rtcpudpsink);
    }

    //watch for the receiving side src pads
    QGlib::connect(rtpbin, "pad-added", this, &VoipExample::onRtpBinPadAdded);

    //watch the bus
    m_pipeline->bus()->addSignalWatch();
    QGlib::connect(m_pipeline->bus(), "message::error", this, &VoipExample::onBusErrorMessage);

    //switch to the video view and connect the video widget
    m_ui.stackedWidget->setCurrentIndex(1);
    m_ui.videoWidget->watchPipeline(m_pipeline);

    //go!
    m_pipeline->setState(QGst::StatePlaying);
}

void VoipExample::onRtpBinPadAdded(const QGst::PadPtr & pad)
{
    QGst::ElementPtr bin;

    try {
        if (pad->caps()->internalStructure(0)->value("media").toString() == QLatin1String("audio")) {
            bin = QGst::Bin::fromDescription(
                "rtpspeexdepay ! speexdec ! audioconvert ! autoaudiosink"
            );
        } else {
            bin = QGst::Bin::fromDescription(
                "rtph264depay ! ffdec_h264 ! ffmpegcolorspace ! autovideosink"
            );
        }
    } catch (const QGlib::Error & error) {
        qCritical() << error;
        qFatal("One ore more required elements are missing. Aborting...");
    }

    m_pipeline->add(bin);
    bin->syncStateWithParent();
    pad->link(bin->getStaticPad("sink"));
}

void VoipExample::onBusErrorMessage(const QGst::MessagePtr & msg)
{
    qCritical() << "Error from bus:" << msg.staticCast<QGst::ErrorMessage>()->error();
    on_endCallButton_clicked(); //stop the call
}

void VoipExample::on_endCallButton_clicked()
{
    m_pipeline->setState(QGst::StateNull);
    m_ui.videoWidget->stopPipelineWatch();
    m_ui.stackedWidget->setCurrentIndex(0);
    m_pipeline.clear();
}


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QGst::init(&argc, &argv);

    VoipExample gui;
    gui.show();

    return app.exec();
}

#include "main.moc"