qgstreamerplaybin.cpp 11.9 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

Val Doroshchuk's avatar
Val Doroshchuk committed
40 41
#include "qgstreamerplaybin.h"
#include "qgstreamerpipeline_p.h"
42
#include <private/qgstreamervideorenderer_p.h>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <private/qgstreamerplayersession_p.h>
#include <QtMultimedia/qmediametadata.h>

QT_BEGIN_NAMESPACE

typedef enum {
    GST_PLAY_FLAG_VIDEO         = 0x00000001,
    GST_PLAY_FLAG_AUDIO         = 0x00000002,
    GST_PLAY_FLAG_TEXT          = 0x00000004,
    GST_PLAY_FLAG_VIS           = 0x00000008,
    GST_PLAY_FLAG_SOFT_VOLUME   = 0x00000010,
    GST_PLAY_FLAG_NATIVE_AUDIO  = 0x00000020,
    GST_PLAY_FLAG_NATIVE_VIDEO  = 0x00000040,
    GST_PLAY_FLAG_DOWNLOAD      = 0x00000080,
    GST_PLAY_FLAG_BUFFERING     = 0x000000100
} GstPlayFlags;

Val Doroshchuk's avatar
Val Doroshchuk committed
60
class QGstreamerPlaybinPrivate : public QGstreamerPipelinePrivate
61
{
Val Doroshchuk's avatar
Val Doroshchuk committed
62
    Q_DECLARE_PUBLIC(QGstreamerPlaybin)
63

64
public:
Val Doroshchuk's avatar
Val Doroshchuk committed
65 66
    QGstreamerPlaybinPrivate(QGstreamerPlaybin *q) 
        : QGstreamerPipelinePrivate(q)
67 68 69
    {
    }

70 71
public Q_SLOTS:
    void streamsChanged();
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
72
    void updatePipeline();
Val Doroshchuk's avatar
Val Doroshchuk committed
73
    void mediaObjectChanged();
74 75

protected:
76 77 78
    QString videoSinkDesc;
    QString pendingVideoSinkDesc;

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    bool showText = false;
    bool pendingShowText = showText;
    QUrl textUri;
    QUrl pendingTextUri;
    QString textFont;
    QString pendingTextFont;
    int audioStreamsCount = 0;
    int pendingAudioStream = -1;
    int audioStream = -1;
    QList<QVariantMap> audioStreamProperties;
    int videoStreamsCount = 0;
    int pendingVideoStream = -1;
    int videoStream = -1;
    QList<QVariantMap> videoStreamProperties;
    int textStreamsCount = 0;
    int pendingTextStream = -1;
    int textStream = -1;
    QList<QVariantMap> textStreamProperties;
};

Val Doroshchuk's avatar
Val Doroshchuk committed
99
void QGstreamerPlaybinPrivate::mediaObjectChanged()
100
{
Val Doroshchuk's avatar
Val Doroshchuk committed
101 102 103 104 105 106 107
    Q_Q(QGstreamerPlaybin);

    if (!session)
        return;

    connect(session, &QGstreamerPlayerSession::streamsChanged,
        this, &QGstreamerPlaybinPrivate::streamsChanged);
108

Val Doroshchuk's avatar
Val Doroshchuk committed
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
    // Create pipeline when renderer is ready.
    connect(session, &QGstreamerPlayerSession::rendererIsReady,
        this, &QGstreamerPlaybinPrivate::updatePipeline);

    // In case if these values've been applied
    // before mediaObject is available.
    q->setShowText(pendingShowText);
    q->setTextUri(pendingTextUri);
    q->setTextFont(pendingTextFont);
    q->setAudioStream(pendingAudioStream);
    q->setVideoStream(pendingVideoStream);
    q->setTextStream(pendingTextStream);

    updatePipeline();
}

static GstElement *parseDesc(const QString &name)
{
    GError *error = NULL;
    GstElement *element = gst_parse_launch(name.toLatin1().constData(), &error);
    if (error) {
        g_printerr("ERROR: %s: %s\n", name.toLatin1().constData(), GST_STR_NULL(error->message));
        g_clear_error(&error);
    }

    return element;
}

void QGstreamerPlaybinPrivate::updatePipeline()
{
    Q_Q(QGstreamerPlaybin);

    auto renderer = session->renderer();
    if (!renderer || !renderer->isReady())
        return;

    if (!pendingVideoSinkDesc.isEmpty()) {
        GstElement *videoSink = parseDesc(pendingVideoSinkDesc);
        if (videoSink) {
            videoSinkDesc = pendingVideoSinkDesc;
            pendingVideoSinkDesc.clear();
            renderer->setVideoSink(videoSink);

            emit q->videoSinkChanged();
        }
    }
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
}

static QVariantMap getStreamProperties(GstElement *pipeline, const char *name, int i)
{
    QVariantMap streamProperties;
    GstTagList *tags = 0;
    g_signal_emit_by_name(G_OBJECT(pipeline), name, i, &tags);

#if GST_CHECK_VERSION(1,0,0)
    if (tags && GST_IS_TAG_LIST(tags)) {
#else
    if (tags && gst_is_tag_list(tags)) {
#endif
        gchar *languageCode = 0;
        if (gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &languageCode))
            streamProperties[QMediaMetaData::Language] = QString::fromUtf8(languageCode);

        g_free (languageCode);
        gst_tag_list_free(tags);
    }

    return streamProperties;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
179
void QGstreamerPlaybinPrivate::streamsChanged()
180
{
Val Doroshchuk's avatar
Val Doroshchuk committed
181
    Q_Q(QGstreamerPlaybin);
182

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
183
    GstElement *playbin = session ? session->playbin() : nullptr;
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
    if (!playbin)
        return;

    gint audioCount = 0;
    gint videoCount = 0;
    gint textCount = 0;

    g_object_get(G_OBJECT(playbin), "n-audio", &audioCount, NULL);
    g_object_get(G_OBJECT(playbin), "n-video", &videoCount, NULL);
    g_object_get(G_OBJECT(playbin), "n-text", &textCount, NULL);

    QList<QVariantMap> audioProperties, videoProperties, textProperties;
    for (int i = 0; i < audioCount; ++i)
        audioProperties.append(getStreamProperties(playbin, "get-audio-tags", i));

    for (int i = 0; i < videoCount; ++i)
        videoProperties.append(getStreamProperties(playbin, "get-video-tags", i));

    for (int i = 0; i < textCount; ++i)
        textProperties.append(getStreamProperties(playbin, "get-text-tags", i));

    bool audioPropertiesChanged = audioStreamProperties != audioProperties;
    if (audioPropertiesChanged)
        audioStreamProperties = audioProperties;

    bool videoPropertiesChanged = videoStreamProperties != videoProperties;
    if (videoPropertiesChanged)
        videoStreamProperties = videoProperties;

    bool textPropertiesChanged = textStreamProperties != textProperties;
    if (textPropertiesChanged)
        textStreamProperties = textProperties;

    if (audioStreamsCount != audioCount) {
        audioStreamsCount = audioCount;
        emit q->audioStreamsCountChanged();
    }

    if (audioPropertiesChanged)
        emit q->audioStreamPropertiesChanged();

    if (videoStreamsCount != videoCount) {
        videoStreamsCount = videoCount;
        emit q->videoStreamsCountChanged();
    }

    if (videoPropertiesChanged)
        emit q->videoStreamPropertiesChanged();

    if (textStreamsCount != textCount) {
        textStreamsCount = textCount;
        emit q->textStreamsCountChanged();
    }

    if (textPropertiesChanged)
        emit q->textStreamPropertiesChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
242 243
QGstreamerPlaybin::QGstreamerPlaybin(QMediaObject *media, QObject *parent)
    : QGstreamerPipeline(new QGstreamerPlaybinPrivate(this), media, parent)
244
{
Val Doroshchuk's avatar
Val Doroshchuk committed
245
    Q_D(QGstreamerPlaybin);
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
246

Val Doroshchuk's avatar
Val Doroshchuk committed
247 248
    connect((QGstreamerPipeline*)this, &QGstreamerPipeline::mediaObjectChanged,
        d, QOverload<>::of(&QGstreamerPlaybinPrivate::mediaObjectChanged));
Val Doroshchuk's avatar
Val Doroshchuk committed
249

Val Doroshchuk's avatar
Val Doroshchuk committed
250
    d->mediaObjectChanged();
251 252
}

Val Doroshchuk's avatar
Val Doroshchuk committed
253 254
QGstreamerPlaybin::QGstreamerPlaybin(QObject *parent)
    : QGstreamerPlaybin(nullptr, parent)
255 256 257
{
}

Val Doroshchuk's avatar
Val Doroshchuk committed
258
QGstreamerPlaybin::~QGstreamerPlaybin()
259 260 261
{
}

Val Doroshchuk's avatar
Val Doroshchuk committed
262
QString QGstreamerPlaybin::videoSink() const
263 264 265 266
{
    return d_func()->videoSinkDesc;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
267
void QGstreamerPlaybin::setVideoSink(const QString &desc)
268
{
Val Doroshchuk's avatar
Val Doroshchuk committed
269
    Q_D(QGstreamerPlaybin);
270 271

    d->pendingVideoSinkDesc = desc;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
272
    if (!d->session || d->videoSinkDesc == desc)
273 274
        return;

Val Doroshchuk's avatar
Val Doroshchuk committed
275
    d->updatePipeline();
276 277
}

Val Doroshchuk's avatar
Val Doroshchuk committed
278
bool QGstreamerPlaybin::showText() const
279 280 281 282
{
    return d_func()->showText;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
283
void QGstreamerPlaybin::setShowText(bool show)
284
{
Val Doroshchuk's avatar
Val Doroshchuk committed
285
    Q_D(QGstreamerPlaybin);
286 287

    d->pendingShowText = show;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
288 289
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->showText == show)
290 291 292 293
        return;

    d->showText = show;
    int flags = 0;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
294
    g_object_get(G_OBJECT(playbin), "flags", &flags, NULL);
295 296 297 298 299 300

    if (show)
        flags |= GST_PLAY_FLAG_TEXT;
    else
        flags &= ~GST_PLAY_FLAG_TEXT;

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
301
    g_object_set(G_OBJECT(playbin), "flags", flags, NULL);
302 303 304
    emit showTextChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
305
QUrl QGstreamerPlaybin::textUri() const
306 307 308 309
{
    return d_func()->textUri;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
310
void QGstreamerPlaybin::setTextUri(const QUrl &uri)
311
{
Val Doroshchuk's avatar
Val Doroshchuk committed
312
    Q_D(QGstreamerPlaybin);
313 314

    d->pendingTextUri = uri;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
315 316
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->textUri == uri)
317 318 319 320 321
        return;

    d->textUri = uri;
    setShowText(true);

Val Doroshchuk's avatar
test  
Val Doroshchuk committed
322
    g_object_set(G_OBJECT(playbin), "suburi", uri.toEncoded().constData(), NULL);
323 324 325
    emit textUriChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
326
QString QGstreamerPlaybin::textFont() const
327 328 329 330
{
    return d_func()->textFont;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
331
void QGstreamerPlaybin::setTextFont(const QString &str)
332
{
Val Doroshchuk's avatar
Val Doroshchuk committed
333
    Q_D(QGstreamerPlaybin);
334 335

    d->pendingTextFont = str;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
336 337
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->textFont == str)
338 339 340
        return;

    d->textFont = str;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
341
    g_object_set(G_OBJECT(playbin), "subtitle-font-desc", str.toUtf8().constData(), NULL);
342 343 344
    emit textFontChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
345
int QGstreamerPlaybin::audioStreamsCount() const
346 347 348 349
{
    return d_func()->audioStreamsCount;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
350
int QGstreamerPlaybin::audioStream() const
351 352 353 354
{
    return d_func()->audioStream;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
355
void QGstreamerPlaybin::setAudioStream(int i)
356
{
Val Doroshchuk's avatar
Val Doroshchuk committed
357
    Q_D(QGstreamerPlaybin);
358 359

    d->pendingAudioStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
360 361
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->audioStream == i)
362 363 364
        return;

    d->audioStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
365
    g_object_set(G_OBJECT(playbin), "current-audio", i, NULL);
366 367 368
    emit audioStreamChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
369
QVariantMap QGstreamerPlaybin::audioStreamProperties(int i)
370 371 372 373
{
    return d_func()->audioStreamProperties[i];
}

Val Doroshchuk's avatar
Val Doroshchuk committed
374
int QGstreamerPlaybin::videoStreamsCount() const
375 376 377 378
{
    return d_func()->videoStreamsCount;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
379
int QGstreamerPlaybin::videoStream() const
380 381 382 383
{
    return d_func()->videoStream;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
384
void QGstreamerPlaybin::setVideoStream(int i)
385
{
Val Doroshchuk's avatar
Val Doroshchuk committed
386
    Q_D(QGstreamerPlaybin);
387 388

    d->pendingVideoStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
389 390
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->videoStream == i)
391 392 393
        return;

    d->videoStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
394
    g_object_set(G_OBJECT(playbin), "current-video", i, NULL);
395 396 397
    emit videoStreamChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
398
QVariantMap QGstreamerPlaybin::videoStreamProperties(int i)
399 400 401 402
{
    return d_func()->videoStreamProperties[i];
}

Val Doroshchuk's avatar
Val Doroshchuk committed
403
int QGstreamerPlaybin::textStreamsCount() const
404 405 406 407
{
    return d_func()->textStreamsCount;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
408
int QGstreamerPlaybin::textStream() const
409 410 411 412
{
    return d_func()->textStream;
}

Val Doroshchuk's avatar
Val Doroshchuk committed
413
void QGstreamerPlaybin::setTextStream(int i)
414
{
Val Doroshchuk's avatar
Val Doroshchuk committed
415
    Q_D(QGstreamerPlaybin);
416 417

    d->pendingTextStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
418 419
    GstElement *playbin = d->session ? d->session->playbin() : nullptr;
    if (!playbin || d->textStream == i)
420 421 422
        return;

    d->textStream = i;
Val Doroshchuk's avatar
test  
Val Doroshchuk committed
423
    g_object_set(G_OBJECT(playbin), "current-text", i, NULL);
424 425 426
    emit textStreamChanged();
}

Val Doroshchuk's avatar
Val Doroshchuk committed
427
QVariantMap QGstreamerPlaybin::textStreamProperties(int i)
428 429 430 431
{
    return d_func()->textStreamProperties[i];
}

Val Doroshchuk's avatar
Val Doroshchuk committed
432
#include "moc_qgstreamerplaybin.cpp"
433 434

QT_END_NAMESPACE