File indexing completed on 2024-04-21 05:44:59

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "screencasting.h"
0008 
0009 #include <PipeWireRecord>
0010 #include <KSignalHandler>
0011 #include <QCommandLineParser>
0012 #include <QGuiApplication>
0013 #include <QLoggingCategory>
0014 #include <QScreen>
0015 
0016 #include <csignal>
0017 
0018 using namespace KWayland::Client;
0019 
0020 int main(int argc, char **argv)
0021 {
0022     QGuiApplication app(argc, argv);
0023     auto m_record = new PipeWireRecord(&app);
0024 
0025     QCommandLineParser parser;
0026     QCommandLineOption outputOption(QStringLiteral("output"),
0027                                     QStringLiteral("path for the generated video"),
0028                                     QStringLiteral("path"),
0029                                     QStringLiteral("recording.%1").arg(m_record->extension()));
0030     parser.addHelpOption();
0031     parser.addOption(outputOption);
0032     parser.process(app);
0033 
0034     Screencasting screencasting;
0035 
0036     QRect region;
0037     for (auto screen : qGuiApp->screens()) {
0038         region |= screen->geometry();
0039     }
0040 
0041     auto stream = screencasting.createRegionStream(region, 1, Screencasting::Metadata);
0042     m_record->setOutput(parser.value(outputOption));
0043     QObject::connect(stream, &ScreencastingStream::created, &app, [stream, m_record] {
0044         m_record->setNodeId(stream->nodeId());
0045         m_record->setActive(true);
0046     });
0047 
0048     KSignalHandler::self()->watchSignal(SIGTERM);
0049     KSignalHandler::self()->watchSignal(SIGINT);
0050     QObject::connect(KSignalHandler::self(), &KSignalHandler::signalReceived, m_record, [m_record] {
0051         m_record->setActive(false);
0052     });
0053 
0054     QObject::connect(m_record, &PipeWireRecord::errorFound, qGuiApp, [](const QString &error) {
0055         qWarning() << "recording error!" << error;
0056         qGuiApp->exit(3);
0057     });
0058     qDebug() << "initial state" << m_record->state();
0059     bool hasStarted = false;
0060     QObject::connect(m_record, &PipeWireRecord::stateChanged, qGuiApp, [m_record, &hasStarted] {
0061         auto state = m_record->state();
0062         qDebug() << "state changed" << state;
0063         switch (state) {
0064         case PipeWireRecord::Idle:
0065             qWarning() << "idle!" << hasStarted;
0066             if (hasStarted) {
0067                 qGuiApp->quit();
0068             }
0069             break;
0070         case PipeWireRecord::Recording:
0071             qWarning() << "recording...";
0072             hasStarted = true;
0073             break;
0074         case PipeWireRecord::Rendering:
0075             qWarning() << "rendering...";
0076             break;
0077         }
0078     });
0079 
0080     return app.exec();
0081 }