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