1 | // Copyright (C) 2024 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 QFFMPEGENCODERTHREAD_P_H |
4 | #define QFFMPEGENCODERTHREAD_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/qffmpegthread_p.h> |
18 | #include "qpointer.h" |
19 | #include "qsemaphore.h" |
20 | |
21 | #include <QtMultimedia/private/qmediainputencoderinterface_p.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | namespace QFFmpeg { |
26 | |
27 | class RecordingEngine; |
28 | |
29 | class EncoderThread : public ConsumerThread, public QMediaInputEncoderInterface |
30 | { |
31 | Q_OBJECT |
32 | public: |
33 | EncoderThread(RecordingEngine &recordingEngine); |
34 | |
35 | void setPaused(bool paused); |
36 | |
37 | void setAutoStop(bool autoStop); |
38 | |
39 | void setSource(QObject *source) { m_source = source; } |
40 | |
41 | QObject *source() const { return m_source; } |
42 | |
43 | bool canPushFrame() const override { return m_canPushFrame.load(m: std::memory_order_relaxed); } |
44 | |
45 | void setEndOfSourceStream(); |
46 | |
47 | bool isEndOfSourceStream() const { return m_endOfSourceStream; } |
48 | |
49 | void startEncoding(bool noError); |
50 | |
51 | bool isInitialized() const { return m_initialized; } |
52 | |
53 | protected: |
54 | bool init() override; |
55 | |
56 | void updateCanPushFrame(); |
57 | |
58 | virtual bool checkIfCanPushFrame() const = 0; |
59 | |
60 | void resetEndOfSourceStream() { m_endOfSourceStream = false; } |
61 | |
62 | auto lockLoopData() |
63 | { |
64 | return QScopeGuard([this, locker = ConsumerThread::lockLoopData()]() mutable { |
65 | const bool autoStopActivated = m_endOfSourceStream && m_autoStop; |
66 | const bool canPush = !autoStopActivated && !m_paused && checkIfCanPushFrame(); |
67 | locker.unlock(); |
68 | if (m_canPushFrame.exchange(i: canPush, m: std::memory_order_relaxed) != canPush) |
69 | emit canPushFrameChanged(); |
70 | }); |
71 | } |
72 | |
73 | Q_SIGNALS: |
74 | void canPushFrameChanged(); |
75 | void endOfSourceStream(); |
76 | void initialized(); |
77 | |
78 | protected: |
79 | bool m_paused = false; |
80 | bool m_endOfSourceStream = false; |
81 | bool m_autoStop = false; |
82 | bool m_initialized = false; |
83 | bool m_encodingStarted = false; |
84 | std::atomic_bool m_canPushFrame = false; |
85 | RecordingEngine &m_recordingEngine; |
86 | QPointer<QObject> m_source; |
87 | QSemaphore m_encodingStartSemaphore; |
88 | }; |
89 | |
90 | } // namespace QFFmpeg |
91 | |
92 | QT_END_NAMESPACE |
93 | |
94 | #endif |
95 | |