File: RecordMe.cpp

package info (click to toggle)
kpipewire 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 996 kB
  • sloc: cpp: 6,204; xml: 133; makefile: 5; sh: 1
file content (227 lines) | stat: -rw-r--r-- 6,967 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
    SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/

#include "RecordMe.h"
#include <QLoggingCategory>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTimer>

#include "xdp_dbus_screencast_interface.h"

Q_DECLARE_METATYPE(Stream)

QDebug operator<<(QDebug debug, const Stream& plug)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "Stream(id: " << plug.id << ", opts: " << plug.opts << ')';
    return debug;
}


const QDBusArgument &operator<<(const QDBusArgument &argument, const Stream &/*stream*/)
{
    argument.beginStructure();
//     argument << stream.id << stream.opts;
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, Stream &stream)
{
    argument.beginStructure();
    argument >> stream.id >> stream.opts;
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Stream> &stream)
{
    argument.beginArray();
    while ( !argument.atEnd() ) {
        Stream element;
        argument >> element;
        stream.append( element );
    }
    argument.endArray();
    return argument;
}

RecordMe::RecordMe(QObject* parent)
    : QObject(parent)
    , iface(new OrgFreedesktopPortalScreenCastInterface(
        QLatin1String("org.freedesktop.portal.Desktop"), QLatin1String("/org/freedesktop/portal/desktop"), QDBusConnection::sessionBus(), this))
    , m_durationTimer(new QTimer(this))
    , m_handleToken(QStringLiteral("RecordMe%1").arg(QRandomGenerator::global()->generate()))
    , m_engine(new QQmlApplicationEngine(this))
{
    m_engine->rootContext()->setContextProperty(QStringLiteral("app"), this);
    m_engine->load(QUrl(QStringLiteral("qrc:/main.qml")));

    // create session
    const auto sessionParameters = QVariantMap {
        { QLatin1String("session_handle_token"), m_handleToken },
        { QLatin1String("handle_token"), m_handleToken }
    };
    auto sessionReply = iface->CreateSession(sessionParameters);
    sessionReply.waitForFinished();
    if (!sessionReply.isValid()) {
        qWarning("Couldn't initialize the remote control session");
        exit(1);
        return;
    }

    const bool ret = QDBusConnection::sessionBus().connect(QString(),
                                                           sessionReply.value().path(),
                                                           QLatin1String("org.freedesktop.portal.Request"),
                                                           QLatin1String("Response"),
                                                           this,
                                                           SLOT(response(uint, QVariantMap)));
    if (!ret) {
        qWarning() << "failed to create session";
        exit(2);
        return;
    }

    qDBusRegisterMetaType<Stream>();
    qDBusRegisterMetaType<QList<Stream>>();

    m_durationTimer->setSingleShot(true);
}

RecordMe::~RecordMe() = default;

void RecordMe::init(const QDBusObjectPath& path)
{
    m_path = path;
    {
        uint32_t cursor_mode;
        if (iface->availableCursorModes() & Metadata) {
            cursor_mode = Metadata;
        } else {
            cursor_mode = Hidden;
        }
        QVariantMap sourcesParameters = {{QLatin1String("handle_token"), m_handleToken},
                                         {QLatin1String("types"), iface->availableSourceTypes()},
                                         {QLatin1String("multiple"), false}, // for now?
                                         {QLatin1String("cursor_mode"), uint(cursor_mode)},
                                         {QLatin1String("persist_mode"), uint(m_persistMode)}};

        if (!m_restoreToken.isEmpty()) {
            sourcesParameters[QLatin1String("restore_token")] = m_restoreToken;
        }

        auto reply = iface->SelectSources(m_path, sourcesParameters);
        reply.waitForFinished();

        if (reply.isError()) {
            qWarning() << "Could not select sources" << reply.error();
            exit(1);
            return;
        }
        qDebug() << "select sources done" << reply.value().path();
    }
}

void RecordMe::response(uint code, const QVariantMap& results)
{
    if (code > 0) {
        qWarning() << "error!!!" << code << results;
        exit(666);
        return;
    }

    if (results.contains(QLatin1String("restore_token"))) {
        qDebug() << "Restore token:" << results[QLatin1String("restore_token")].toString();
    }

    const auto streamsIt = results.constFind(QStringLiteral("streams"));
    if (streamsIt != results.constEnd()) {
        QList<Stream> streams;
        streamsIt->value<QDBusArgument>() >> streams;

        handleStreams(streams);
        return;
    }

    const auto handleIt = results.constFind(QStringLiteral("session_handle"));
    if (handleIt != results.constEnd()) {
        init(QDBusObjectPath(handleIt->toString()));
        return;
    }

    qDebug() << "params" << results << code;
    if (results.isEmpty()) {
        start();
        return;
    }
}

void RecordMe::start()
{
    const QVariantMap startParameters = {
        { QLatin1String("handle_token"), m_handleToken }
    };

    auto reply = iface->Start(m_path, QStringLiteral("org.freedesktop.RecordMe"), startParameters);
    reply.waitForFinished();

    if (reply.isError()) {
        qWarning() << "Could not start stream" << reply.error();
        exit(1);
        return;
    }
    qDebug() << "started!" << reply.value().path();
}

void RecordMe::handleStreams(const QList<Stream> &streams)
{
    const QVariantMap startParameters = {
        { QLatin1String("handle_token"), m_handleToken }
    };

    auto reply = iface->OpenPipeWireRemote(m_path, startParameters);
    reply.waitForFinished();

    if (reply.isError()) {
        qWarning() << "Could not start stream" << reply.error();
        exit(1);
        return;
    }

    const int fd = reply.value().takeFileDescriptor();

    const auto roots = m_engine->rootObjects();
    for (const auto &stream : streams) {
        for (auto root : roots) {
            auto mo = root->metaObject();
            qDebug() << "feeding..." << stream.id << fd;
            mo->invokeMethod(root,
                             "addStream",
                             Q_ARG(QVariant, QVariant::fromValue<quint32>(stream.id)),
                             Q_ARG(QVariant, m_handleToken),
                             Q_ARG(QVariant, fd),
                             Q_ARG(QVariant, true));
        }
    }
}

void RecordMe::setPersistMode(PersistMode persistMode)
{
    m_persistMode = persistMode;
}

void RecordMe::setRestoreToken(const QString &restoreToken)
{
    m_restoreToken = restoreToken;
}

void RecordMe::setDuration(int duration)
{
    m_durationTimer->setInterval(duration);
}

#include "moc_RecordMe.cpp"