| 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 QFFMPEGRENDERER_P_H | 
| 4 | #define QFFMPEGRENDERER_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/qffmpegtimecontroller_p.h> | 
| 19 | #include <QtFFmpegMediaPluginImpl/private/qffmpegframe_p.h> | 
| 20 |  | 
| 21 | #include <QtCore/qpointer.h> | 
| 22 | #include <QtCore/qqueue.h> | 
| 23 |  | 
| 24 | #include <chrono> | 
| 25 |  | 
| 26 | QT_BEGIN_NAMESPACE | 
| 27 |  | 
| 28 | namespace QFFmpeg { | 
| 29 |  | 
| 30 | class Renderer : public PlaybackEngineObject | 
| 31 | { | 
| 32 |     Q_OBJECT | 
| 33 | public: | 
| 34 |     using TimePoint = RealClock::time_point; | 
| 35 |  | 
| 36 |     Renderer(const TimeController &tc); | 
| 37 |  | 
| 38 |     void syncSoft(TimePoint tp, TrackPosition trackPos); | 
| 39 |  | 
| 40 |     TrackPosition seekPosition() const; | 
| 41 |  | 
| 42 |     TrackPosition lastPosition() const; | 
| 43 |  | 
| 44 |     void setPlaybackRate(float rate); | 
| 45 |  | 
| 46 |     void doForceStep(); | 
| 47 |  | 
| 48 |     bool isStepForced() const; | 
| 49 |  | 
| 50 |     void start(const TimeController &tc); | 
| 51 |  | 
| 52 | public slots: | 
| 53 |  | 
| 54 |     void onFinalFrameReceived(); | 
| 55 |  | 
| 56 |     void render(Frame); | 
| 57 |  | 
| 58 | signals: | 
| 59 |     void frameProcessed(Frame); | 
| 60 |  | 
| 61 |     void synchronized(Id id, TimePoint tp, TrackPosition pos); | 
| 62 |  | 
| 63 |     void forceStepDone(); | 
| 64 |  | 
| 65 |     void loopChanged(Id id, TrackPosition offset, int index); | 
| 66 |  | 
| 67 | protected: | 
| 68 |     bool setForceStepDone(); | 
| 69 |  | 
| 70 |     void onPauseChanged() override; | 
| 71 |  | 
| 72 |     bool canDoNextStep() const override; | 
| 73 |  | 
| 74 |     std::chrono::milliseconds timerInterval() const override; | 
| 75 |  | 
| 76 |     virtual void onPlaybackRateChanged() { } | 
| 77 |  | 
| 78 |     struct RenderingResult | 
| 79 |     { | 
| 80 |         bool done = true; | 
| 81 |         std::chrono::microseconds recheckInterval = std::chrono::microseconds(0); | 
| 82 |     }; | 
| 83 |  | 
| 84 |     virtual RenderingResult renderInternal(Frame frame) = 0; | 
| 85 |  | 
| 86 |     float playbackRate() const; | 
| 87 |  | 
| 88 |     std::chrono::microseconds frameDelay(const Frame &frame, | 
| 89 |                                          TimePoint timePoint = RealClock::now()) const; | 
| 90 |  | 
| 91 |     void changeRendererTime(std::chrono::microseconds offset); | 
| 92 |  | 
| 93 |     template<typename Output, typename ChangeHandler> | 
| 94 |     void setOutputInternal(QPointer<Output> &actual, Output *desired, ChangeHandler &&changeHandler) | 
| 95 |     { | 
| 96 |         const auto connectionType = | 
| 97 |                 thread()->isCurrentThread() ? Qt::AutoConnection : Qt::BlockingQueuedConnection; | 
| 98 |         auto doer = [desired, changeHandler, &actual]() { | 
| 99 |             const auto prev = std::exchange(actual, desired); | 
| 100 |             if (prev != desired) | 
| 101 |                 changeHandler(prev); | 
| 102 |         }; | 
| 103 |         QMetaObject::invokeMethod(this, doer, connectionType); | 
| 104 |     } | 
| 105 |  | 
| 106 | private: | 
| 107 |     void doNextStep() override; | 
| 108 |  | 
| 109 | private: | 
| 110 |     TimeController m_timeController; | 
| 111 |     TrackPosition m_lastFrameEnd = TrackPosition(0); | 
| 112 |     QAtomicInteger<qint64> m_lastPosition = 0; | 
| 113 |     QAtomicInteger<qint64> m_seekPos = 0; | 
| 114 |  | 
| 115 |     int m_loopIndex = 0; | 
| 116 |     QQueue<Frame> m_frames; | 
| 117 |  | 
| 118 |     QAtomicInteger<bool> m_isStepForced = false; | 
| 119 |     bool m_started = false; | 
| 120 |     std::optional<TimePoint> m_explicitNextFrameTime; | 
| 121 | }; | 
| 122 |  | 
| 123 | } // namespace QFFmpeg | 
| 124 |  | 
| 125 | QT_END_NAMESPACE | 
| 126 |  | 
| 127 | #endif // QFFMPEGRENDERER_P_H | 
| 128 |  |