| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #ifdef GSTREAMER_AVAILABLE |
| 6 | |
| 7 | #include "ScreenCastPortal.h" |
| 8 | #include "ChatPage.h" |
| 9 | #include "Logging.h" |
| 10 | #include "UserSettingsPage.h" |
| 11 | |
| 12 | #include <QDBusConnection> |
| 13 | #include <QDBusMessage> |
| 14 | #include <QDBusPendingCallWatcher> |
| 15 | #include <QDBusPendingReply> |
| 16 | #include <QDBusUnixFileDescriptor> |
| 17 | #include <mtxclient/utils.hpp> |
| 18 | #include <random> |
| 19 | |
| 20 | static QString |
| 21 | make_token() |
| 22 | { |
| 23 | return QString::fromStdString("nheko" + mtx::client::utils::random_token(64, false)); |
| 24 | } |
| 25 | |
| 26 | static QString |
| 27 | handle_path(QString handle_token) |
| 28 | { |
| 29 | QString sender = QDBusConnection::sessionBus().baseService(); |
| 30 | if (sender[0] == ':') |
| 31 | sender.remove(0, 1); |
| 32 | sender.replace(".", "_"); |
| 33 | return QStringLiteral("/org/freedesktop/portal/desktop/request/") + sender + |
| 34 | QStringLiteral("/") + handle_token; |
| 35 | } |
| 36 | |
| 37 | bool |
| 38 | ScreenCastPortal::makeConnection(QString service, |
| 39 | QString path, |
| 40 | QString interface, |
| 41 | QString name, |
| 42 | const char *slot) |
| 43 | { |
| 44 | if (QDBusConnection::sessionBus().connect(service, path, interface, name, this, slot)) { |
| 45 | last_connection = { |
| 46 | std::move(service), std::move(path), std::move(interface), std::move(name), slot}; |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | ScreenCastPortal::disconnectClose() |
| 54 | { |
| 55 | QDBusConnection::sessionBus().disconnect(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 56 | sessionHandle.path(), |
| 57 | QStringLiteral("org.freedesktop.portal.Session"), |
| 58 | QStringLiteral("Closed"), |
| 59 | this, |
| 60 | SLOT(closedHandler(QVariantMap))); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | ScreenCastPortal::removeConnection() |
| 65 | { |
| 66 | if (!last_connection.has_value()) |
| 67 | return; |
| 68 | |
| 69 | const auto &connection = *last_connection; |
| 70 | QDBusConnection::sessionBus().disconnect(connection[0], |
| 71 | connection[1], |
| 72 | connection[2], |
| 73 | connection[3], |
| 74 | this, |
| 75 | connection[4].toLocal8Bit().data()); |
| 76 | last_connection = std::nullopt; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | ScreenCastPortal::init() |
| 81 | { |
| 82 | switch (state) { |
| 83 | case State::Closed: |
| 84 | state = State::Starting; |
| 85 | createSession(); |
| 86 | break; |
| 87 | case State::Starting: |
| 88 | nhlog::ui()->warn("ScreenCastPortal already starting"); |
| 89 | break; |
| 90 | case State::Started: |
| 91 | close(true); |
| 92 | break; |
| 93 | case State::Closing: |
| 94 | nhlog::ui()->warn("ScreenCastPortal still closing"); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | const ScreenCastPortal::Stream * |
| 100 | ScreenCastPortal::getStream() const |
| 101 | { |
| 102 | if (state != State::Started) |
| 103 | return nullptr; |
| 104 | else |
| 105 | return &stream; |
| 106 | } |
| 107 | |
| 108 | bool |
| 109 | ScreenCastPortal::ready() const |
| 110 | { |
| 111 | return state == State::Started; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | ScreenCastPortal::close(bool reinit) |
| 116 | { |
| 117 | switch (state) { |
| 118 | case State::Closed: |
| 119 | if (reinit) |
| 120 | init(); |
| 121 | break; |
| 122 | case State::Starting: |
| 123 | if (!reinit) { |
| 124 | disconnectClose(); |
| 125 | removeConnection(); |
| 126 | state = State::Closed; |
| 127 | } |
| 128 | break; |
| 129 | case State::Started: { |
| 130 | state = State::Closing; |
| 131 | disconnectClose(); |
| 132 | // Close file descriptor if it was opened |
| 133 | stream = Stream{}; |
| 134 | |
| 135 | emit readyChanged(); |
| 136 | |
| 137 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 138 | sessionHandle.path(), |
| 139 | QStringLiteral("org.freedesktop.portal.Session"), |
| 140 | QStringLiteral("Close")); |
| 141 | |
| 142 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 143 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 144 | connect(watcher, |
| 145 | &QDBusPendingCallWatcher::finished, |
| 146 | this, |
| 147 | [this, reinit](QDBusPendingCallWatcher *self) { |
| 148 | self->deleteLater(); |
| 149 | QDBusPendingReply reply = *self; |
| 150 | |
| 151 | if (!reply.isValid()) { |
| 152 | nhlog::ui()->warn("org.freedesktop.portal.ScreenCast (Close): {}", |
| 153 | reply.error().message().toStdString()); |
| 154 | } |
| 155 | state = State::Closed; |
| 156 | if (reinit) |
| 157 | init(); |
| 158 | }); |
| 159 | } break; |
| 160 | case State::Closing: |
| 161 | nhlog::ui()->warn("ScreenCastPortal already closing"); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | ScreenCastPortal::closedHandler(uint response, const QVariantMap &) |
| 168 | { |
| 169 | removeConnection(); |
| 170 | disconnectClose(); |
| 171 | |
| 172 | if (response != 0) { |
| 173 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (Closed): {}", response); |
| 174 | } |
| 175 | |
| 176 | nhlog::ui()->debug("org.freedesktop.portal.ScreenCast: Connection closed"); |
| 177 | state = State::Closed; |
| 178 | emit readyChanged(); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | ScreenCastPortal::createSession() |
| 183 | { |
| 184 | // Connect before sending the request to avoid missing the reply |
| 185 | QString handle_token = make_token(); |
| 186 | if (!makeConnection(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 187 | handle_path(handle_token), |
| 188 | QStringLiteral("org.freedesktop.portal.Request"), |
| 189 | QStringLiteral("Response"), |
| 190 | SLOT(createSessionHandler(uint, QVariantMap)))) { |
| 191 | nhlog::ui()->error( |
| 192 | "Connection to signal Response for org.freedesktop.portal.Request failed"); |
| 193 | close(); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 198 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 199 | QStringLiteral("org.freedesktop.portal.ScreenCast"), |
| 200 | QStringLiteral("CreateSession")); |
| 201 | msg << QVariantMap{{QStringLiteral("handle_token"), handle_token}, |
| 202 | {QStringLiteral("session_handle_token"), make_token()}}; |
| 203 | |
| 204 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 205 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 206 | connect( |
| 207 | watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { |
| 208 | self->deleteLater(); |
| 209 | QDBusPendingReply<QDBusObjectPath> reply = *self; |
| 210 | |
| 211 | if (!reply.isValid()) { |
| 212 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (CreateSession): {}", |
| 213 | reply.error().message().toStdString()); |
| 214 | close(); |
| 215 | } |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | ScreenCastPortal::createSessionHandler(uint response, const QVariantMap &results) |
| 221 | { |
| 222 | removeConnection(); |
| 223 | |
| 224 | if (state != State::Starting) { |
| 225 | nhlog::ui()->warn("ScreenCastPortal not starting"); |
| 226 | return; |
| 227 | } |
| 228 | if (response != 0) { |
| 229 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (CreateSession Response): {}", |
| 230 | response); |
| 231 | close(); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | sessionHandle = QDBusObjectPath(results.value(QStringLiteral("session_handle")).toString()); |
| 236 | |
| 237 | nhlog::ui()->debug("org.freedesktop.portal.ScreenCast: sessionHandle = {}", |
| 238 | sessionHandle.path().toStdString()); |
| 239 | |
| 240 | QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 241 | sessionHandle.path(), |
| 242 | QStringLiteral("org.freedesktop.portal.Session"), |
| 243 | QStringLiteral("Closed"), |
| 244 | this, |
| 245 | SLOT(closedHandler(QVariantMap))); |
| 246 | |
| 247 | getAvailableSourceTypes(); |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | ScreenCastPortal::getAvailableSourceTypes() |
| 252 | { |
| 253 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 254 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 255 | QStringLiteral("org.freedesktop.DBus.Properties"), |
| 256 | QStringLiteral("Get")); |
| 257 | msg << QStringLiteral("org.freedesktop.portal.ScreenCast") |
| 258 | << QStringLiteral("AvailableSourceTypes"); |
| 259 | |
| 260 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 261 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 262 | connect( |
| 263 | watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { |
| 264 | self->deleteLater(); |
| 265 | QDBusPendingReply<QDBusVariant> reply = *self; |
| 266 | |
| 267 | if (!reply.isValid()) { |
| 268 | nhlog::ui()->error("org.freedesktop.DBus.Properties (Get AvailableSourceTypes): {}", |
| 269 | reply.error().message().toStdString()); |
| 270 | close(); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | if (state != State::Starting) { |
| 275 | nhlog::ui()->warn("ScreenCastPortal not starting"); |
| 276 | return; |
| 277 | } |
| 278 | const auto &value = reply.value().variant(); |
| 279 | if (value.canConvert<uint>()) { |
| 280 | availableSourceTypes = value.value<uint>(); |
| 281 | } else { |
| 282 | nhlog::ui()->error("Invalid reply from org.freedesktop.DBus.Properties (Get " |
| 283 | "AvailableSourceTypes)"); |
| 284 | close(); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | getAvailableCursorModes(); |
| 289 | }); |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | ScreenCastPortal::getAvailableCursorModes() |
| 294 | { |
| 295 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 296 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 297 | QStringLiteral("org.freedesktop.DBus.Properties"), |
| 298 | QStringLiteral("Get")); |
| 299 | msg << QStringLiteral("org.freedesktop.portal.ScreenCast") |
| 300 | << QStringLiteral("AvailableCursorModes"); |
| 301 | |
| 302 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 303 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 304 | connect( |
| 305 | watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { |
| 306 | self->deleteLater(); |
| 307 | QDBusPendingReply<QDBusVariant> reply = *self; |
| 308 | |
| 309 | if (!reply.isValid()) { |
| 310 | nhlog::ui()->error("org.freedesktop.DBus.Properties (Get AvailableCursorModes): {}", |
| 311 | reply.error().message().toStdString()); |
| 312 | close(); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | if (state != State::Starting) { |
| 317 | nhlog::ui()->warn("ScreenCastPortal not starting"); |
| 318 | return; |
| 319 | } |
| 320 | const auto &value = reply.value().variant(); |
| 321 | if (value.canConvert<uint>()) { |
| 322 | availableCursorModes = value.value<uint>(); |
| 323 | } else { |
| 324 | nhlog::ui()->error("Invalid reply from org.freedesktop.DBus.Properties (Get " |
| 325 | "AvailableCursorModes)"); |
| 326 | close(); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | selectSources(); |
| 331 | }); |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | ScreenCastPortal::selectSources() |
| 336 | { |
| 337 | // Connect before sending the request to avoid missing the reply |
| 338 | auto handle_token = make_token(); |
| 339 | if (!makeConnection(QString(), |
| 340 | handle_path(handle_token), |
| 341 | QStringLiteral("org.freedesktop.portal.Request"), |
| 342 | QStringLiteral("Response"), |
| 343 | SLOT(selectSourcesHandler(uint, QVariantMap)))) { |
| 344 | nhlog::ui()->error( |
| 345 | "Connection to signal Response for org.freedesktop.portal.Request failed"); |
| 346 | close(); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 351 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 352 | QStringLiteral("org.freedesktop.portal.ScreenCast"), |
| 353 | QStringLiteral("SelectSources")); |
| 354 | |
| 355 | QVariantMap options{{QStringLiteral("multiple"), false}, |
| 356 | {QStringLiteral("types"), availableSourceTypes}, |
| 357 | {QStringLiteral("handle_token"), handle_token}}; |
| 358 | |
| 359 | auto settings = ChatPage::instance()->userSettings(); |
| 360 | if (settings->screenShareHideCursor() && (availableCursorModes & (uint)1) != 0) { |
| 361 | options["cursor_mode"] = (uint)1; |
| 362 | } |
| 363 | |
| 364 | msg << QVariant::fromValue(sessionHandle) << options; |
| 365 | |
| 366 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 367 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 368 | connect( |
| 369 | watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { |
| 370 | self->deleteLater(); |
| 371 | QDBusPendingReply<QDBusObjectPath> reply = *self; |
| 372 | |
| 373 | if (!reply.isValid()) { |
| 374 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (SelectSources): {}", |
| 375 | reply.error().message().toStdString()); |
| 376 | close(); |
| 377 | } |
| 378 | }); |
| 379 | } |
| 380 | |
| 381 | void |
| 382 | ScreenCastPortal::selectSourcesHandler(uint response, const QVariantMap &) |
| 383 | { |
| 384 | removeConnection(); |
| 385 | |
| 386 | if (state != State::Starting) { |
| 387 | nhlog::ui()->warn("ScreenCastPortal not starting"); |
| 388 | return; |
| 389 | } |
| 390 | if (response != 0) { |
| 391 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (SelectSources Response): {}", |
| 392 | response); |
| 393 | close(); |
| 394 | return; |
| 395 | } |
| 396 | start(); |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | ScreenCastPortal::start() |
| 401 | { |
| 402 | // Connect before sending the request to avoid missing the reply |
| 403 | auto handle_token = make_token(); |
| 404 | if (!makeConnection(QString(), |
| 405 | handle_path(handle_token), |
| 406 | QStringLiteral("org.freedesktop.portal.Request"), |
| 407 | QStringLiteral("Response"), |
| 408 | SLOT(startHandler(uint, QVariantMap)))) { |
| 409 | nhlog::ui()->error("Connection to org.freedesktop.portal.Request Response failed"); |
| 410 | close(); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 415 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 416 | QStringLiteral("org.freedesktop.portal.ScreenCast"), |
| 417 | QStringLiteral("Start")); |
| 418 | msg << QVariant::fromValue(sessionHandle) << QString() |
| 419 | << QVariantMap{{QStringLiteral("handle_token"), handle_token}}; |
| 420 | |
| 421 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 422 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 423 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [](QDBusPendingCallWatcher *self) { |
| 424 | self->deleteLater(); |
| 425 | QDBusPendingReply<QDBusObjectPath> reply = *self; |
| 426 | |
| 427 | if (!reply.isValid()) { |
| 428 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (Start): {}", |
| 429 | reply.error().message().toStdString()); |
| 430 | } |
| 431 | }); |
| 432 | } |
| 433 | |
| 434 | struct PipeWireStream |
| 435 | { |
| 436 | quint32 nodeId = 0; |
| 437 | QVariantMap map; |
| 438 | }; |
| 439 | |
| 440 | const QDBusArgument & |
| 441 | operator>>(const QDBusArgument &argument, PipeWireStream &stream) |
| 442 | { |
| 443 | argument.beginStructure(); |
| 444 | argument >> stream.nodeId; |
| 445 | argument.beginMap(); |
| 446 | while (!argument.atEnd()) { |
| 447 | QString key; |
| 448 | QVariant map; |
| 449 | argument.beginMapEntry(); |
| 450 | argument >> key >> map; |
| 451 | argument.endMapEntry(); |
| 452 | stream.map.insert(key, map); |
| 453 | } |
| 454 | argument.endMap(); |
| 455 | argument.endStructure(); |
| 456 | return argument; |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | ScreenCastPortal::startHandler(uint response, const QVariantMap &results) |
| 461 | { |
| 462 | removeConnection(); |
| 463 | |
| 464 | if (response != 0) { |
| 465 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (Start Response): {}", response); |
| 466 | close(); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | QVector<PipeWireStream> streams = |
| 471 | qdbus_cast<QVector<PipeWireStream>>(results.value(QStringLiteral("streams"))); |
| 472 | if (streams.size() == 0) { |
| 473 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast: No stream was returned"); |
| 474 | close(); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | stream.nodeId = streams[0].nodeId; |
| 479 | nhlog::ui()->debug("org.freedesktop.portal.ScreenCast: nodeId = {}", stream.nodeId); |
| 480 | openPipeWireRemote(); |
| 481 | } |
| 482 | |
| 483 | void |
| 484 | ScreenCastPortal::openPipeWireRemote() |
| 485 | { |
| 486 | auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.portal.Desktop"), |
| 487 | QStringLiteral("/org/freedesktop/portal/desktop"), |
| 488 | QStringLiteral("org.freedesktop.portal.ScreenCast"), |
| 489 | QStringLiteral("OpenPipeWireRemote")); |
| 490 | msg << QVariant::fromValue(sessionHandle) << QVariantMap{}; |
| 491 | |
| 492 | QDBusPendingCall pendingCall = QDBusConnection::sessionBus().asyncCall(msg); |
| 493 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
| 494 | connect( |
| 495 | watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *self) { |
| 496 | self->deleteLater(); |
| 497 | QDBusPendingReply<QDBusUnixFileDescriptor> reply = *self; |
| 498 | |
| 499 | if (!reply.isValid()) { |
| 500 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast (OpenPipeWireRemote): {}", |
| 501 | reply.error().message().toStdString()); |
| 502 | close(); |
| 503 | } else { |
| 504 | stream.fd = reply.value(); |
| 505 | nhlog::ui()->error("org.freedesktop.portal.ScreenCast: fd = {}", |
| 506 | stream.fd.fileDescriptor()); |
| 507 | state = State::Started; |
| 508 | emit readyChanged(); |
| 509 | } |
| 510 | }); |
| 511 | } |
| 512 | |
| 513 | #endif |
| 514 | |
| 515 | #include "moc_ScreenCastPortal.cpp" |