| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "capturesessionfixture_p.h" |
| 5 | #include <QtTest/qtest.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | using namespace std::chrono; |
| 10 | |
| 11 | CaptureSessionFixture::CaptureSessionFixture(StreamType streamType) : m_streamType{ streamType } { } |
| 12 | |
| 13 | CaptureSessionFixture::~CaptureSessionFixture() |
| 14 | { |
| 15 | if (!m_recorder.actualLocation().isEmpty()) |
| 16 | QFile::remove(fileName: m_recorder.actualLocation().toLocalFile()); |
| 17 | } |
| 18 | |
| 19 | void CaptureSessionFixture::setVideoSink(QVideoSink *videoSink) |
| 20 | { |
| 21 | m_videoSink = videoSink; |
| 22 | } |
| 23 | |
| 24 | void CaptureSessionFixture::start(RunMode mode, AutoStop autoStop) |
| 25 | { |
| 26 | if (hasVideo()) { |
| 27 | m_session.setVideoFrameInput(&m_videoInput); |
| 28 | |
| 29 | QObject::connect(sender: &m_videoGenerator, signal: &VideoGenerator::frameCreated, // |
| 30 | context: &m_videoInput, slot: &QVideoFrameInput::sendVideoFrame); |
| 31 | |
| 32 | if (autoStop == AutoStop::EmitEmpty) { |
| 33 | m_recorder.setAutoStop(true); |
| 34 | m_videoGenerator.emitEmptyFrameOnStop(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (hasAudio()) { |
| 39 | m_session.setAudioBufferInput(&m_audioInput); |
| 40 | |
| 41 | QObject::connect(sender: &m_audioGenerator, signal: &AudioGenerator::audioBufferCreated, // |
| 42 | context: &m_audioInput, slot: &QAudioBufferInput::sendAudioBuffer); |
| 43 | |
| 44 | if (autoStop == AutoStop::EmitEmpty) { |
| 45 | m_recorder.setAutoStop(true); |
| 46 | m_audioGenerator.emitEmptyBufferOnStop(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (mode == RunMode::Pull) { |
| 51 | if (hasVideo()) |
| 52 | QObject::connect(sender: &m_videoInput, signal: &QVideoFrameInput::readyToSendVideoFrame, // |
| 53 | context: &m_videoGenerator, slot: &VideoGenerator::nextFrame); |
| 54 | |
| 55 | if (hasAudio()) |
| 56 | QObject::connect(sender: &m_audioInput, signal: &QAudioBufferInput::readyToSendAudioBuffer, // |
| 57 | context: &m_audioGenerator, slot: &AudioGenerator::nextBuffer); |
| 58 | } |
| 59 | |
| 60 | m_session.setRecorder(&m_recorder); |
| 61 | m_recorder.setQuality(QMediaRecorder::VeryHighQuality); |
| 62 | |
| 63 | if (m_recorder.outputLocation().isEmpty()) { |
| 64 | // Create a temporary file. |
| 65 | // The file name is not available without opening. |
| 66 | m_tempFile.open(); |
| 67 | m_tempFile.close(); |
| 68 | |
| 69 | m_recorder.setOutputLocation(m_tempFile.fileName()); |
| 70 | } |
| 71 | // else: outputLocation has been already set |
| 72 | |
| 73 | m_recorder.record(); |
| 74 | |
| 75 | // HACK: Add sink after starting recording because setting the video sink will |
| 76 | // emit ready signals and start the processing chain. TODO: Fix this |
| 77 | if (m_videoSink) |
| 78 | m_session.setVideoSink(m_videoSink); |
| 79 | } |
| 80 | |
| 81 | bool CaptureSessionFixture::waitForRecorderStopped(std::chrono::milliseconds duration) |
| 82 | { |
| 83 | // StoppedState is emitted when media is finalized. |
| 84 | const bool stopped = QTest::qWaitFor( |
| 85 | predicate: [&] { // |
| 86 | return recorderStateChanged.contains( |
| 87 | t: QList<QVariant>{ QMediaRecorder::RecorderState::StoppedState }); |
| 88 | }, |
| 89 | deadline: duration); |
| 90 | |
| 91 | if (!stopped) |
| 92 | return false; |
| 93 | |
| 94 | return m_recorder.recorderState() == QMediaRecorder::StoppedState; |
| 95 | } |
| 96 | |
| 97 | bool CaptureSessionFixture::hasAudio() const |
| 98 | { |
| 99 | return m_streamType == StreamType::Audio || m_streamType == StreamType::AudioAndVideo; |
| 100 | } |
| 101 | |
| 102 | bool CaptureSessionFixture::hasVideo() const |
| 103 | { |
| 104 | return m_streamType == StreamType::Video || m_streamType == StreamType::AudioAndVideo; |
| 105 | } |
| 106 | |
| 107 | QT_END_NAMESPACE |
| 108 | |