2020-11-20 18:20:36 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2020 Seungha Yang <seungha@centricular.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 along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-10-13 12:45:34 +00:00
|
|
|
* SECTION:element-d3d11screencapturesrc
|
|
|
|
* @title: d3d11screencapturesrc
|
2020-11-20 18:20:36 +00:00
|
|
|
*
|
2021-02-21 08:38:38 +00:00
|
|
|
* A DXGI Desktop Duplication API based screen capture element
|
2020-11-20 18:20:36 +00:00
|
|
|
*
|
2021-02-21 08:38:38 +00:00
|
|
|
* ## Example launch line
|
|
|
|
* ```
|
2021-10-13 12:45:34 +00:00
|
|
|
* gst-launch-1.0 d3d11screencapturesrc ! queue ! d3d11videosink
|
2021-02-21 08:38:38 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Since: 1.20
|
2020-11-20 18:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
#include "gstd3d11screencapturesrc.h"
|
|
|
|
#include "gstd3d11screencapture.h"
|
2021-01-19 17:29:43 +00:00
|
|
|
#include "gstd3d11pluginutils.h"
|
2021-10-05 12:49:38 +00:00
|
|
|
#include <wrl.h>
|
2020-11-20 18:20:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-10-05 12:49:38 +00:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_screen_capture_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_d3d11_screen_capture_debug
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_MONITOR_INDEX,
|
2021-10-05 12:49:38 +00:00
|
|
|
PROP_MONITOR_HANDLE,
|
2020-11-20 18:20:36 +00:00
|
|
|
PROP_SHOW_CURSOR,
|
|
|
|
|
|
|
|
PROP_LAST,
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[PROP_LAST];
|
|
|
|
|
|
|
|
#define DEFAULT_MONITOR_INDEX -1
|
|
|
|
#define DEFAULT_SHOW_CURSOR FALSE
|
|
|
|
|
2021-01-19 17:29:43 +00:00
|
|
|
static GstStaticCaps template_caps =
|
2021-10-05 18:19:30 +00:00
|
|
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
|
|
|
|
(GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, "BGRA") ";"
|
|
|
|
GST_VIDEO_CAPS_MAKE ("BGRA"));
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
struct _GstD3D11ScreenCaptureSrc
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-05-20 08:03:15 +00:00
|
|
|
GstBaseSrc src;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
guint64 last_frame_no;
|
|
|
|
GstClockID clock_id;
|
|
|
|
GstVideoInfo video_info;
|
|
|
|
|
|
|
|
GstD3D11Device *device;
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCapture *capture;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
GstBufferPool *pool;
|
|
|
|
|
2021-10-05 12:49:38 +00:00
|
|
|
gint64 adapter_luid;
|
2020-11-20 18:20:36 +00:00
|
|
|
gint monitor_index;
|
2021-10-05 12:49:38 +00:00
|
|
|
HMONITOR monitor_handle;
|
2020-11-20 18:20:36 +00:00
|
|
|
gboolean show_cursor;
|
|
|
|
|
|
|
|
gboolean flushing;
|
|
|
|
GstClockTime min_latency;
|
|
|
|
GstClockTime max_latency;
|
2021-10-05 18:19:30 +00:00
|
|
|
|
|
|
|
gboolean downstream_supports_d3d11;
|
2020-11-20 18:20:36 +00:00
|
|
|
};
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
static void gst_d3d11_screen_capture_src_dispose (GObject * object);
|
|
|
|
static void gst_d3d11_screen_capture_src_set_property (GObject * object,
|
2020-11-20 18:20:36 +00:00
|
|
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
2021-10-13 12:45:34 +00:00
|
|
|
static void gst_d3d11_screen_capture_src_get_property (GObject * object,
|
2020-11-20 18:20:36 +00:00
|
|
|
guint prop_id, GValue * value, GParamSpec * pspec);
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
static void gst_d3d11_screen_capture_src_set_context (GstElement * element,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstContext * context);
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
static GstCaps *gst_d3d11_screen_capture_src_get_caps (GstBaseSrc * bsrc,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstCaps * filter);
|
2021-10-13 12:45:34 +00:00
|
|
|
static GstCaps *gst_d3d11_screen_capture_src_fixate (GstBaseSrc * bsrc,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstCaps * caps);
|
2021-10-13 12:45:34 +00:00
|
|
|
static gboolean gst_d3d11_screen_capture_src_set_caps (GstBaseSrc * bsrc,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstCaps * caps);
|
2021-10-13 12:45:34 +00:00
|
|
|
static gboolean gst_d3d11_screen_capture_src_decide_allocation (GstBaseSrc *
|
|
|
|
bsrc, GstQuery * query);
|
|
|
|
static gboolean gst_d3d11_screen_capture_src_start (GstBaseSrc * bsrc);
|
|
|
|
static gboolean gst_d3d11_screen_capture_src_stop (GstBaseSrc * bsrc);
|
|
|
|
static gboolean gst_d3d11_screen_capture_src_unlock (GstBaseSrc * bsrc);
|
|
|
|
static gboolean gst_d3d11_screen_capture_src_unlock_stop (GstBaseSrc * bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_src_query (GstBaseSrc * bsrc, GstQuery * query);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
static GstFlowReturn gst_d3d11_screen_capture_src_create (GstBaseSrc * bsrc,
|
2021-05-20 08:03:15 +00:00
|
|
|
guint64 offset, guint size, GstBuffer ** buf);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
#define gst_d3d11_screen_capture_src_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstD3D11ScreenCaptureSrc, gst_d3d11_screen_capture_src,
|
2021-05-20 08:03:15 +00:00
|
|
|
GST_TYPE_BASE_SRC);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_class_init (GstD3D11ScreenCaptureSrcClass * klass)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
GstBaseSrcClass *basesrc_class = GST_BASE_SRC_CLASS (klass);
|
2021-01-19 17:29:43 +00:00
|
|
|
GstCaps *caps;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
gobject_class->dispose = gst_d3d11_screen_capture_src_dispose;
|
|
|
|
gobject_class->set_property = gst_d3d11_screen_capture_src_set_property;
|
|
|
|
gobject_class->get_property = gst_d3d11_screen_capture_src_get_property;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
properties[PROP_MONITOR_INDEX] =
|
|
|
|
g_param_spec_int ("monitor-index", "Monitor Index",
|
|
|
|
"Zero-based index for monitor to capture (-1 = primary monitor)",
|
|
|
|
-1, G_MAXINT, DEFAULT_MONITOR_INDEX,
|
2021-03-13 08:40:57 +00:00
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 12:49:38 +00:00
|
|
|
properties[PROP_MONITOR_HANDLE] =
|
|
|
|
g_param_spec_uint64 ("monitor-handle", "Monitor Handle",
|
|
|
|
"A HMONITOR handle of monitor to capture",
|
|
|
|
0, G_MAXUINT64, 0,
|
|
|
|
(GParamFlags) (G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
properties[PROP_SHOW_CURSOR] =
|
|
|
|
g_param_spec_boolean ("show-cursor",
|
|
|
|
"Show Mouse Cursor", "Whether to show mouse cursor",
|
2021-03-13 08:40:57 +00:00
|
|
|
DEFAULT_SHOW_CURSOR,
|
|
|
|
(GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
g_object_class_install_properties (gobject_class, PROP_LAST, properties);
|
|
|
|
|
|
|
|
element_class->set_context =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_set_context);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2021-10-13 12:45:34 +00:00
|
|
|
"Direct3D11 screen capture src", "Source/Video",
|
2020-11-20 18:20:36 +00:00
|
|
|
"Capture desktop image by using Desktop Duplication API",
|
|
|
|
"Seungha Yang <seungha@centricular.com>");
|
|
|
|
|
2021-01-19 17:29:43 +00:00
|
|
|
caps = gst_d3d11_get_updated_template_caps (&template_caps);
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
|
|
|
|
gst_caps_unref (caps);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
basesrc_class->get_caps =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_get_caps);
|
|
|
|
basesrc_class->fixate =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_fixate);
|
2020-11-20 18:20:36 +00:00
|
|
|
basesrc_class->set_caps =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_set_caps);
|
2020-11-20 18:20:36 +00:00
|
|
|
basesrc_class->decide_allocation =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_decide_allocation);
|
|
|
|
basesrc_class->start = GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_start);
|
|
|
|
basesrc_class->stop = GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_stop);
|
|
|
|
basesrc_class->unlock =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_unlock);
|
2020-11-20 18:20:36 +00:00
|
|
|
basesrc_class->unlock_stop =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_unlock_stop);
|
2020-11-20 18:20:36 +00:00
|
|
|
basesrc_class->query =
|
2021-10-13 12:45:34 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_src_query);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
basesrc_class->create =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_d3d11_screen_capture_src_create);
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_init (GstD3D11ScreenCaptureSrc * self)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
|
|
|
gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
|
|
|
|
gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
|
|
|
|
|
|
|
|
self->monitor_index = DEFAULT_MONITOR_INDEX;
|
|
|
|
self->show_cursor = DEFAULT_SHOW_CURSOR;
|
|
|
|
self->min_latency = GST_CLOCK_TIME_NONE;
|
|
|
|
self->max_latency = GST_CLOCK_TIME_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_dispose (GObject * object)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (object);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_clear_object (&self->capture);
|
2020-11-20 18:20:36 +00:00
|
|
|
gst_clear_object (&self->device);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_set_property (GObject * object, guint prop_id,
|
2020-11-20 18:20:36 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (object);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MONITOR_INDEX:
|
|
|
|
self->monitor_index = g_value_get_int (value);
|
|
|
|
break;
|
2021-10-05 12:49:38 +00:00
|
|
|
case PROP_MONITOR_HANDLE:
|
|
|
|
self->monitor_handle = (HMONITOR) g_value_get_uint64 (value);
|
|
|
|
break;
|
2020-11-20 18:20:36 +00:00
|
|
|
case PROP_SHOW_CURSOR:
|
|
|
|
self->show_cursor = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_get_property (GObject * object, guint prop_id,
|
2020-11-20 18:20:36 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (object);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MONITOR_INDEX:
|
|
|
|
g_value_set_int (value, self->monitor_index);
|
|
|
|
break;
|
2021-10-05 12:49:38 +00:00
|
|
|
case PROP_MONITOR_HANDLE:
|
|
|
|
g_value_set_uint64 (value, (guint64) self->monitor_handle);
|
|
|
|
break;
|
2020-11-20 18:20:36 +00:00
|
|
|
case PROP_SHOW_CURSOR:
|
|
|
|
g_value_set_boolean (value, self->show_cursor);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_set_context (GstElement * element,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstContext * context)
|
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (element);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 12:49:38 +00:00
|
|
|
gst_d3d11_handle_set_context_for_adapter_luid (element,
|
|
|
|
context, self->adapter_luid, &self->device);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_get_caps (GstBaseSrc * bsrc, GstCaps * filter)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
GstCaps *caps = NULL;
|
|
|
|
guint width, height;
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
if (!self->capture) {
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "Duplication object is not configured yet");
|
|
|
|
return gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (bsrc));
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
if (!gst_d3d11_screen_capture_get_size (self->capture, &width, &height)) {
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ,
|
|
|
|
("Cannot query supported resolution"), (NULL));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
caps = gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD (bsrc));
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
|
|
|
|
gst_caps_set_simple (caps, "width", G_TYPE_INT, width, "height",
|
|
|
|
G_TYPE_INT, height, nullptr);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
GstCaps *tmp =
|
|
|
|
gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
caps = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstCaps *
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-05 18:19:30 +00:00
|
|
|
guint size;
|
|
|
|
GstCaps *d3d11_caps = nullptr;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
caps = gst_caps_make_writable (caps);
|
2021-10-05 18:19:30 +00:00
|
|
|
size = gst_caps_get_size (caps);
|
|
|
|
|
|
|
|
for (guint i = 0; i < size; i++) {
|
|
|
|
GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_caps_get_structure (caps, i);
|
|
|
|
gst_structure_fixate_field_nearest_fraction (s, "framerate", 30, 1);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (!d3d11_caps) {
|
|
|
|
GstCapsFeatures *features;
|
|
|
|
features = gst_caps_get_features (caps, i);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (features && gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) {
|
|
|
|
|
|
|
|
d3d11_caps = gst_caps_new_empty ();
|
|
|
|
gst_caps_append_structure (d3d11_caps, gst_structure_copy (s));
|
|
|
|
|
|
|
|
gst_caps_set_features (d3d11_caps, 0,
|
|
|
|
gst_caps_features_new (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY,
|
|
|
|
nullptr));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (d3d11_caps) {
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
caps = d3d11_caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_caps_fixate (caps);
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_set_caps (GstBaseSrc * bsrc, GstCaps * caps)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2021-10-05 18:19:30 +00:00
|
|
|
GstCapsFeatures *features;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Set caps %" GST_PTR_FORMAT, caps);
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
features = gst_caps_get_features (caps, 0);
|
|
|
|
if (features && gst_caps_features_contains (features,
|
|
|
|
GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) {
|
|
|
|
self->downstream_supports_d3d11 = TRUE;
|
|
|
|
} else {
|
|
|
|
self->downstream_supports_d3d11 = FALSE;
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
gst_video_info_from_caps (&self->video_info, caps);
|
2021-10-05 18:19:30 +00:00
|
|
|
gst_base_src_set_blocksize (bsrc, GST_VIDEO_INFO_SIZE (&self->video_info));
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_decide_allocation (GstBaseSrc * bsrc,
|
2020-11-20 18:20:36 +00:00
|
|
|
GstQuery * query)
|
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
GstBufferPool *pool = NULL;
|
|
|
|
GstStructure *config;
|
|
|
|
GstD3D11AllocationParams *d3d11_params;
|
|
|
|
GstCaps *caps;
|
|
|
|
guint min, max, size;
|
|
|
|
gboolean update_pool;
|
|
|
|
GstVideoInfo vinfo;
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (self->pool) {
|
|
|
|
gst_buffer_pool_set_active (self->pool, FALSE);
|
|
|
|
gst_clear_object (&self->pool);
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
gst_query_parse_allocation (query, &caps, NULL);
|
|
|
|
|
|
|
|
if (!caps) {
|
|
|
|
GST_ERROR_OBJECT (self, "No output caps");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_info_from_caps (&vinfo, caps);
|
|
|
|
|
|
|
|
if (gst_query_get_n_allocation_pools (query) > 0) {
|
|
|
|
gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
|
|
|
|
update_pool = TRUE;
|
|
|
|
} else {
|
|
|
|
size = GST_VIDEO_INFO_SIZE (&vinfo);
|
|
|
|
|
|
|
|
min = max = 0;
|
|
|
|
update_pool = FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (pool && self->downstream_supports_d3d11) {
|
2021-04-23 09:44:41 +00:00
|
|
|
if (!GST_IS_D3D11_BUFFER_POOL (pool)) {
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
} else {
|
|
|
|
GstD3D11BufferPool *dpool = GST_D3D11_BUFFER_POOL (pool);
|
|
|
|
if (dpool->device != self->device)
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (!pool) {
|
|
|
|
if (self->downstream_supports_d3d11)
|
|
|
|
pool = gst_d3d11_buffer_pool_new (self->device);
|
|
|
|
else
|
|
|
|
pool = gst_video_buffer_pool_new ();
|
|
|
|
}
|
2021-04-23 09:44:41 +00:00
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min, max);
|
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (self->downstream_supports_d3d11) {
|
|
|
|
d3d11_params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
|
|
|
|
if (!d3d11_params) {
|
|
|
|
d3d11_params = gst_d3d11_allocation_params_new (self->device, &vinfo,
|
|
|
|
(GstD3D11AllocationFlags) 0,
|
|
|
|
D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET);
|
|
|
|
} else {
|
|
|
|
d3d11_params->desc[0].BindFlags |= D3D11_BIND_RENDER_TARGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
|
|
|
|
gst_d3d11_allocation_params_free (d3d11_params);
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (!gst_buffer_pool_set_config (pool, config)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to set config");
|
|
|
|
goto error;
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (self->downstream_supports_d3d11) {
|
|
|
|
/* d3d11 buffer pool will update buffer size based on allocated texture,
|
|
|
|
* get size from config again */
|
|
|
|
config = gst_buffer_pool_get_config (pool);
|
|
|
|
gst_buffer_pool_config_get_params (config,
|
|
|
|
nullptr, &size, nullptr, nullptr);
|
|
|
|
gst_structure_free (config);
|
|
|
|
} else {
|
|
|
|
self->pool = gst_d3d11_buffer_pool_new (self->device);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
config = gst_buffer_pool_get_config (self->pool);
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
|
|
|
|
gst_buffer_pool_config_add_option (config,
|
|
|
|
GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
|
|
|
|
d3d11_params = gst_buffer_pool_config_get_d3d11_allocation_params (config);
|
|
|
|
if (!d3d11_params) {
|
|
|
|
d3d11_params = gst_d3d11_allocation_params_new (self->device, &vinfo,
|
|
|
|
(GstD3D11AllocationFlags) 0, D3D11_BIND_RENDER_TARGET);
|
|
|
|
} else {
|
|
|
|
d3d11_params->desc[0].BindFlags |= D3D11_BIND_RENDER_TARGET;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
|
|
|
|
gst_d3d11_allocation_params_free (d3d11_params);
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_set_config (self->pool, config)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to set config for internal pool");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_buffer_pool_set_active (self->pool, TRUE)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to activate internal pool");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
if (update_pool)
|
|
|
|
gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
|
|
|
|
else
|
|
|
|
gst_query_add_allocation_pool (query, pool, size, min, max);
|
|
|
|
|
|
|
|
gst_object_unref (pool);
|
|
|
|
|
|
|
|
return TRUE;
|
2021-10-05 18:19:30 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
gst_clear_object (&self->pool);
|
|
|
|
gst_clear_object (&pool);
|
|
|
|
|
|
|
|
return FALSE;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_start (GstBaseSrc * bsrc)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2021-04-29 12:44:07 +00:00
|
|
|
GstFlowReturn ret;
|
2021-10-05 12:49:38 +00:00
|
|
|
HMONITOR monitor = self->monitor_handle;
|
|
|
|
ComPtr < IDXGIAdapter1 > adapter;
|
|
|
|
DXGI_ADAPTER_DESC desc;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (monitor) {
|
2021-10-13 12:45:34 +00:00
|
|
|
hr = gst_d3d11_screen_capture_find_output_for_monitor (monitor,
|
2021-10-05 12:49:38 +00:00
|
|
|
&adapter, nullptr);
|
|
|
|
} else if (self->monitor_index < 0) {
|
2021-10-13 12:45:34 +00:00
|
|
|
hr = gst_d3d11_screen_capture_find_primary_monitor (&monitor,
|
2021-10-05 12:49:38 +00:00
|
|
|
&adapter, nullptr);
|
|
|
|
} else {
|
2021-10-13 12:45:34 +00:00
|
|
|
hr = gst_d3d11_screen_capture_find_nth_monitor (self->monitor_index,
|
2021-10-05 12:49:38 +00:00
|
|
|
&monitor, &adapter, nullptr);
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 12:49:38 +00:00
|
|
|
if (FAILED (hr))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
hr = adapter->GetDesc (&desc);
|
|
|
|
if (FAILED (hr))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
self->adapter_luid = gst_d3d11_luid_to_int64 (&desc.AdapterLuid);
|
|
|
|
gst_clear_object (&self->device);
|
|
|
|
|
|
|
|
if (!gst_d3d11_ensure_element_data_for_adapter_luid (GST_ELEMENT_CAST (self),
|
|
|
|
self->adapter_luid, &self->device)) {
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
|
2021-10-05 12:49:38 +00:00
|
|
|
("D3D11 device for LUID %" G_GINT64_FORMAT " is unavailble",
|
|
|
|
self->adapter_luid), (nullptr));
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
self->capture = gst_d3d11_screen_capture_new (self->device, monitor);
|
|
|
|
if (!self->capture)
|
2021-04-29 12:44:07 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* Check if we can open device */
|
2021-10-13 12:45:34 +00:00
|
|
|
ret = gst_d3d11_screen_capture_prepare (self->capture);
|
2021-04-29 12:44:07 +00:00
|
|
|
switch (ret) {
|
2021-10-13 12:45:34 +00:00
|
|
|
case GST_D3D11_SCREEN_CAPTURE_FLOW_EXPECTED_ERROR:
|
2021-04-29 12:44:07 +00:00
|
|
|
case GST_FLOW_OK:
|
|
|
|
break;
|
2021-10-13 12:45:34 +00:00
|
|
|
case GST_D3D11_SCREEN_CAPTURE_FLOW_UNSUPPORTED:
|
2021-04-29 12:44:07 +00:00
|
|
|
goto unsupported;
|
|
|
|
default:
|
|
|
|
goto error;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self->last_frame_no = -1;
|
|
|
|
self->min_latency = self->max_latency = GST_CLOCK_TIME_NONE;
|
|
|
|
|
|
|
|
return TRUE;
|
2021-04-29 12:44:07 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
|
2021-10-13 12:45:34 +00:00
|
|
|
("Failed to prepare capture object with given configuration, "
|
2021-10-05 12:49:38 +00:00
|
|
|
"monitor-index: %d, monitor-handle: %p",
|
|
|
|
self->monitor_index, self->monitor_handle), (nullptr));
|
|
|
|
return FALSE;
|
2021-04-29 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsupported:
|
|
|
|
{
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ,
|
2021-10-13 12:45:34 +00:00
|
|
|
("Failed to prepare capture object with given configuration, "
|
2021-10-05 12:49:38 +00:00
|
|
|
"monitor-index: %d, monitor-handle: %p",
|
|
|
|
self->monitor_index, self->monitor_handle),
|
2021-04-29 12:44:07 +00:00
|
|
|
("Try run the application on the integrated GPU"));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_stop (GstBaseSrc * bsrc)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (self->pool) {
|
|
|
|
gst_buffer_pool_set_active (self->pool, FALSE);
|
|
|
|
gst_clear_object (&self->pool);
|
|
|
|
}
|
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_clear_object (&self->capture);
|
2020-11-20 18:20:36 +00:00
|
|
|
gst_clear_object (&self->device);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_unlock (GstBaseSrc * bsrc)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->clock_id) {
|
|
|
|
GST_DEBUG_OBJECT (self, "Waking up waiting clock");
|
|
|
|
gst_clock_id_unschedule (self->clock_id);
|
|
|
|
}
|
|
|
|
self->flushing = TRUE;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_unlock_stop (GstBaseSrc * bsrc)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
self->flushing = FALSE;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_src_query (GstBaseSrc * bsrc, GstQuery * query)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CONTEXT:
|
|
|
|
if (gst_d3d11_handle_context_query (GST_ELEMENT_CAST (self), query,
|
|
|
|
self->device)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GST_QUERY_LATENCY:
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (self->min_latency)) {
|
|
|
|
gst_query_set_latency (query,
|
|
|
|
TRUE, self->min_latency, self->max_latency);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2021-10-13 12:45:34 +00:00
|
|
|
gst_d3d11_screen_capture_src_create (GstBaseSrc * bsrc, guint64 offset,
|
|
|
|
guint size, GstBuffer ** buf)
|
2020-11-20 18:20:36 +00:00
|
|
|
{
|
2021-10-13 12:45:34 +00:00
|
|
|
GstD3D11ScreenCaptureSrc *self = GST_D3D11_SCREEN_CAPTURE_SRC (bsrc);
|
2020-11-20 18:20:36 +00:00
|
|
|
ID3D11Texture2D *texture;
|
|
|
|
ID3D11RenderTargetView *rtv = NULL;
|
|
|
|
gint fps_n, fps_d;
|
|
|
|
GstMapInfo info;
|
|
|
|
GstMemory *mem;
|
|
|
|
GstD3D11Memory *dmem;
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstClock *clock = NULL;
|
|
|
|
GstClockTime base_time;
|
|
|
|
GstClockTime next_capture_ts;
|
|
|
|
GstClockTime before_capture;
|
|
|
|
GstClockTime after_capture;
|
|
|
|
GstClockTime latency;
|
|
|
|
GstClockTime dur;
|
|
|
|
gboolean update_latency = FALSE;
|
2021-03-13 08:40:57 +00:00
|
|
|
guint64 next_frame_no;
|
2020-11-20 18:20:36 +00:00
|
|
|
gboolean draw_mouse;
|
2021-04-29 12:44:07 +00:00
|
|
|
/* Just magic number... */
|
|
|
|
gint unsupported_retry_count = 100;
|
2021-05-20 08:03:15 +00:00
|
|
|
GstBuffer *buffer = NULL;
|
2021-10-05 18:19:30 +00:00
|
|
|
GstBuffer *sysmem_buf = NULL;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
2021-10-13 12:45:34 +00:00
|
|
|
if (!self->capture) {
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ,
|
2021-10-13 12:45:34 +00:00
|
|
|
("Couldn't configure capture object"), (nullptr));
|
2020-11-20 18:20:36 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
}
|
|
|
|
|
|
|
|
fps_n = GST_VIDEO_INFO_FPS_N (&self->video_info);
|
|
|
|
fps_d = GST_VIDEO_INFO_FPS_D (&self->video_info);
|
|
|
|
|
|
|
|
if (fps_n <= 0 || fps_d <= 0)
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
|
|
|
again:
|
|
|
|
clock = gst_element_get_clock (GST_ELEMENT_CAST (self));
|
|
|
|
if (!clock) {
|
|
|
|
GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
|
2021-10-13 12:45:34 +00:00
|
|
|
("Cannot operate without a clock"), (nullptr));
|
2020-11-20 18:20:36 +00:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check flushing before waiting clock because we are might be doing
|
|
|
|
* retry */
|
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
if (self->flushing) {
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
base_time = GST_ELEMENT_CAST (self)->base_time;
|
|
|
|
next_capture_ts = gst_clock_get_time (clock);
|
|
|
|
next_capture_ts -= base_time;
|
|
|
|
|
|
|
|
next_frame_no = gst_util_uint64_scale (next_capture_ts,
|
|
|
|
fps_n, GST_SECOND * fps_d);
|
|
|
|
|
|
|
|
if (next_frame_no == self->last_frame_no) {
|
|
|
|
GstClockID id;
|
2021-03-13 08:40:57 +00:00
|
|
|
GstClockReturn clock_ret;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
/* Need to wait for the next frame */
|
|
|
|
next_frame_no += 1;
|
|
|
|
|
|
|
|
/* Figure out what the next frame time is */
|
|
|
|
next_capture_ts = gst_util_uint64_scale (next_frame_no,
|
|
|
|
fps_d * GST_SECOND, fps_n);
|
|
|
|
|
|
|
|
id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (self),
|
|
|
|
next_capture_ts + base_time);
|
|
|
|
self->clock_id = id;
|
|
|
|
|
|
|
|
/* release the object lock while waiting */
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "Waiting for next frame time %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (next_capture_ts));
|
2021-03-13 08:40:57 +00:00
|
|
|
clock_ret = gst_clock_id_wait (id, NULL);
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_OBJECT_LOCK (self);
|
|
|
|
|
|
|
|
gst_clock_id_unref (id);
|
|
|
|
self->clock_id = NULL;
|
|
|
|
|
2021-03-13 08:40:57 +00:00
|
|
|
if (clock_ret == GST_CLOCK_UNSCHEDULED) {
|
2020-11-20 18:20:36 +00:00
|
|
|
/* Got woken up by the unlock function */
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* Duration is a complete 1/fps frame duration */
|
|
|
|
dur = gst_util_uint64_scale_int (GST_SECOND, fps_d, fps_n);
|
|
|
|
} else {
|
|
|
|
GstClockTime next_frame_ts;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (self, "No need to wait for next frame time %"
|
|
|
|
GST_TIME_FORMAT " next frame = %" G_GINT64_FORMAT " prev = %"
|
|
|
|
G_GINT64_FORMAT, GST_TIME_ARGS (next_capture_ts), next_frame_no,
|
|
|
|
self->last_frame_no);
|
|
|
|
|
|
|
|
next_frame_ts = gst_util_uint64_scale (next_frame_no + 1,
|
|
|
|
fps_d * GST_SECOND, fps_n);
|
|
|
|
/* Frame duration is from now until the next expected capture time */
|
|
|
|
dur = next_frame_ts - next_capture_ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->last_frame_no = next_frame_no;
|
|
|
|
GST_OBJECT_UNLOCK (self);
|
|
|
|
|
2021-05-20 08:03:15 +00:00
|
|
|
if (!buffer) {
|
2021-10-05 18:19:30 +00:00
|
|
|
if (self->downstream_supports_d3d11) {
|
|
|
|
ret = GST_BASE_SRC_CLASS (parent_class)->alloc (bsrc,
|
|
|
|
offset, size, &buffer);
|
|
|
|
} else {
|
|
|
|
if (!self->pool) {
|
|
|
|
GST_ERROR_OBJECT (self, "Internal pool wasn't configured");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gst_buffer_pool_acquire_buffer (self->pool, &buffer, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-05-20 08:03:15 +00:00
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
mem = gst_buffer_peek_memory (buffer, 0);
|
2021-10-05 18:19:30 +00:00
|
|
|
if (!gst_is_d3d11_memory (mem)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Not a D3D11 memory");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
dmem = (GstD3D11Memory *) mem;
|
|
|
|
draw_mouse = self->show_cursor;
|
|
|
|
rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
|
|
|
|
if (draw_mouse && !rtv) {
|
|
|
|
GST_ERROR_OBJECT (self, "Render target view is unavailable");
|
2021-10-05 18:19:30 +00:00
|
|
|
goto error;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 08:40:57 +00:00
|
|
|
if (!gst_memory_map (mem, &info,
|
|
|
|
(GstMapFlags) (GST_MAP_WRITE | GST_MAP_D3D11))) {
|
2020-11-20 18:20:36 +00:00
|
|
|
GST_ERROR_OBJECT (self, "Failed to map d3d11 memory");
|
2021-10-05 18:19:30 +00:00
|
|
|
goto error;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
texture = (ID3D11Texture2D *) info.data;
|
|
|
|
before_capture = gst_clock_get_time (clock);
|
2021-10-13 12:45:34 +00:00
|
|
|
ret =
|
|
|
|
gst_d3d11_screen_capture_do_capture (self->capture, texture, rtv,
|
|
|
|
draw_mouse);
|
2020-11-20 18:20:36 +00:00
|
|
|
gst_memory_unmap (mem, &info);
|
|
|
|
|
2021-05-20 08:03:15 +00:00
|
|
|
switch (ret) {
|
2021-10-13 12:45:34 +00:00
|
|
|
case GST_D3D11_SCREEN_CAPTURE_FLOW_EXPECTED_ERROR:
|
2021-05-20 08:03:15 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Got expected error, try again");
|
|
|
|
gst_clear_object (&clock);
|
|
|
|
goto again;
|
2021-10-13 12:45:34 +00:00
|
|
|
case GST_D3D11_SCREEN_CAPTURE_FLOW_UNSUPPORTED:
|
2021-05-20 08:03:15 +00:00
|
|
|
GST_WARNING_OBJECT (self, "Got DXGI_ERROR_UNSUPPORTED error");
|
|
|
|
unsupported_retry_count--;
|
2021-04-29 12:44:07 +00:00
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (unsupported_retry_count < 0)
|
|
|
|
goto error;
|
|
|
|
|
2021-05-20 08:03:15 +00:00
|
|
|
gst_clear_object (&clock);
|
|
|
|
goto again;
|
2021-10-13 12:45:34 +00:00
|
|
|
case GST_D3D11_SCREEN_CAPTURE_FLOW_SIZE_CHANGED:
|
2021-05-20 08:03:15 +00:00
|
|
|
GST_INFO_OBJECT (self, "Size was changed, need negotiation");
|
|
|
|
gst_clear_buffer (&buffer);
|
|
|
|
gst_clear_object (&clock);
|
|
|
|
|
|
|
|
if (!gst_base_src_negotiate (bsrc)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to negotiate with new size");
|
|
|
|
ret = GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
default:
|
|
|
|
break;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:19:30 +00:00
|
|
|
if (!self->downstream_supports_d3d11) {
|
|
|
|
GstVideoFrame src_frame, dst_frame;
|
|
|
|
gboolean copy_ret;
|
|
|
|
|
|
|
|
ret = GST_BASE_SRC_CLASS (parent_class)->alloc (bsrc,
|
|
|
|
offset, size, &sysmem_buf);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
gst_clear_buffer (&buffer);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&src_frame, &self->video_info, buffer,
|
|
|
|
GST_MAP_READ)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to map d3d11 buffer");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&dst_frame, &self->video_info, sysmem_buf,
|
|
|
|
GST_MAP_WRITE)) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to map sysmem buffer");
|
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_ret = gst_video_frame_copy (&dst_frame, &src_frame);
|
|
|
|
gst_video_frame_unmap (&dst_frame);
|
|
|
|
gst_video_frame_unmap (&src_frame);
|
|
|
|
|
|
|
|
if (!copy_ret) {
|
|
|
|
GST_ERROR_OBJECT (self, "Failed to copy frame");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_buffer_unref (buffer);
|
|
|
|
buffer = sysmem_buf;
|
|
|
|
sysmem_buf = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
|
|
|
|
GST_BUFFER_PTS (buffer) = next_capture_ts;
|
|
|
|
GST_BUFFER_DURATION (buffer) = dur;
|
|
|
|
|
2020-11-20 18:20:36 +00:00
|
|
|
after_capture = gst_clock_get_time (clock);
|
|
|
|
latency = after_capture - before_capture;
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (self->min_latency)) {
|
|
|
|
self->min_latency = self->max_latency = latency;
|
|
|
|
update_latency = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (self, "Initial latency %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (latency));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (latency > self->max_latency) {
|
|
|
|
self->max_latency = latency;
|
|
|
|
update_latency = TRUE;
|
|
|
|
GST_DEBUG_OBJECT (self, "Updating max latency %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (latency));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update_latency) {
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (self),
|
|
|
|
gst_message_new_latency (GST_OBJECT_CAST (self)));
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
gst_clear_object (&clock);
|
2021-05-20 08:03:15 +00:00
|
|
|
*buf = buffer;
|
2020-11-20 18:20:36 +00:00
|
|
|
|
|
|
|
return ret;
|
2021-10-05 18:19:30 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
gst_clear_buffer (&buffer);
|
|
|
|
gst_clear_buffer (&sysmem_buf);
|
|
|
|
gst_clear_object (&clock);
|
|
|
|
|
|
|
|
return GST_FLOW_ERROR;
|
2020-11-20 18:20:36 +00:00
|
|
|
}
|