| 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 | |
| 26 | #include <common/qgst_p.h> | 
| 27 | #include <common/qgst_bus_observer_p.h> | 
| 28 | #include <common/qgstpipeline_p.h> | 
| 29 | |
| 30 | #include <gst/app/gstappsink.h> | 
| 31 | |
| 32 | QT_BEGIN_NAMESPACE | 
| 33 | |
| 34 | class QGstreamerMessage; | 
| 35 | |
| 36 | class QGstreamerAudioDecoder final : public QPlatformAudioDecoder, public QGstreamerBusMessageFilter | 
| 37 | { | 
| 38 | Q_OBJECT | 
| 39 | |
| 40 | public: | 
| 41 | static QMaybe<QPlatformAudioDecoder *> create(QAudioDecoder *parent); | 
| 42 | ~QGstreamerAudioDecoder() override; | 
| 43 | |
| 44 | QUrl source() const override; | 
| 45 | void setSource(const QUrl &fileName) override; | 
| 46 | |
| 47 | QIODevice *sourceDevice() const override; | 
| 48 | void setSourceDevice(QIODevice *device) override; | 
| 49 | |
| 50 | void start() override; | 
| 51 | void stop() override; | 
| 52 | |
| 53 | QAudioFormat audioFormat() const override; | 
| 54 | void setAudioFormat(const QAudioFormat &format) override; | 
| 55 | |
| 56 | QAudioBuffer read() override; | 
| 57 | |
| 58 | qint64 position() const override; | 
| 59 | qint64 duration() const override; | 
| 60 | |
| 61 | // GStreamerBusMessageFilter interface | 
| 62 | bool processBusMessage(const QGstreamerMessage &message) override; | 
| 63 | |
| 64 | bool canReadQrc() const override; | 
| 65 | |
| 66 | private slots: | 
| 67 | void updateDuration(); | 
| 68 | |
| 69 | private: | 
| 70 | explicit QGstreamerAudioDecoder(QAudioDecoder *parent); | 
| 71 | |
| 72 | static GstFlowReturn new_sample(GstAppSink *sink, gpointer user_data); | 
| 73 | GstFlowReturn newSample(GstAppSink *sink); | 
| 74 | |
| 75 | void setAudioFlags(bool wantNativeAudio); | 
| 76 | void addAppSink(); | 
| 77 | void removeAppSink(); | 
| 78 | |
| 79 | void processInvalidMedia(QAudioDecoder::Error errorCode, const QString &errorString); | 
| 80 | static std::chrono::nanoseconds getPositionFromBuffer(GstBuffer *buffer); | 
| 81 | |
| 82 | bool processBusMessageError(const QGstreamerMessage &); | 
| 83 | bool processBusMessageDuration(const QGstreamerMessage &); | 
| 84 | bool processBusMessageWarning(const QGstreamerMessage &); | 
| 85 | bool processBusMessageInfo(const QGstreamerMessage &); | 
| 86 | bool processBusMessageEOS(const QGstreamerMessage &); | 
| 87 | bool processBusMessageStateChanged(const QGstreamerMessage &); | 
| 88 | bool processBusMessageStreamsSelected(const QGstreamerMessage &); | 
| 89 | |
| 90 | QGstPipeline m_playbin; | 
| 91 | QGstBin m_outputBin; | 
| 92 | QGstElement m_audioConvert; | 
| 93 | QGstAppSink m_appSink; | 
| 94 | |
| 95 | QUrl mSource; | 
| 96 | QIODevice *mDevice = nullptr; | 
| 97 | QAudioFormat mFormat; | 
| 98 | |
| 99 | int m_buffersAvailable = 0; | 
| 100 | |
| 101 | static constexpr auto invalidDuration = std::chrono::milliseconds{ -1 }; | 
| 102 | static constexpr auto invalidPosition = std::chrono::milliseconds{ -1 }; | 
| 103 | std::chrono::milliseconds m_position{ invalidPosition }; | 
| 104 | std::chrono::milliseconds m_duration{ invalidDuration }; | 
| 105 | |
| 106 | int m_durationQueries = 0; | 
| 107 | |
| 108 | qint32 m_currentSessionId{}; | 
| 109 | |
| 110 | QGObjectHandlerScopedConnection m_deepNotifySourceConnection; | 
| 111 | }; | 
| 112 | |
| 113 | QT_END_NAMESPACE | 
| 114 | |
| 115 | #endif // QGSTREAMERPLAYERSESSION_H | 
| 116 | 
