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 | #ifndef QFFMPEGTHREAD_P_H |
4 | #define QFFMPEGTHREAD_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 <private/qtmultimediaglobal_p.h> |
18 | |
19 | #include <qmutex.h> |
20 | #include <qwaitcondition.h> |
21 | #include <qthread.h> |
22 | |
23 | QT_BEGIN_NAMESPACE |
24 | |
25 | class QAudioSink; |
26 | |
27 | namespace QFFmpeg |
28 | { |
29 | |
30 | /*! |
31 | FFmpeg thread that is used to implement a consumer pattern. |
32 | |
33 | This thread processes work items until no more data is available. |
34 | When no more data is available, it sleeps until it is notified about |
35 | more available data. |
36 | */ |
37 | class ConsumerThread : public QThread |
38 | { |
39 | public: |
40 | struct Deleter |
41 | { |
42 | void operator()(ConsumerThread *thread) const { thread->stopAndDelete(); } |
43 | }; |
44 | |
45 | protected: |
46 | /*! |
47 | Stops the thread and deletes this object |
48 | */ |
49 | void stopAndDelete(); |
50 | |
51 | /*! |
52 | Called on this thread when thread starts |
53 | */ |
54 | virtual bool init() = 0; |
55 | |
56 | /*! |
57 | Called on this thread before thread exits |
58 | */ |
59 | virtual void cleanup() = 0; |
60 | |
61 | /*! |
62 | Process one work item. Called repeatedly until hasData() returns |
63 | false, in which case the thread sleeps until the next dataReady() |
64 | notification. |
65 | |
66 | Note: processOne() should never block. |
67 | */ |
68 | virtual void processOne() = 0; |
69 | |
70 | /*! |
71 | Wake thread from sleep and process data until |
72 | hasData() returns false. The method is supposed to be invoked |
73 | right after the scope of QMutexLocker that lockLoopData returns. |
74 | */ |
75 | void dataReady(); |
76 | |
77 | /*! |
78 | Must return true when data is available for processing |
79 | */ |
80 | virtual bool hasData() const = 0; |
81 | |
82 | /*! |
83 | Locks the loop data mutex. It must be used to protect loop data |
84 | like a queue of video frames. |
85 | */ |
86 | QMutexLocker<QMutex> lockLoopData() const; |
87 | |
88 | private: |
89 | void run() final; |
90 | |
91 | mutable QMutex m_loopDataMutex; |
92 | QWaitCondition m_condition; |
93 | bool m_exit = false; |
94 | }; |
95 | |
96 | template <typename T> |
97 | using ConsumerThreadUPtr = std::unique_ptr<T, ConsumerThread::Deleter>; |
98 | } |
99 | |
100 | QT_END_NAMESPACE |
101 | |
102 | #endif |
103 | |