1 | // Copyright (C) 2024 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef TESTVIDEOSINK_H |
5 | #define TESTVIDEOSINK_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <qvideosink.h> |
19 | #include <qvideoframe.h> |
20 | #include <qelapsedtimer.h> |
21 | #include <qsignalspy.h> |
22 | #include <chrono> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | /* |
27 | This is a simple video surface which records all presented frames. |
28 | */ |
29 | class TestVideoSink : public QVideoSink |
30 | { |
31 | Q_OBJECT |
32 | public: |
33 | explicit TestVideoSink(bool storeFrames = false) : m_storeFrames(storeFrames) |
34 | { |
35 | connect(sender: this, signal: &QVideoSink::videoFrameChanged, context: this, slot: &TestVideoSink::addVideoFrame); |
36 | connect(sender: this, signal: &QVideoSink::videoFrameChanged, context: this, slot: &TestVideoSink::videoFrameChangedSync); |
37 | } |
38 | |
39 | QVideoFrame waitForFrame() |
40 | { |
41 | QSignalSpy spy(this, &TestVideoSink::videoFrameChangedSync); |
42 | return spy.wait() ? spy.at(i: 0).at(i: 0).value<QVideoFrame>() : QVideoFrame{}; |
43 | } |
44 | |
45 | void setStoreFrames(bool storeFrames = true) { m_storeFrames = storeFrames; } |
46 | |
47 | private Q_SLOTS: |
48 | void addVideoFrame(const QVideoFrame &frame) |
49 | { |
50 | if (!m_elapsedTimer.isValid()) |
51 | m_elapsedTimer.start(); |
52 | else |
53 | m_elapsedTimer.restart(); |
54 | |
55 | if (m_storeFrames) |
56 | m_frameList.append(t: frame); |
57 | |
58 | if (frame.isValid()) |
59 | m_frameTimes.emplace_back(args: std::chrono::microseconds(frame.startTime())); |
60 | |
61 | ++m_totalFrames; |
62 | } |
63 | |
64 | signals: |
65 | void videoFrameChangedSync(const QVideoFrame &frame); |
66 | |
67 | public: |
68 | QList<QVideoFrame> m_frameList; |
69 | int m_totalFrames = 0; // used instead of the list when frames are not stored |
70 | QElapsedTimer m_elapsedTimer; |
71 | using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>; |
72 | std::vector<TimePoint> m_frameTimes; |
73 | |
74 | private: |
75 | bool m_storeFrames; |
76 | }; |
77 | |
78 | QT_END_NAMESPACE |
79 | |
80 | #endif // TESTVIDEOSINK_H |
81 |