| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QGSTREAMERAUDIODECODERCONTROL_H |
| 5 | #define QGSTREAMERAUDIODECODERCONTROL_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtMultimedia/private/qmultimediautils_p.h> |
| 19 | #include <QtMultimedia/private/qplatformaudiodecoder_p.h> |
| 20 | #include <QtMultimedia/private/qtmultimediaglobal_p.h> |
| 21 | #include <QtMultimedia/qaudiodecoder.h> |
| 22 | #include <QtCore/qobject.h> |
| 23 | #include <QtCore/qmutex.h> |
| 24 | #include <QtCore/qurl.h> |
| 25 | #include <QtCore/private/qexpected_p.h> |
| 26 | |
| 27 | #include <common/qgst_p.h> |
| 28 | #include <common/qgst_bus_observer_p.h> |
| 29 | #include <common/qgstpipeline_p.h> |
| 30 | |
| 31 | #include <gst/app/gstappsink.h> |
| 32 | |
| 33 | QT_BEGIN_NAMESPACE |
| 34 | |
| 35 | class QGstreamerMessage; |
| 36 | |
| 37 | class QGstreamerAudioDecoder final : public QPlatformAudioDecoder, public QGstreamerBusMessageFilter |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | public: |
| 42 | static q23::expected<QPlatformAudioDecoder *, QString> create(QAudioDecoder *parent); |
| 43 | ~QGstreamerAudioDecoder() override; |
| 44 | |
| 45 | QUrl source() const override; |
| 46 | void setSource(const QUrl &fileName) override; |
| 47 | |
| 48 | QIODevice *sourceDevice() const override; |
| 49 | void setSourceDevice(QIODevice *device) override; |
| 50 | |
| 51 | void start() override; |
| 52 | void stop() override; |
| 53 | |
| 54 | QAudioFormat audioFormat() const override; |
| 55 | void setAudioFormat(const QAudioFormat &format) override; |
| 56 | |
| 57 | QAudioBuffer read() override; |
| 58 | |
| 59 | qint64 position() const override; |
| 60 | qint64 duration() const override; |
| 61 | |
| 62 | // GStreamerBusMessageFilter interface |
| 63 | bool processBusMessage(const QGstreamerMessage &message) override; |
| 64 | |
| 65 | bool canReadQrc() const override; |
| 66 | |
| 67 | private slots: |
| 68 | void updateDuration(); |
| 69 | |
| 70 | private: |
| 71 | explicit QGstreamerAudioDecoder(QAudioDecoder *parent); |
| 72 | |
| 73 | static GstFlowReturn new_sample(GstAppSink *sink, gpointer user_data); |
| 74 | GstFlowReturn newSample(GstAppSink *sink); |
| 75 | |
| 76 | void setAudioFlags(bool wantNativeAudio); |
| 77 | void addAppSink(); |
| 78 | void removeAppSink(); |
| 79 | |
| 80 | void processInvalidMedia(QAudioDecoder::Error errorCode, const QString &errorString); |
| 81 | static std::chrono::nanoseconds getPositionFromBuffer(GstBuffer *buffer); |
| 82 | |
| 83 | bool processBusMessageError(const QGstreamerMessage &); |
| 84 | bool processBusMessageDuration(const QGstreamerMessage &); |
| 85 | bool processBusMessageWarning(const QGstreamerMessage &); |
| 86 | bool processBusMessageInfo(const QGstreamerMessage &); |
| 87 | bool processBusMessageEOS(const QGstreamerMessage &); |
| 88 | bool processBusMessageStateChanged(const QGstreamerMessage &); |
| 89 | bool processBusMessageStreamsSelected(const QGstreamerMessage &); |
| 90 | |
| 91 | QGstPipeline m_playbin; |
| 92 | QGstBin m_outputBin; |
| 93 | QGstElement m_audioConvert; |
| 94 | QGstAppSink m_appSink; |
| 95 | |
| 96 | QUrl mSource; |
| 97 | QIODevice *mDevice = nullptr; |
| 98 | QAudioFormat mFormat; |
| 99 | |
| 100 | int m_buffersAvailable = 0; |
| 101 | |
| 102 | static constexpr auto invalidDuration = std::chrono::milliseconds{ -1 }; |
| 103 | static constexpr auto invalidPosition = std::chrono::milliseconds{ -1 }; |
| 104 | std::chrono::milliseconds m_position{ invalidPosition }; |
| 105 | std::chrono::milliseconds m_duration{ invalidDuration }; |
| 106 | |
| 107 | int m_durationQueries = 0; |
| 108 | |
| 109 | qint32 m_currentSessionId{}; |
| 110 | |
| 111 | QGObjectHandlerScopedConnection m_deepNotifySourceConnection; |
| 112 | }; |
| 113 | |
| 114 | QT_END_NAMESPACE |
| 115 | |
| 116 | #endif // QGSTREAMERPLAYERSESSION_H |
| 117 |
