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
|
/*
* Copyright (C) 2018 Metrological Group B.V.
* Author: Thibault Saunier <tsaunier@igalia.com>
* Author: Alejandro G. Castro <alex@igalia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* aint with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#if ENABLE(MEDIA_STREAM) && USE(GSTREAMER)
#include "GStreamerCaptureDeviceManager.h"
#include "GStreamerVideoCaptureSource.h"
#include "PipeWireCaptureDevice.h"
#include <wtf/UUID.h>
#include <wtf/glib/GUniquePtr.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
GStreamerDisplayCaptureDeviceManager& GStreamerDisplayCaptureDeviceManager::singleton()
{
static NeverDestroyed<GStreamerDisplayCaptureDeviceManager> manager;
return manager;
}
GStreamerDisplayCaptureDeviceManager::GStreamerDisplayCaptureDeviceManager()
{
}
GStreamerDisplayCaptureDeviceManager::~GStreamerDisplayCaptureDeviceManager()
{
for (auto& sourceId : m_sessions.keys())
stopSource(sourceId);
}
void GStreamerDisplayCaptureDeviceManager::computeCaptureDevices(CompletionHandler<void()>&& callback)
{
m_devices.clear();
CaptureDevice screenCaptureDevice(createVersion4UUIDString(), CaptureDevice::DeviceType::Screen, "Capture Screen"_s);
screenCaptureDevice.setEnabled(true);
m_devices.append(WTFMove(screenCaptureDevice));
callback();
}
CaptureSourceOrError GStreamerDisplayCaptureDeviceManager::createDisplayCaptureSource(const CaptureDevice& device, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints)
{
const auto it = m_sessions.find(device.persistentId());
if (it != m_sessions.end()) {
auto& node = it->value;
PipeWireCaptureDevice pipewireCaptureDevice { *node, device.persistentId(), device.type(), device.label(), device.groupId() };
return GStreamerVideoCaptureSource::createPipewireSource(WTFMove(pipewireCaptureDevice), WTFMove(hashSalts), constraints);
}
if (!m_portal)
m_portal = DesktopPortalScreenCast::create();
if (!m_portal)
return CaptureSourceOrError({ { }, MediaAccessDenialReason::PermissionDenied });
auto session = m_portal->createScreencastSession();
if (!session)
return CaptureSourceOrError({ { }, MediaAccessDenialReason::PermissionDenied });
// FIXME: Maybe check this depending on device.type().
auto outputType = GStreamerDisplayCaptureDeviceManager::PipeWireOutputType::Monitor | GStreamerDisplayCaptureDeviceManager::PipeWireOutputType::Window;
GVariantBuilder options;
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
g_variant_builder_add(&options, "{sv}", "types", g_variant_new_uint32(static_cast<uint32_t>(outputType)));
g_variant_builder_add(&options, "{sv}", "multiple", g_variant_new_boolean(false));
if (auto version = m_portal->getProperty("version")) {
if (g_variant_get_uint32(version.get()) >= 2) {
// Enable embedded cursor. FIXME: Should be checked in the constraints.
g_variant_builder_add(&options, "{sv}", "cursor_mode", g_variant_new_uint32(2));
}
}
auto result = session->selectSources(options);
if (!result)
return CaptureSourceOrError({ { }, MediaAccessDenialReason::PermissionDenied });
GUniqueOutPtr<char> objectPath;
g_variant_get(result.get(), "(o)", &objectPath.outPtr());
m_portal->waitResponseSignal(ASCIILiteral::fromLiteralUnsafe(objectPath.get()));
result = session->start();
if (!result)
return CaptureSourceOrError({ { }, MediaAccessDenialReason::PermissionDenied });
std::optional<uint32_t> nodeId;
g_variant_get(result.get(), "(o)", &objectPath.outPtr());
m_portal->waitResponseSignal(ASCIILiteral::fromLiteralUnsafe(objectPath.get()), [&nodeId](GVariant* parameters) mutable {
uint32_t portalResponse;
GRefPtr<GVariant> responseData;
g_variant_get(parameters, "(u@a{sv})", &portalResponse, &responseData.outPtr());
if (portalResponse) {
WTFLogAlways("User cancelled the Start request or an unknown error happened");
return;
}
// The portal interface allows multiple streams but we care only about the first one.
GUniqueOutPtr<GVariantIter> iter;
if (g_variant_lookup(responseData.get(), "streams", "a(ua{sv})", &iter.outPtr())) {
auto variant = adoptGRef(g_variant_iter_next_value(iter.get()));
if (!variant) {
WTFLogAlways("Stream list is empty");
return;
}
uint32_t streamId;
GRefPtr<GVariant> options;
g_variant_get(variant.get(), "(u@a{sv})", &streamId, &options.outPtr());
nodeId = streamId;
}
});
if (!nodeId) {
WTFLogAlways("Unable to retrieve display capture session data");
return CaptureSourceOrError({ { } , MediaAccessDenialReason::PermissionDenied });
}
auto nodeData = session->openPipewireRemote();
if (!nodeData)
return CaptureSourceOrError({ { }, MediaAccessDenialReason::PermissionDenied });
nodeData->objectId = *nodeId;
PipeWireCaptureDevice pipewireCaptureDevice { *nodeData, device.persistentId(), device.type(), device.label(), device.groupId() };
m_sessions.add(device.persistentId(), makeUnique<PipeWireNodeData>(WTFMove(*nodeData)));
return GStreamerVideoCaptureSource::createPipewireSource(WTFMove(pipewireCaptureDevice), WTFMove(hashSalts), constraints);
}
void GStreamerDisplayCaptureDeviceManager::stopSource(const String& persistentID)
{
if (!m_portal) [[unlikely]]
return;
auto session = m_sessions.take(persistentID);
m_portal->closeSession(session->path);
}
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM) && USE(GSTREAMER)
|