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/qffmpegplaybackengineobject_p.h"
5
6#include "qtimer.h"
7#include "qdebug.h"
8
9QT_BEGIN_NAMESPACE
10
11namespace QFFmpeg {
12
13static QAtomicInteger<PlaybackEngineObject::Id> PersistentId = 0;
14
15PlaybackEngineObject::PlaybackEngineObject() : m_id(PersistentId.fetchAndAddRelaxed(valueToAdd: 1)) { }
16
17PlaybackEngineObject::~PlaybackEngineObject()
18{
19 if (thread() != QThread::currentThread())
20 qWarning() << "The playback engine object is being removed in an unexpected thread";
21}
22
23bool PlaybackEngineObject::isPaused() const
24{
25 return m_paused;
26}
27
28void PlaybackEngineObject::setAtEnd(bool isAtEnd)
29{
30 if (m_atEnd.testAndSetRelease(expectedValue: !isAtEnd, newValue: isAtEnd) && isAtEnd)
31 emit atEnd();
32}
33
34bool PlaybackEngineObject::isAtEnd() const
35{
36 return m_atEnd;
37}
38
39PlaybackEngineObject::Id PlaybackEngineObject::id() const
40{
41 return m_id;
42}
43
44void PlaybackEngineObject::setPaused(bool isPaused)
45{
46 if (m_paused.testAndSetRelease(expectedValue: !isPaused, newValue: isPaused))
47 QMetaObject::invokeMethod(object: this, function: &PlaybackEngineObject::onPauseChanged);
48}
49
50void PlaybackEngineObject::kill()
51{
52 m_deleting.storeRelease(newValue: true);
53
54 disconnect();
55 deleteLater();
56}
57
58bool PlaybackEngineObject::canDoNextStep() const
59{
60 return !m_paused;
61}
62
63QTimer &PlaybackEngineObject::timer()
64{
65 if (!m_timer) {
66 m_timer = std::make_unique<QTimer>();
67 m_timer->setTimerType(Qt::PreciseTimer);
68 m_timer->setSingleShot(true);
69 connect(sender: m_timer.get(), signal: &QTimer::timeout, context: this, slot: &PlaybackEngineObject::onTimeout);
70 }
71
72 return *m_timer;
73}
74
75void PlaybackEngineObject::onTimeout()
76{
77 if (!m_deleting && canDoNextStep())
78 doNextStep();
79}
80
81int PlaybackEngineObject::timerInterval() const
82{
83 return 0;
84}
85
86void PlaybackEngineObject::onPauseChanged()
87{
88 scheduleNextStep();
89}
90
91void PlaybackEngineObject::scheduleNextStep(bool allowDoImmediatelly)
92{
93 if (!m_deleting && canDoNextStep()) {
94 const auto interval = timerInterval();
95 if (interval == 0 && allowDoImmediatelly) {
96 timer().stop();
97 doNextStep();
98 } else {
99 timer().start(msec: interval);
100 }
101 } else {
102 timer().stop();
103 }
104}
105} // namespace QFFmpeg
106
107QT_END_NAMESPACE
108
109#include "moc_qffmpegplaybackengineobject_p.cpp"
110

source code of qtmultimedia/src/plugins/multimedia/ffmpeg/playbackengine/qffmpegplaybackengineobject.cpp