summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/ffmpeg/playbackengine/qffmpegtimecontroller.cpp
blob: 989c3c222b3a1f4d6a256fa6c0ffbd3f772e561b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "playbackengine/qffmpegtimecontroller_p.h"

#include "qglobal.h"
#include "qdebug.h"

#include <algorithm>

QT_BEGIN_NAMESPACE

namespace QFFmpeg::PlaybackEngineInternal {

TimeController::TimeController()
{
    sync();
}

float TimeController::playbackRate() const
{
    return m_playbackRate;
}

void TimeController::setPlaybackRate(PlaybackRate playbackRate)
{
    if (playbackRate == m_playbackRate)
        return;

    Q_ASSERT(playbackRate > 0.f);

    scrollTimeTillNow();
    m_playbackRate = playbackRate;

    if (m_softSyncData)
        m_softSyncData = makeSoftSyncData(m_softSyncData->srcTimePoint, m_softSyncData->srcPosition,
                                          m_softSyncData->dstTimePoint);
}

void TimeController::sync(qint64 trackPos)
{
    sync(Clock::now(), trackPos);
}

void TimeController::sync(const TimePoint &tp, qint64 pos)
{
    m_softSyncData.reset();
    m_position = TrackTime(pos);
    m_timePoint = tp;
}

void TimeController::syncSoft(const TimePoint &tp, qint64 pos, const Clock::duration &fixingTime)
{
    const auto srcTime = Clock::now();
    const auto srcPos = positionFromTime(srcTime, true);
    const auto dstTime = srcTime + fixingTime;

    m_position = TrackTime(pos);
    m_timePoint = tp;

    m_softSyncData = makeSoftSyncData(srcTime, TrackTime(srcPos), dstTime);
}

qint64 TimeController::currentPosition(const Clock::duration &offset) const
{
    return positionFromTime(Clock::now() + offset);
}

void TimeController::setPaused(bool paused)
{
    if (m_paused == paused)
        return;

    scrollTimeTillNow();
    m_paused = paused;
}

qint64 TimeController::positionFromTime(TimePoint tp, bool ignorePause) const
{
    tp = m_paused && !ignorePause ? m_timePoint : tp;

    if (m_softSyncData && tp < m_softSyncData->dstTimePoint) {
        const float rate =
                tp > m_softSyncData->srcTimePoint ? m_softSyncData->internalRate : m_playbackRate;

        return (m_softSyncData->srcPosition
                + toTrackTime((tp - m_softSyncData->srcTimePoint) * rate))
                .count();
    }

    return positionFromTimeInternal(tp).count();
}

TimeController::TimePoint TimeController::timeFromPosition(qint64 pos, bool ignorePause) const
{
    auto position = m_paused && !ignorePause ? m_position : TrackTime(pos);

    if (m_softSyncData && position < m_softSyncData->dstPosition) {
        const auto rate = position > m_softSyncData->srcPosition ? m_softSyncData->internalRate
                                                                 : m_playbackRate;
        return m_softSyncData->srcTimePoint
                + toClockTime((position - m_softSyncData->srcPosition) / rate);
    }

    return timeFromPositionInternal(position);
}

TimeController::SoftSyncData TimeController::makeSoftSyncData(const TimePoint &srcTp,
                                                              const TrackTime &srcPos,
                                                              const TimePoint &dstTp) const
{
    SoftSyncData result;
    result.srcTimePoint = srcTp;
    result.srcPosition = srcPos;
    result.dstTimePoint = dstTp;
    result.srcPosOffest = srcPos - positionFromTimeInternal(srcTp);
    result.dstPosition = positionFromTimeInternal(dstTp);
    result.internalRate =
            static_cast<PlaybackRate>(toClockTime(TrackTime(result.dstPosition - srcPos)).count())
            / (dstTp - srcTp).count();

    return result;
}

TimeController::TrackTime TimeController::positionFromTimeInternal(const TimePoint &tp) const
{
    return m_position + toTrackTime((tp - m_timePoint) * m_playbackRate);
}

TimeController::TimePoint TimeController::timeFromPositionInternal(const TrackTime &pos) const
{
    return m_timePoint + toClockTime(TrackTime(pos - m_position) / m_playbackRate);
}

void TimeController::scrollTimeTillNow()
{
    const auto now = Clock::now();
    if (!m_paused)
        m_position = positionFromTimeInternal(now);
    else if (m_softSyncData) {
        m_softSyncData->dstTimePoint += now - m_timePoint;
        m_softSyncData->srcTimePoint += now - m_timePoint;
    }

    m_timePoint = now;
}

template<typename T>
TimeController::Clock::duration TimeController::toClockTime(const T &t)
{
    return std::chrono::duration_cast<Clock::duration>(t);
}

template<typename T>
TimeController::TrackTime TimeController::toTrackTime(const T &t)
{
    return std::chrono::duration_cast<TrackTime>(t);
}

}

QT_END_NAMESPACE