| 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 |  | 
| 4 | #include "playbackengine/qffmpegtimecontroller_p.h" | 
| 5 |  | 
| 6 | #include "qglobal.h" | 
| 7 | #include "qdebug.h" | 
| 8 |  | 
| 9 | #include <algorithm> | 
| 10 |  | 
| 11 | QT_BEGIN_NAMESPACE | 
| 12 |  | 
| 13 | namespace QFFmpeg { | 
| 14 |  | 
| 15 | TimeController::TimeController() | 
| 16 | { | 
| 17 |     sync(); | 
| 18 | } | 
| 19 |  | 
| 20 | TimeController::PlaybackRate TimeController::playbackRate() const | 
| 21 | { | 
| 22 |     return m_playbackRate; | 
| 23 | } | 
| 24 |  | 
| 25 | void TimeController::setPlaybackRate(PlaybackRate playbackRate) | 
| 26 | { | 
| 27 |     if (playbackRate == m_playbackRate) | 
| 28 |         return; | 
| 29 |  | 
| 30 |     Q_ASSERT(playbackRate > 0.f); | 
| 31 |  | 
| 32 |     scrollTimeTillNow(); | 
| 33 |     m_playbackRate = playbackRate; | 
| 34 |  | 
| 35 |     if (m_softSyncData) | 
| 36 |         m_softSyncData = makeSoftSyncData(srcTp: m_timePoint, srcPos: m_position, dstTp: m_softSyncData->dstTimePoint); | 
| 37 | } | 
| 38 |  | 
| 39 | void TimeController::sync(TrackPosition trackPos) | 
| 40 | { | 
| 41 |     sync(tp: RealClock::now(), pos: trackPos); | 
| 42 | } | 
| 43 |  | 
| 44 | void TimeController::sync(TimePoint tp, TrackPosition pos) | 
| 45 | { | 
| 46 |     m_softSyncData.reset(); | 
| 47 |     m_position = pos; | 
| 48 |     m_timePoint = tp; | 
| 49 | } | 
| 50 |  | 
| 51 | void TimeController::syncSoft(TimePoint tp, TrackPosition pos, RealClock::duration fixingTime) | 
| 52 | { | 
| 53 |     const auto srcTime = RealClock::now(); | 
| 54 |     const auto srcPos = positionFromTime(tp: srcTime, ignorePause: true); | 
| 55 |     const auto dstTime = srcTime + fixingTime; | 
| 56 |  | 
| 57 |     m_position = pos; | 
| 58 |     m_timePoint = tp; | 
| 59 |  | 
| 60 |     m_softSyncData = makeSoftSyncData(srcTp: srcTime, srcPos, dstTp: dstTime); | 
| 61 | } | 
| 62 |  | 
| 63 | TrackPosition TimeController::currentPosition(RealClock::duration offset) const | 
| 64 | { | 
| 65 |     return positionFromTime(tp: RealClock::now() + offset); | 
| 66 | } | 
| 67 |  | 
| 68 | void TimeController::setPaused(bool paused) | 
| 69 | { | 
| 70 |     if (m_paused == paused) | 
| 71 |         return; | 
| 72 |  | 
| 73 |     scrollTimeTillNow(); | 
| 74 |     m_paused = paused; | 
| 75 | } | 
| 76 |  | 
| 77 | TrackPosition TimeController::positionFromTime(TimePoint tp, bool ignorePause) const | 
| 78 | { | 
| 79 |     tp = m_paused && !ignorePause ? m_timePoint : tp; | 
| 80 |  | 
| 81 |     if (m_softSyncData && tp < m_softSyncData->dstTimePoint) { | 
| 82 |         const PlaybackRate rate = | 
| 83 |                 tp > m_softSyncData->srcTimePoint ? m_softSyncData->internalRate : m_playbackRate; | 
| 84 |  | 
| 85 |         return m_softSyncData->srcPosition | 
| 86 |                 + toTrackDuration(clockDuration: tp - m_softSyncData->srcTimePoint, rate); | 
| 87 |     } | 
| 88 |  | 
| 89 |     return positionFromTimeInternal(tp); | 
| 90 | } | 
| 91 |  | 
| 92 | TimeController::TimePoint TimeController::timeFromPosition(TrackPosition pos, | 
| 93 |                                                            bool ignorePause) const | 
| 94 | { | 
| 95 |     auto position = m_paused && !ignorePause ? m_position : TrackPosition(pos); | 
| 96 |  | 
| 97 |     if (m_softSyncData && position < m_softSyncData->dstPosition) { | 
| 98 |         const auto rate = position > m_softSyncData->srcPosition ? m_softSyncData->internalRate | 
| 99 |                                                                  : m_playbackRate; | 
| 100 |         return m_softSyncData->srcTimePoint | 
| 101 |                 + toClockDuration(trackDuration: position - m_softSyncData->srcPosition, rate); | 
| 102 |     } | 
| 103 |  | 
| 104 |     return timeFromPositionInternal(pos: position); | 
| 105 | } | 
| 106 |  | 
| 107 | TimeController::SoftSyncData TimeController::makeSoftSyncData(const TimePoint &srcTp, | 
| 108 |                                                               const TrackPosition &srcPos, | 
| 109 |                                                               const TimePoint &dstTp) const | 
| 110 | { | 
| 111 |     SoftSyncData result; | 
| 112 |     result.srcTimePoint = srcTp; | 
| 113 |     result.srcPosition = srcPos; | 
| 114 |     result.dstTimePoint = dstTp; | 
| 115 |     result.srcPosOffest = srcPos - positionFromTimeInternal(tp: srcTp); | 
| 116 |     result.dstPosition = positionFromTimeInternal(tp: dstTp); | 
| 117 |     result.internalRate = | 
| 118 |             static_cast<PlaybackRate>(toClockDuration(trackDuration: result.dstPosition - srcPos).count()) | 
| 119 |             / (dstTp - srcTp).count(); | 
| 120 |  | 
| 121 |     return result; | 
| 122 | } | 
| 123 |  | 
| 124 | TrackPosition TimeController::positionFromTimeInternal(const TimePoint &tp) const | 
| 125 | { | 
| 126 |     return m_position + toTrackDuration(clockDuration: tp - m_timePoint, rate: m_playbackRate); | 
| 127 | } | 
| 128 |  | 
| 129 | TimeController::TimePoint TimeController::timeFromPositionInternal(const TrackPosition &pos) const | 
| 130 | { | 
| 131 |     return m_timePoint + toClockDuration(trackDuration: pos - m_position, rate: m_playbackRate); | 
| 132 | } | 
| 133 |  | 
| 134 | void TimeController::scrollTimeTillNow() | 
| 135 | { | 
| 136 |     const auto now = RealClock::now(); | 
| 137 |     if (!m_paused) { | 
| 138 |         m_position = positionFromTimeInternal(tp: now); | 
| 139 |  | 
| 140 |         // let's forget outdated syncronizations | 
| 141 |         if (m_softSyncData && m_softSyncData->dstTimePoint <= now) | 
| 142 |             m_softSyncData.reset(); | 
| 143 |     } else if (m_softSyncData) { | 
| 144 |         m_softSyncData->dstTimePoint += now - m_timePoint; | 
| 145 |         m_softSyncData->srcTimePoint += now - m_timePoint; | 
| 146 |     } | 
| 147 |  | 
| 148 |     m_timePoint = now; | 
| 149 | } | 
| 150 |  | 
| 151 | RealClock::duration TimeController::toClockDuration(TrackDuration trackDuration, PlaybackRate rate) | 
| 152 | { | 
| 153 |     return std::chrono::duration_cast<RealClock::duration>( | 
| 154 |             d: std::chrono::microseconds(trackDuration.get()) / rate); | 
| 155 | } | 
| 156 |  | 
| 157 | TrackDuration TimeController::toTrackDuration(RealClock::duration clockDuration, PlaybackRate rate) | 
| 158 | { | 
| 159 |     return TrackDuration( | 
| 160 |             std::chrono::duration_cast<std::chrono::microseconds>(d: clockDuration * rate).count()); | 
| 161 | } | 
| 162 |  | 
| 163 | } // namespace QFFmpeg | 
| 164 |  | 
| 165 | QT_END_NAMESPACE | 
| 166 |  |