Warning, file /plasma/krdp/examples/streamer/main.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2023 Arjen Hiemstra <ahiemstra@heimr.nl>
0002 //
0003 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0004 
0005 #include <csignal>
0006 
0007 #include <QCommandLineParser>
0008 #include <QDebug>
0009 #include <QFile>
0010 #include <QGuiApplication>
0011 #include <QTimer>
0012 #include <QUrl>
0013 
0014 #include "PortalSession.h"
0015 #include "VideoStream.h"
0016 
0017 int main(int argc, char **argv)
0018 {
0019     QGuiApplication application{argc, argv};
0020 
0021     QCommandLineParser parser;
0022     parser.addHelpOption();
0023     parser.addOptions({
0024         {u"quit-after"_qs, u"Quit after running for this amount of seconds"_qs, u"seconds"_qs},
0025         {u"monitor"_qs, u"Index of the monitor to display."_qs, u"monitor"_qs, u"-1"_qs},
0026         {u"quality"_qs, u"Encoding quality of the stream, from 0 (lowest) to 100 (highest)"_qs, u"quality"_qs},
0027     });
0028     parser.process(application);
0029 
0030     KRdp::PortalSession session{nullptr};
0031     session.requestStreamingEnable(&application);
0032     session.setActiveStream(parser.value(u"monitor"_qs).toInt());
0033     if (parser.isSet(u"quality"_qs)) {
0034         session.setVideoQuality(parser.value(u"quality"_qs).toUShort());
0035     }
0036 
0037     signal(SIGINT, [](int) {
0038         QCoreApplication::exit(0);
0039     });
0040 
0041     signal(SIGUSR1, [](int) {
0042         QCoreApplication::exit(0);
0043     });
0044 
0045     QFile file{u"stream.raw"_qs};
0046     if (!file.open(QFile::WriteOnly)) {
0047         qDebug() << "Failed opening stream.raw";
0048         return -1;
0049     }
0050 
0051     QObject::connect(&session, &KRdp::PortalSession::frameReceived, &session, [&file](const KRdp::VideoFrame &frame) {
0052         file.write(frame.data);
0053     });
0054 
0055     QTimer timer;
0056     QObject::connect(&timer, &QTimer::timeout, &application, &QCoreApplication::quit);
0057     if (parser.isSet(u"quit-after"_qs)) {
0058         QObject::connect(&session, &KRdp::PortalSession::started, &timer, qOverload<>(&QTimer::start));
0059         timer.setInterval(parser.value(u"quit-after"_qs).toInt() * 1000);
0060     }
0061 
0062     auto result = application.exec();
0063 
0064     file.close();
0065 
0066     return result;
0067 }