1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QGSTREAMERPLAYERSESSION_H
41 #define QGSTREAMERPLAYERSESSION_H
42 
43 #include <QtMultimedia/private/qtmultimediaglobal_p.h>
44 #include <QObject>
45 #include <QtCore/qmutex.h>
46 #include "qgstreameraudiodecodercontrol.h"
47 #include <private/qgstreamerbushelper_p.h>
48 #include "qaudiodecoder.h"
49 
50 #if QT_CONFIG(gstreamer_app)
51 #include <private/qgstappsrc_p.h>
52 #endif
53 
54 #include <gst/gst.h>
55 #include <gst/app/gstappsink.h>
56 
57 QT_BEGIN_NAMESPACE
58 
59 class QGstreamerBusHelper;
60 class QGstreamerMessage;
61 
62 class QGstreamerAudioDecoderSession : public QObject,
63                                 public QGstreamerBusMessageFilter
64 {
65 Q_OBJECT
66 Q_INTERFACES(QGstreamerBusMessageFilter)
67 
68 public:
69     QGstreamerAudioDecoderSession(QObject *parent);
70     virtual ~QGstreamerAudioDecoderSession();
71 
bus()72     QGstreamerBusHelper *bus() const { return m_busHelper; }
73 
state()74     QAudioDecoder::State state() const { return m_state; }
pendingState()75     QAudioDecoder::State pendingState() const { return m_pendingState; }
76 
77     bool processBusMessage(const QGstreamerMessage &message) override;
78 
79 #if QT_CONFIG(gstreamer_app)
appsrc()80     QGstAppSrc *appsrc() const { return m_appSrc; }
81     static void configureAppSrcElement(GObject*, GObject*, GParamSpec*,QGstreamerAudioDecoderSession* _this);
82 #endif
83 
84     QString sourceFilename() const;
85     void setSourceFilename(const QString &fileName);
86 
87     QIODevice* sourceDevice() const;
88     void setSourceDevice(QIODevice *device);
89 
90     void start();
91     void stop();
92 
93     QAudioFormat audioFormat() const;
94     void setAudioFormat(const QAudioFormat &format);
95 
96     QAudioBuffer read();
97     bool bufferAvailable() const;
98 
99     qint64 position() const;
100     qint64 duration() const;
101 
102     static GstFlowReturn new_sample(GstAppSink *sink, gpointer user_data);
103 
104 signals:
105     void stateChanged(QAudioDecoder::State newState);
106     void formatChanged(const QAudioFormat &format);
107     void sourceChanged();
108 
109     void error(int error, const QString &errorString);
110 
111     void bufferReady();
112     void bufferAvailableChanged(bool available);
113     void finished();
114 
115     void positionChanged(qint64 position);
116     void durationChanged(qint64 duration);
117 
118 private slots:
119     void updateDuration();
120 
121 private:
122     void setAudioFlags(bool wantNativeAudio);
123     void addAppSink();
124     void removeAppSink();
125 
126     void processInvalidMedia(QAudioDecoder::Error errorCode, const QString& errorString);
127     static qint64 getPositionFromBuffer(GstBuffer* buffer);
128 
129     QAudioDecoder::State m_state;
130     QAudioDecoder::State m_pendingState;
131     QGstreamerBusHelper *m_busHelper;
132     GstBus *m_bus;
133     GstElement *m_playbin;
134     GstElement *m_outputBin;
135     GstElement *m_audioConvert;
136     GstAppSink *m_appSink;
137 
138 #if QT_CONFIG(gstreamer_app)
139     QGstAppSrc *m_appSrc;
140 #endif
141 
142     QString mSource;
143     QIODevice *mDevice; // QWeakPointer perhaps
144     QAudioFormat mFormat;
145 
146     mutable QMutex m_buffersMutex;
147     int m_buffersAvailable;
148 
149     qint64 m_position;
150     qint64 m_duration;
151 
152     int m_durationQueries;
153 };
154 
155 QT_END_NAMESPACE
156 
157 #endif // QGSTREAMERPLAYERSESSION_H
158