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 QFFMPEGTIMECONTROLLER_P_H |
4 | #define QFFMPEGTIMECONTROLLER_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 "qglobal.h" |
18 | |
19 | #include <chrono> |
20 | #include <optional> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | namespace QFFmpeg { |
25 | |
26 | class TimeController |
27 | { |
28 | using TrackTime = std::chrono::microseconds; |
29 | |
30 | public: |
31 | using Clock = std::chrono::steady_clock; |
32 | using TimePoint = Clock::time_point; |
33 | using PlaybackRate = float; |
34 | |
35 | TimeController(); |
36 | |
37 | PlaybackRate playbackRate() const; |
38 | |
39 | void setPlaybackRate(PlaybackRate playbackRate); |
40 | |
41 | void sync(qint64 trackPos = 0); |
42 | |
43 | void sync(const TimePoint &tp, qint64 pos); |
44 | |
45 | void syncSoft(const TimePoint &tp, qint64 pos, |
46 | const Clock::duration &fixingTime = std::chrono::seconds(4)); |
47 | |
48 | qint64 currentPosition(const Clock::duration &offset = Clock::duration{ 0 }) const; |
49 | |
50 | void setPaused(bool paused); |
51 | |
52 | qint64 positionFromTime(TimePoint tp, bool ignorePause = false) const; |
53 | |
54 | TimePoint timeFromPosition(qint64 pos, bool ignorePause = false) const; |
55 | |
56 | private: |
57 | struct SoftSyncData |
58 | { |
59 | TimePoint srcTimePoint; |
60 | TrackTime srcPosition; |
61 | TimePoint dstTimePoint; |
62 | TrackTime srcPosOffest; |
63 | TrackTime dstPosition; |
64 | PlaybackRate internalRate = 1; |
65 | }; |
66 | |
67 | SoftSyncData makeSoftSyncData(const TimePoint &srcTp, const TrackTime &srcPos, |
68 | const TimePoint &dstTp) const; |
69 | |
70 | TrackTime positionFromTimeInternal(const TimePoint &tp) const; |
71 | |
72 | TimePoint timeFromPositionInternal(const TrackTime &pos) const; |
73 | |
74 | void scrollTimeTillNow(); |
75 | |
76 | template<typename T> |
77 | static Clock::duration toClockTime(const T &t); |
78 | |
79 | template<typename T> |
80 | static TrackTime toTrackTime(const T &t); |
81 | |
82 | private: |
83 | bool m_paused = true; |
84 | PlaybackRate m_playbackRate = 1; |
85 | TrackTime m_position; |
86 | TimePoint m_timePoint = {}; |
87 | std::optional<SoftSyncData> m_softSyncData; |
88 | }; |
89 | |
90 | } // namespace QFFmpeg |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif // QFFMPEGTIMECONTROLLER_P_H |
95 | |