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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/*
* Copyright (C) 2018 Metrological Group B.V.
* Copyright (C) 2020 Igalia S.L.
* 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 "GStreamerVideoCapturer.h"
#include "VideoFrameGStreamer.h"
#include <gst/app/gstappsink.h>
GST_DEBUG_CATEGORY(webkit_video_capturer_debug);
#define GST_CAT_DEFAULT webkit_video_capturer_debug
namespace WebCore {
static void initializeVideoCapturerDebugCategory()
{
ensureGStreamerInitialized();
static std::once_flag debugRegisteredFlag;
std::call_once(debugRegisteredFlag, [] {
GST_DEBUG_CATEGORY_INIT(webkit_video_capturer_debug, "webkitvideocapturer", 0, "WebKit Video Capturer");
});
}
GStreamerVideoCapturer::GStreamerVideoCapturer(GStreamerCaptureDevice&& device)
: GStreamerCapturer(WTFMove(device), adoptGRef(gst_caps_new_empty_simple("video/x-raw")))
{
initializeVideoCapturerDebugCategory();
}
GStreamerVideoCapturer::GStreamerVideoCapturer(const char* sourceFactory, CaptureDevice::DeviceType deviceType)
: GStreamerCapturer(sourceFactory, adoptGRef(gst_caps_new_empty_simple("video/x-raw")), deviceType)
{
initializeVideoCapturerDebugCategory();
}
void GStreamerVideoCapturer::setSinkVideoFrameCallback(SinkVideoFrameCallback&& callback)
{
if (m_sinkVideoFrameCallback.first)
g_signal_handler_disconnect(sink(), m_sinkVideoFrameCallback.first);
m_sinkVideoFrameCallback.second = WTFMove(callback);
m_sinkVideoFrameCallback.first = g_signal_connect_swapped(sink(), "new-sample", G_CALLBACK(+[](GStreamerVideoCapturer* capturer, GstElement* sink) -> GstFlowReturn {
auto sample = adoptGRef(gst_app_sink_pull_sample(GST_APP_SINK(sink)));
VideoFrameTimeMetadata metadata;
metadata.captureTime = MonotonicTime::now().secondsSinceEpoch();
auto buffer = gst_sample_get_buffer(sample.get());
MediaTime presentationTime = MediaTime::invalidTime();
if (GST_BUFFER_PTS_IS_VALID(buffer))
presentationTime = fromGstClockTime(GST_BUFFER_PTS(buffer));
auto& size = capturer->size();
capturer->m_sinkVideoFrameCallback.second(VideoFrameGStreamer::create(WTFMove(sample), size, presentationTime, VideoFrameGStreamer::Rotation::None, false, WTFMove(metadata)));
return GST_FLOW_OK;
}), this);
}
GstElement* GStreamerVideoCapturer::createSource()
{
auto* src = GStreamerCapturer::createSource();
if (m_nodeAndFd) {
auto& [node, fd] = *m_nodeAndFd;
auto path = AtomString::number(node);
// FIXME: The path property is deprecated in favor of target-object but the portal doesn't expose this object.
g_object_set(m_src.get(), "path", path.string().ascii().data(), nullptr);
g_object_set(m_src.get(), "fd", fd, nullptr);
}
return src;
}
GstElement* GStreamerVideoCapturer::createConverter()
{
if (isCapturingDisplay()) {
#if USE(GBM)
m_caps = buildDMABufCaps();
#endif
return nullptr;
}
auto* bin = gst_bin_new(nullptr);
auto* videoscale = makeGStreamerElement("videoscale", "videoscale");
auto* videoconvert = makeGStreamerElement("videoconvert", nullptr);
auto* videorate = makeGStreamerElement("videorate", "videorate");
// https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/issues/97#note_56575
g_object_set(videorate, "drop-only", TRUE, "average-period", UINT64_C(1), nullptr);
gst_bin_add_many(GST_BIN_CAST(bin), videoscale, videoconvert, videorate, nullptr);
m_videoSrcMIMETypeFilter = gst_element_factory_make("capsfilter", "mimetype-filter");
auto caps = adoptGRef(gst_caps_new_empty_simple("video/x-raw"));
g_object_set(m_videoSrcMIMETypeFilter.get(), "caps", caps.get(), nullptr);
auto* decodebin = makeGStreamerElement("decodebin3", nullptr);
gst_bin_add_many(GST_BIN_CAST(bin), m_videoSrcMIMETypeFilter.get(), decodebin, nullptr);
gst_element_link(m_videoSrcMIMETypeFilter.get(), decodebin);
auto sinkPad = adoptGRef(gst_element_get_static_pad(videoscale, "sink"));
g_signal_connect_swapped(decodebin, "pad-added", G_CALLBACK(+[](GstPad* sinkPad, GstPad* srcPad) {
RELEASE_ASSERT(!gst_pad_is_linked(sinkPad));
gst_pad_link(srcPad, sinkPad);
}), sinkPad.get());
gst_element_link_many(videoscale, videoconvert, videorate, nullptr);
sinkPad = adoptGRef(gst_element_get_static_pad(m_videoSrcMIMETypeFilter.get(), "sink"));
gst_element_add_pad(bin, gst_ghost_pad_new("sink", sinkPad.get()));
auto srcPad = adoptGRef(gst_element_get_static_pad(videorate, "src"));
gst_element_add_pad(bin, gst_ghost_pad_new("src", srcPad.get()));
return bin;
}
bool GStreamerVideoCapturer::setSize(const IntSize& size)
{
if (isCapturingDisplay()) {
// Pipewiresrc doesn't seem to support caps re-negotiation and framerate configuration properly.
GST_FIXME_OBJECT(m_pipeline.get(), "Resizing disabled on display capture source");
return true;
}
int width = size.width();
int height = size.height();
if (!width || !height)
return false;
auto videoResolution = getVideoResolutionFromCaps(m_caps.get());
if (videoResolution && videoResolution->width() == width && videoResolution->height() == height) {
GST_DEBUG_OBJECT(m_pipeline.get(), "Size has not changed");
return true;
}
if (UNLIKELY(!m_capsfilter))
return false;
GST_INFO_OBJECT(m_pipeline.get(), "Setting size to %dx%d", width, height);
m_size = size;
m_caps = adoptGRef(gst_caps_copy(m_caps.get()));
gst_caps_set_simple(m_caps.get(), "width", G_TYPE_INT, width, "height", G_TYPE_INT, height, nullptr);
g_object_set(m_capsfilter.get(), "caps", m_caps.get(), nullptr);
return true;
}
bool GStreamerVideoCapturer::setFrameRate(double frameRate)
{
if (isCapturingDisplay()) {
// Pipewiresrc doesn't seem to support caps re-negotiation and framerate configuration properly.
GST_FIXME_OBJECT(m_pipeline.get(), "Framerate override disabled on display capture source");
return true;
}
int numerator, denominator;
gst_util_double_to_fraction(frameRate, &numerator, &denominator);
if (numerator < -G_MAXINT) {
GST_INFO_OBJECT(m_pipeline.get(), "Framerate %f not allowed", frameRate);
return false;
}
if (!numerator) {
GST_INFO_OBJECT(m_pipeline.get(), "Do not force variable framerate");
return false;
}
if (UNLIKELY(!m_capsfilter))
return false;
m_caps = adoptGRef(gst_caps_copy(m_caps.get()));
gst_caps_set_simple(m_caps.get(), "framerate", GST_TYPE_FRACTION, numerator, denominator, nullptr);
GST_INFO_OBJECT(m_pipeline.get(), "Setting framerate to %f fps", frameRate);
g_object_set(m_capsfilter.get(), "caps", m_caps.get(), nullptr);
return true;
}
static std::optional<int> getMaxIntValueFromStructure(const GstStructure* structure, const char* fieldName)
{
const GValue* value = gst_structure_get_value(structure, fieldName);
if (!value)
return std::nullopt;
int maxInt = -G_MAXINT;
if (G_VALUE_HOLDS_INT(value))
maxInt = g_value_get_int(value);
else if (GST_VALUE_HOLDS_INT_RANGE(value))
maxInt = gst_value_get_int_range_max(value);
else if (GST_VALUE_HOLDS_ARRAY(value)) {
const guint size = gst_value_array_get_size(value);
for (guint i = 0; i < size; ++i) {
const GValue* item = gst_value_array_get_value(value, i);
if (G_VALUE_HOLDS_INT(item)) {
int val = g_value_get_int(item);
if (val > maxInt)
maxInt = val;
}
}
} else if (GST_VALUE_HOLDS_LIST(value)) {
const guint size = gst_value_list_get_size(value);
for (guint i = 0; i < size; ++i) {
const GValue* item = gst_value_list_get_value(value, i);
if (G_VALUE_HOLDS_INT(item)) {
int val = g_value_get_int(item);
if (val > maxInt)
maxInt = val;
}
}
}
return (maxInt > -G_MAXINT) ? std::make_optional<>(maxInt) : std::nullopt;
}
static std::optional<double> getMaxFractionValueFromStructure(const GstStructure* structure, const char* fieldName)
{
const GValue* value = gst_structure_get_value(structure, fieldName);
if (!value)
return std::nullopt;
double maxFraction = -G_MAXDOUBLE;
if (GST_VALUE_HOLDS_FRACTION(value)) {
gst_util_fraction_to_double(gst_value_get_fraction_numerator(value),
gst_value_get_fraction_denominator(value), &maxFraction);
} else if (GST_VALUE_HOLDS_FRACTION_RANGE(value)) {
const GValue* fractionValue = gst_value_get_fraction_range_max(value);
gst_util_fraction_to_double(gst_value_get_fraction_numerator(fractionValue),
gst_value_get_fraction_denominator(fractionValue), &maxFraction);
} else if (GST_VALUE_HOLDS_ARRAY(value)) {
const guint size = gst_value_array_get_size(value);
for (guint i = 0; i < size; ++i) {
const GValue* item = gst_value_array_get_value(value, i);
if (GST_VALUE_HOLDS_FRACTION(item)) {
double val = -G_MAXDOUBLE;
gst_util_fraction_to_double(gst_value_get_fraction_numerator(item),
gst_value_get_fraction_denominator(item), &val);
if (val > maxFraction)
maxFraction = val;
}
}
} else if (GST_VALUE_HOLDS_LIST(value)) {
const guint size = gst_value_list_get_size(value);
for (guint i = 0; i < size; ++i) {
const GValue* item = gst_value_list_get_value(value, i);
if (GST_VALUE_HOLDS_FRACTION(item)) {
double val = -G_MAXDOUBLE;
gst_util_fraction_to_double(gst_value_get_fraction_numerator(item),
gst_value_get_fraction_denominator(item), &val);
if (val > maxFraction)
maxFraction = val;
}
}
}
return (maxFraction > -G_MAXDOUBLE) ? std::make_optional<>(maxFraction) : std::nullopt;
}
void GStreamerVideoCapturer::reconfigure()
{
if (isCapturingDisplay()) {
// Pipewiresrc doesn't seem to support caps re-negotiation and framerate configuration properly.
GST_FIXME_OBJECT(m_pipeline.get(), "Caps re-negotiation disabled on display capture source");
return;
}
if (!m_videoSrcMIMETypeFilter)
return;
struct MimeTypeSelector {
String mimeType = "video/x-raw"_s;
String format;
int maxWidth = 0;
int maxHeight = 0;
double maxFrameRate = 0;
struct {
int width = 0;
int height = 0;
double frameRate = 0;
} stopCondition;
} selector;
// If nothing has been specified by the user, we target at least an arbitrary resolution of 1920x1080@24fps.
const GstStructure* capsStruct = gst_caps_get_structure(m_caps.get(), 0);
selector.stopCondition.width = gstStructureGet<int>(capsStruct, "width"_s).value_or(1920);
selector.stopCondition.height = gstStructureGet<int>(capsStruct, "height"_s).value_or(1080);
int numerator = 0;
int denominator = 1;
if (gst_structure_get_fraction(capsStruct, "framerate", &numerator, &denominator))
gst_util_fraction_to_double(numerator, denominator, &selector.stopCondition.frameRate);
else
selector.stopCondition.frameRate = 24;
GST_DEBUG_OBJECT(m_pipeline.get(), "Searching best video capture device mime type for resolution %dx%d@%.3f",
selector.stopCondition.width, selector.stopCondition.height, selector.stopCondition.frameRate);
auto deviceCaps = adoptGRef(gst_device_get_caps(m_device->device()));
gst_caps_foreach(deviceCaps.get(),
reinterpret_cast<GstCapsForeachFunc>(+[](GstCapsFeatures*, GstStructure* structure, MimeTypeSelector* selector) -> gboolean {
auto width = getMaxIntValueFromStructure(structure, "width");
if (!width.has_value())
return TRUE;
auto height = getMaxIntValueFromStructure(structure, "height");
if (!height.has_value())
return TRUE;
auto frameRate = getMaxFractionValueFromStructure(structure, "framerate");
if (!frameRate.has_value())
return TRUE;
if (*width >= selector->stopCondition.width && *height >= selector->stopCondition.height
&& *frameRate >= selector->stopCondition.frameRate) {
selector->maxWidth = *width;
selector->maxHeight = *height;
selector->maxFrameRate = *frameRate;
selector->mimeType = gstStructureGetName(structure).toString();
if (gst_structure_has_name(structure, "video/x-raw")) {
if (gst_structure_has_field(structure, "format"))
selector->format = makeString(gstStructureGetString(structure, "format"_s));
else
return TRUE;
}
return FALSE;
}
if (*width >= selector->maxWidth && *height >= selector->maxHeight && *frameRate >= selector->maxFrameRate) {
selector->maxWidth = *width;
selector->maxHeight = *height;
selector->maxFrameRate = *frameRate;
selector->mimeType = gstStructureGetName(structure).toString();
if (gst_structure_has_name(structure, "video/x-raw")) {
if (gst_structure_has_field(structure, "format"))
selector->format = makeString(gstStructureGetString(structure, "format"_s));
else
return TRUE;
}
}
return TRUE;
}), &selector);
auto caps = adoptGRef(gst_caps_new_simple(selector.mimeType.ascii().data(), "width", G_TYPE_INT, selector.maxWidth,
"height", G_TYPE_INT, selector.maxHeight, nullptr));
// Workaround for https://gitlab.freedesktop.org/pipewire/pipewire/-/issues/1793.
if (!selector.format.isEmpty())
gst_caps_set_simple(caps.get(), "format", G_TYPE_STRING, selector.format.ascii().data(), nullptr);
GST_INFO_OBJECT(m_pipeline.get(), "Setting video capture device caps to %" GST_PTR_FORMAT, caps.get());
g_object_set(m_videoSrcMIMETypeFilter.get(), "caps", caps.get(), nullptr);
}
#undef GST_CAT_DEFAULT
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM) && USE(GSTREAMER)
|