| 1 | // Copyright (C) 2021 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 | #ifndef QFFMPEGDEMUXER_P_H | 
| 4 | #define QFFMPEGDEMUXER_P_H | 
| 5 |  | 
| 6 | // | 
| 7 | //  W A R N I N G | 
| 8 | //  ------------- | 
| 9 | // | 
| 10 | // This file is not part of the Qt API. It exists purely as an | 
| 11 | // implementation detail. This header file may change from version to | 
| 12 | // version without notice, or even be removed. | 
| 13 | // | 
| 14 | // We mean it. | 
| 15 | // | 
| 16 |  | 
| 17 | #include <QtFFmpegMediaPluginImpl/private/qffmpegplaybackengineobject_p.h> | 
| 18 | #include <QtFFmpegMediaPluginImpl/private/qffmpegpacket_p.h> | 
| 19 | #include <QtFFmpegMediaPluginImpl/private/qffmpegplaybackutils_p.h> | 
| 20 | #include <QtFFmpegMediaPluginImpl/private/qffmpegtime_p.h> | 
| 21 | #include <QtMultimedia/private/qplatformmediaplayer_p.h> | 
| 22 |  | 
| 23 | #include <unordered_map> | 
| 24 |  | 
| 25 | QT_BEGIN_NAMESPACE | 
| 26 |  | 
| 27 | namespace QFFmpeg { | 
| 28 |  | 
| 29 | class Demuxer : public PlaybackEngineObject | 
| 30 | { | 
| 31 |     Q_OBJECT | 
| 32 | public: | 
| 33 |     Demuxer(AVFormatContext *context, TrackPosition initialPosUs, bool seekPending, | 
| 34 |             const LoopOffset &loopOffset, const StreamIndexes &streamIndexes, int loops); | 
| 35 |  | 
| 36 |     using RequestingSignal = void (Demuxer::*)(Packet); | 
| 37 |     static RequestingSignal signalByTrackType(QPlatformMediaPlayer::TrackType trackType); | 
| 38 |  | 
| 39 |     void setLoops(int loopsCount); | 
| 40 |  | 
| 41 | public slots: | 
| 42 |     void onPacketProcessed(Packet); | 
| 43 |  | 
| 44 | signals: | 
| 45 |     void requestProcessAudioPacket(Packet); | 
| 46 |     void requestProcessVideoPacket(Packet); | 
| 47 |     void requestProcessSubtitlePacket(Packet); | 
| 48 |     void firstPacketFound(Id id, TrackPosition absSeekPos); | 
| 49 |     void packetsBuffered(); | 
| 50 |  | 
| 51 | protected: | 
| 52 |     std::chrono::milliseconds timerInterval() const override; | 
| 53 |  | 
| 54 | private: | 
| 55 |     bool canDoNextStep() const override; | 
| 56 |  | 
| 57 |     void doNextStep() override; | 
| 58 |  | 
| 59 |     void ensureSeeked(); | 
| 60 |  | 
| 61 | private: | 
| 62 |     struct StreamData | 
| 63 |     { | 
| 64 |         QPlatformMediaPlayer::TrackType trackType = QPlatformMediaPlayer::TrackType::NTrackTypes; | 
| 65 |         TrackDuration bufferedDuration = TrackDuration(0); | 
| 66 |         qint64 bufferedSize = 0; | 
| 67 |  | 
| 68 |         TrackPosition maxSentPacketsPos = TrackPosition(0); | 
| 69 |         TrackPosition maxProcessedPacketPos = TrackPosition(0); | 
| 70 |  | 
| 71 |         bool isDataLimitReached = false; | 
| 72 |     }; | 
| 73 |  | 
| 74 |     void updateStreamDataLimitFlag(StreamData &streamData); | 
| 75 |  | 
| 76 | private: | 
| 77 |     AVFormatContext *m_context = nullptr; | 
| 78 |     bool m_seeked = false; | 
| 79 |     bool m_firstPacketFound = false; | 
| 80 |     std::unordered_map<int, StreamData> m_streams; | 
| 81 |     TrackPosition m_posInLoopUs = TrackPosition(0); // Position in current loop in [0, duration()] | 
| 82 |     LoopOffset m_loopOffset; | 
| 83 |     TrackPosition m_maxPacketsEndPos = TrackPosition(0); | 
| 84 |     QAtomicInt m_loops = QMediaPlayer::Once; | 
| 85 |     bool m_buffered = false; | 
| 86 |     qsizetype m_demuxerRetryCount = 0; | 
| 87 |     static constexpr qsizetype s_maxDemuxerRetries = 10; // Arbitrarily chosen | 
| 88 |     static constexpr std::chrono::milliseconds s_demuxerRetryInterval = std::chrono::milliseconds(10); | 
| 89 | }; | 
| 90 |  | 
| 91 | } // namespace QFFmpeg | 
| 92 |  | 
| 93 | QT_END_NAMESPACE // QFFMPEGDEMUXER_P_H | 
| 94 |  | 
| 95 | #endif | 
| 96 |  |