File indexing completed on 2024-05-12 05:38:09

0001 /*
0002     SPDX-FileCopyrightText: 2020 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 "screencastingrequest.h"
0008 #include "logging.h"
0009 
0010 #include <QCoreApplication>
0011 #include <QDebug>
0012 #include <QPointer>
0013 #include <functional>
0014 
0015 struct ScreencastingRequestPrivate {
0016     Screencasting *m_screenCasting = nullptr;
0017     QPointer<ScreencastingStream> m_stream;
0018     QString m_uuid;
0019     QString m_outputName;
0020     quint32 m_nodeId = 0;
0021 };
0022 
0023 ScreencastingRequest::ScreencastingRequest(QObject *parent)
0024     : QObject(parent)
0025     , d(new ScreencastingRequestPrivate)
0026 {
0027 }
0028 
0029 ScreencastingRequest::~ScreencastingRequest() = default;
0030 
0031 quint32 ScreencastingRequest::nodeId() const
0032 {
0033     return d->m_nodeId;
0034 }
0035 
0036 void ScreencastingRequest::setUuid(const QString &uuid)
0037 {
0038     if (d->m_uuid == uuid) {
0039         return;
0040     }
0041 
0042     setNodeid(0);
0043     d->m_uuid = uuid;
0044     Q_EMIT uuidChanged(uuid);
0045 
0046     if (!d->m_uuid.isEmpty()) {
0047         if (!d->m_screenCasting) {
0048             d->m_screenCasting = new Screencasting(this);
0049         }
0050         auto stream = d->m_screenCasting->createWindowStream(d->m_uuid, Screencasting::CursorMode::Hidden);
0051         if (!stream) {
0052             return;
0053         }
0054         adopt(stream);
0055     }
0056 }
0057 
0058 void ScreencastingRequest::setOutputName(const QString &outputName)
0059 {
0060     if (d->m_outputName == outputName) {
0061         return;
0062     }
0063 
0064     setNodeid(0);
0065     d->m_outputName = outputName;
0066     Q_EMIT outputNameChanged(outputName);
0067 
0068     if (!d->m_outputName.isEmpty()) {
0069         if (!d->m_screenCasting) {
0070             d->m_screenCasting = new Screencasting(this);
0071         }
0072         auto stream = d->m_screenCasting->createOutputStream(d->m_outputName, Screencasting::CursorMode::Hidden);
0073         if (!stream) {
0074             return;
0075         }
0076         adopt(stream);
0077         stream->setObjectName(d->m_outputName);
0078     }
0079 }
0080 
0081 void ScreencastingRequest::adopt(ScreencastingStream *stream)
0082 {
0083     d->m_stream = stream;
0084 
0085     connect(stream, &ScreencastingStream::created, this, &ScreencastingRequest::setNodeid);
0086     connect(stream, &ScreencastingStream::failed, this, [](const QString &error) {
0087         qWarning() << "error creating screencast" << error;
0088     });
0089     connect(stream, &ScreencastingStream::closed, this, [this, stream] {
0090         if (stream->nodeId() == d->m_nodeId) {
0091             setNodeid(0);
0092         }
0093     });
0094 }
0095 
0096 void ScreencastingRequest::setNodeid(uint nodeId)
0097 {
0098     if (nodeId != d->m_nodeId) {
0099         d->m_nodeId = nodeId;
0100         Q_EMIT nodeIdChanged(nodeId);
0101     }
0102 
0103     if (nodeId == 0 && d->m_stream) {
0104         delete d->m_stream;
0105     }
0106 }
0107 
0108 QString ScreencastingRequest::uuid() const
0109 {
0110     return d->m_uuid;
0111 }
0112 
0113 QString ScreencastingRequest::outputName() const
0114 {
0115     return d->m_outputName;
0116 }