2 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 * SECTION:element-d3d11videosink
23 * @title: d3d11videosink
25 * Direct3D11 based video render element
27 * ## Example launch line
29 * gst-launch-1.0 videotestsrc ! d3d11upload ! d3d11videosink
31 * This pipeline will display test video stream on screen via d3d11videosink
41 #include "gstd3d11videosink.h"
42 #include "gstd3d11videoprocessor.h"
43 #include "gstd3d11pluginutils.h"
46 #if GST_D3D11_WINAPI_APP
47 #include "gstd3d11window_corewindow.h"
48 #include "gstd3d11window_swapchainpanel.h"
50 #if (!GST_D3D11_WINAPI_ONLY_APP)
51 #include "gstd3d11window_win32.h"
53 #include "gstd3d11window_dummy.h"
59 PROP_FORCE_ASPECT_RATIO,
60 PROP_ENABLE_NAVIGATION_EVENTS,
61 PROP_FULLSCREEN_TOGGLE_MODE,
63 PROP_DRAW_ON_SHARED_TEXTURE,
67 #define DEFAULT_ADAPTER -1
68 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
69 #define DEFAULT_ENABLE_NAVIGATION_EVENTS TRUE
70 #define DEFAULT_FULLSCREEN_TOGGLE_MODE GST_D3D11_WINDOW_FULLSCREEN_TOGGLE_MODE_NONE
71 #define DEFAULT_FULLSCREEN FALSE
72 #define DEFAULT_DRAW_ON_SHARED_TEXTURE FALSE
85 static guint gst_d3d11_video_sink_signals[LAST_SIGNAL] = { 0, };
87 static GstStaticCaps pad_template_caps =
88 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
89 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY, GST_D3D11_SINK_FORMATS) "; "
90 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
91 (GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY ","
92 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
93 GST_D3D11_SINK_FORMATS) ";"
94 GST_VIDEO_CAPS_MAKE (GST_D3D11_SINK_FORMATS) "; "
95 GST_VIDEO_CAPS_MAKE_WITH_FEATURES
96 (GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY ","
97 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION,
98 GST_D3D11_SINK_FORMATS));
100 GST_DEBUG_CATEGORY (d3d11_video_sink_debug);
101 #define GST_CAT_DEFAULT d3d11_video_sink_debug
103 struct _GstD3D11VideoSink
106 GstD3D11Device *device;
107 GstD3D11Window *window;
115 gboolean caps_updated;
119 gboolean force_aspect_ratio;
120 gboolean enable_navigation_events;
121 GstD3D11WindowFullscreenToggleMode fullscreen_toggle_mode;
123 gboolean draw_on_shared_texture;
125 /* saved render rectangle until we have a window */
126 GstVideoRectangle render_rect;
127 gboolean pending_render_rect;
129 /* For drawing on user texture */
131 GstBuffer *current_buffer;
136 /* method configured via property */
137 GstVideoOrientationMethod method;
138 /* method parsed from tag */
139 GstVideoOrientationMethod tag_method;
140 /* method currently selected based on "method" and "tag_method" */
141 GstVideoOrientationMethod selected_method;
144 #define GST_D3D11_VIDEO_SINK_GET_LOCK(d) (&(GST_D3D11_VIDEO_SINK_CAST(d)->lock))
145 #define GST_D3D11_VIDEO_SINK_LOCK(d) G_STMT_START { \
146 GST_TRACE_OBJECT (d, "Locking from thread %p", g_thread_self()); \
147 g_rec_mutex_lock (GST_D3D11_VIDEO_SINK_GET_LOCK (d)); \
148 GST_TRACE_OBJECT (d, "Locked from thread %p", g_thread_self()); \
151 #define GST_D3D11_VIDEO_SINK_UNLOCK(d) G_STMT_START { \
152 GST_TRACE_OBJECT (d, "Unlocking from thread %p", g_thread_self()); \
153 g_rec_mutex_unlock (GST_D3D11_VIDEO_SINK_GET_LOCK (d)); \
156 static void gst_d3d11_videosink_set_property (GObject * object, guint prop_id,
157 const GValue * value, GParamSpec * pspec);
158 static void gst_d3d11_videosink_get_property (GObject * object, guint prop_id,
159 GValue * value, GParamSpec * pspec);
160 static void gst_d3d11_video_sink_finalize (GObject * object);
162 gst_d3d11_video_sink_draw_action (GstD3D11VideoSink * self,
163 gpointer shared_handle, guint texture_misc_flags, guint64 acquire_key,
164 guint64 release_key);
167 gst_d3d11_video_sink_video_overlay_init (GstVideoOverlayInterface * iface);
169 gst_d3d11_video_sink_navigation_init (GstNavigationInterface * iface);
171 static void gst_d3d11_video_sink_set_context (GstElement * element,
172 GstContext * context);
173 static GstCaps *gst_d3d11_video_sink_get_caps (GstBaseSink * sink,
175 static gboolean gst_d3d11_video_sink_set_caps (GstBaseSink * sink,
178 static gboolean gst_d3d11_video_sink_start (GstBaseSink * sink);
179 static gboolean gst_d3d11_video_sink_stop (GstBaseSink * sink);
180 static gboolean gst_d3d11_video_sink_propose_allocation (GstBaseSink * sink,
182 static gboolean gst_d3d11_video_sink_query (GstBaseSink * sink,
184 static gboolean gst_d3d11_video_sink_unlock (GstBaseSink * sink);
185 static gboolean gst_d3d11_video_sink_unlock_stop (GstBaseSink * sink);
186 static gboolean gst_d3d11_video_sink_event (GstBaseSink * sink,
189 gst_d3d11_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf);
190 static gboolean gst_d3d11_video_sink_prepare_window (GstD3D11VideoSink * self);
191 static void gst_d3d11_video_sink_set_orientation (GstD3D11VideoSink * self,
192 GstVideoOrientationMethod method, gboolean from_tag);
194 #define gst_d3d11_video_sink_parent_class parent_class
195 G_DEFINE_TYPE_WITH_CODE (GstD3D11VideoSink, gst_d3d11_video_sink,
197 G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
198 gst_d3d11_video_sink_video_overlay_init);
199 G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
200 gst_d3d11_video_sink_navigation_init);
201 GST_DEBUG_CATEGORY_INIT (d3d11_video_sink_debug,
202 "d3d11videosink", 0, "Direct3D11 Video Sink"));
205 gst_d3d11_video_sink_class_init (GstD3D11VideoSinkClass * klass)
207 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
208 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
209 GstBaseSinkClass *basesink_class = GST_BASE_SINK_CLASS (klass);
210 GstVideoSinkClass *videosink_class = GST_VIDEO_SINK_CLASS (klass);
213 gobject_class->set_property = gst_d3d11_videosink_set_property;
214 gobject_class->get_property = gst_d3d11_videosink_get_property;
215 gobject_class->finalize = gst_d3d11_video_sink_finalize;
217 g_object_class_install_property (gobject_class, PROP_ADAPTER,
218 g_param_spec_int ("adapter", "Adapter",
219 "Adapter index for creating device (-1 for default)",
220 -1, G_MAXINT32, DEFAULT_ADAPTER,
221 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
222 G_PARAM_STATIC_STRINGS)));
224 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
225 g_param_spec_boolean ("force-aspect-ratio",
226 "Force aspect ratio",
227 "When enabled, scaling will respect original aspect ratio",
228 DEFAULT_FORCE_ASPECT_RATIO,
229 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
231 g_object_class_install_property (gobject_class, PROP_ENABLE_NAVIGATION_EVENTS,
232 g_param_spec_boolean ("enable-navigation-events",
233 "Enable navigation events",
234 "When enabled, navigation events are sent upstream",
235 DEFAULT_ENABLE_NAVIGATION_EVENTS,
236 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
238 g_object_class_install_property (gobject_class, PROP_FULLSCREEN_TOGGLE_MODE,
239 g_param_spec_flags ("fullscreen-toggle-mode",
240 "Full screen toggle mode",
241 "Full screen toggle mode used to trigger fullscreen mode change",
242 GST_D3D11_WINDOW_TOGGLE_MODE_GET_TYPE, DEFAULT_FULLSCREEN_TOGGLE_MODE,
243 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
245 g_object_class_install_property (gobject_class, PROP_FULLSCREEN,
246 g_param_spec_boolean ("fullscreen",
248 "Ignored when \"fullscreen-toggle-mode\" does not include \"property\"",
250 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
253 * GstD3D11VideoSink:draw-on-shared-texture:
255 * Instruct the sink to draw on a shared texture provided by user.
256 * User must watch #d3d11videosink::begin-draw signal and should call
257 * #d3d11videosink::draw method on the #d3d11videosink::begin-draw
260 * Currently supported formats for user texture are:
261 * - DXGI_FORMAT_R8G8B8A8_UNORM
262 * - DXGI_FORMAT_B8G8R8A8_UNORM
263 * - DXGI_FORMAT_R10G10B10A2_UNORM
267 g_object_class_install_property (gobject_class, PROP_DRAW_ON_SHARED_TEXTURE,
268 g_param_spec_boolean ("draw-on-shared-texture",
269 "Draw on shared texture",
270 "Draw on user provided shared texture instead of window. "
271 "When enabled, user can pass application's own texture to sink "
272 "by using \"draw\" action signal on \"begin-draw\" signal handler, "
273 "so that sink can draw video data on application's texture. "
274 "Supported texture formats for user texture are "
275 "DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, and "
276 "DXGI_FORMAT_R10G10B10A2_UNORM.",
277 DEFAULT_DRAW_ON_SHARED_TEXTURE,
278 (GParamFlags) (G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY |
279 G_PARAM_STATIC_STRINGS)));
282 * GstD3D11VideoSink:rotate-method:
284 * Video rotation/flip method to use
288 g_object_class_install_property (gobject_class, PROP_ROTATE_METHOD,
289 g_param_spec_enum ("rotate-method", "Rotate Method",
290 "Rotate method to use",
291 GST_TYPE_VIDEO_ORIENTATION_METHOD, GST_VIDEO_ORIENTATION_IDENTITY,
292 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
295 * GstD3D11VideoSink::begin-draw:
296 * @videosink: the #d3d11videosink
298 * Emitted when sink has a texture to draw. Application needs to invoke
299 * #d3d11videosink::draw action signal before returning from
300 * #d3d11videosink::begin-draw signal handler.
304 gst_d3d11_video_sink_signals[SIGNAL_BEGIN_DRAW] =
305 g_signal_new ("begin-draw", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
306 G_STRUCT_OFFSET (GstD3D11VideoSinkClass, begin_draw),
307 NULL, NULL, NULL, G_TYPE_NONE, 0, G_TYPE_NONE);
310 * GstD3D11VideoSink::draw:
311 * @videosink: the #d3d11videosink
312 * @shard_handle: a pointer to HANDLE
313 * @texture_misc_flags: a D3D11_RESOURCE_MISC_FLAG value
314 * @acquire_key: a key value used for IDXGIKeyedMutex::AcquireSync
315 * @release_key: a key value used for IDXGIKeyedMutex::ReleaseSync
317 * Draws on a shared texture. @shard_handle must be a valid pointer to
318 * a HANDLE which was obtained via IDXGIResource::GetSharedHandle or
319 * IDXGIResource1::CreateSharedHandle.
321 * If the texture was created with D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX flag,
322 * caller must specify valid @acquire_key and @release_key.
323 * Otherwise (i.e., created with D3D11_RESOURCE_MISC_SHARED flag),
324 * @acquire_key and @release_key will be ignored.
328 gst_d3d11_video_sink_signals[SIGNAL_DRAW] =
329 g_signal_new ("draw", G_TYPE_FROM_CLASS (klass),
330 (GSignalFlags) (G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
331 G_STRUCT_OFFSET (GstD3D11VideoSinkClass, draw), NULL, NULL, NULL,
332 G_TYPE_BOOLEAN, 4, G_TYPE_POINTER, G_TYPE_UINT, G_TYPE_UINT64,
335 element_class->set_context =
336 GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_set_context);
338 gst_element_class_set_static_metadata (element_class,
339 "Direct3D11 video sink", "Sink/Video",
340 "A Direct3D11 based videosink",
341 "Seungha Yang <seungha.yang@navercorp.com>");
343 caps = gst_d3d11_get_updated_template_caps (&pad_template_caps);
344 gst_element_class_add_pad_template (element_class,
345 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
346 gst_caps_unref (caps);
348 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_get_caps);
349 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_set_caps);
350 basesink_class->start = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_start);
351 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_stop);
352 basesink_class->propose_allocation =
353 GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_propose_allocation);
354 basesink_class->query = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_query);
355 basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_unlock);
356 basesink_class->unlock_stop =
357 GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_unlock_stop);
358 basesink_class->event = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_event);
360 videosink_class->show_frame =
361 GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_show_frame);
363 klass->draw = gst_d3d11_video_sink_draw_action;
365 gst_type_mark_as_plugin_api (GST_D3D11_WINDOW_TOGGLE_MODE_GET_TYPE,
366 (GstPluginAPIFlags) 0);
370 gst_d3d11_video_sink_init (GstD3D11VideoSink * self)
372 self->adapter = DEFAULT_ADAPTER;
373 self->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
374 self->enable_navigation_events = DEFAULT_ENABLE_NAVIGATION_EVENTS;
375 self->fullscreen_toggle_mode = DEFAULT_FULLSCREEN_TOGGLE_MODE;
376 self->fullscreen = DEFAULT_FULLSCREEN;
377 self->draw_on_shared_texture = DEFAULT_DRAW_ON_SHARED_TEXTURE;
379 g_rec_mutex_init (&self->lock);
383 gst_d3d11_videosink_set_property (GObject * object, guint prop_id,
384 const GValue * value, GParamSpec * pspec)
386 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object);
388 GST_D3D11_VIDEO_SINK_LOCK (self);
391 self->adapter = g_value_get_int (value);
393 case PROP_FORCE_ASPECT_RATIO:
394 self->force_aspect_ratio = g_value_get_boolean (value);
396 g_object_set (self->window,
397 "force-aspect-ratio", self->force_aspect_ratio, NULL);
399 case PROP_ENABLE_NAVIGATION_EVENTS:
400 self->enable_navigation_events = g_value_get_boolean (value);
402 g_object_set (self->window,
403 "enable-navigation-events", self->enable_navigation_events, NULL);
406 case PROP_FULLSCREEN_TOGGLE_MODE:
407 self->fullscreen_toggle_mode =
408 (GstD3D11WindowFullscreenToggleMode) g_value_get_flags (value);
410 g_object_set (self->window,
411 "fullscreen-toggle-mode", self->fullscreen_toggle_mode, NULL);
414 case PROP_FULLSCREEN:
415 self->fullscreen = g_value_get_boolean (value);
417 g_object_set (self->window, "fullscreen", self->fullscreen, NULL);
420 case PROP_DRAW_ON_SHARED_TEXTURE:
421 self->draw_on_shared_texture = g_value_get_boolean (value);
423 case PROP_ROTATE_METHOD:
424 gst_d3d11_video_sink_set_orientation (self,
425 (GstVideoOrientationMethod) g_value_get_enum (value), FALSE);
428 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
431 GST_D3D11_VIDEO_SINK_UNLOCK (self);
435 gst_d3d11_videosink_get_property (GObject * object, guint prop_id,
436 GValue * value, GParamSpec * pspec)
438 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object);
440 GST_D3D11_VIDEO_SINK_LOCK (self);
443 g_value_set_int (value, self->adapter);
445 case PROP_FORCE_ASPECT_RATIO:
446 g_value_set_boolean (value, self->force_aspect_ratio);
448 case PROP_ENABLE_NAVIGATION_EVENTS:
449 g_value_set_boolean (value, self->enable_navigation_events);
451 case PROP_FULLSCREEN_TOGGLE_MODE:
452 g_value_set_flags (value, self->fullscreen_toggle_mode);
454 case PROP_FULLSCREEN:
456 g_object_get_property (G_OBJECT (self->window), pspec->name, value);
458 g_value_set_boolean (value, self->fullscreen);
461 case PROP_DRAW_ON_SHARED_TEXTURE:
462 g_value_set_boolean (value, self->draw_on_shared_texture);
464 case PROP_ROTATE_METHOD:
465 g_value_set_enum (value, self->method);
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
471 GST_D3D11_VIDEO_SINK_UNLOCK (self);
475 gst_d3d11_video_sink_finalize (GObject * object)
477 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object);
479 g_rec_mutex_clear (&self->lock);
480 g_free (self->title);
482 G_OBJECT_CLASS (parent_class)->finalize (object);
486 gst_d3d11_video_sink_set_context (GstElement * element, GstContext * context)
488 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (element);
490 gst_d3d11_handle_set_context (element, context, self->adapter, &self->device);
492 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
496 gst_d3d11_video_sink_get_caps (GstBaseSink * sink, GstCaps * filter)
498 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
499 GstCaps *caps = NULL;
501 caps = gst_pad_get_pad_template_caps (GST_VIDEO_SINK_PAD (sink));
504 gboolean is_hardware = FALSE;
506 g_object_get (self->device, "hardware", &is_hardware, NULL);
508 /* In case of WARP device, conversion via shader would be inefficient than
509 * upstream videoconvert. Allow native formats in this case */
511 GValue format_list = G_VALUE_INIT;
512 GValue format = G_VALUE_INIT;
514 g_value_init (&format_list, GST_TYPE_LIST);
515 g_value_init (&format, G_TYPE_STRING);
517 g_value_set_string (&format, "RGBA");
518 gst_value_list_append_and_take_value (&format_list, &format);
520 format = G_VALUE_INIT;
521 g_value_init (&format, G_TYPE_STRING);
522 g_value_set_string (&format, "BGRA");
523 gst_value_list_append_and_take_value (&format_list, &format);
525 caps = gst_caps_make_writable (caps);
526 gst_caps_set_value (caps, "format", &format_list);
527 g_value_unset (&format_list);
533 isect = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
534 gst_caps_unref (caps);
542 gst_d3d11_video_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
544 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
546 GST_DEBUG_OBJECT (self, "set caps %" GST_PTR_FORMAT, caps);
548 /* We will update window on show_frame() */
549 self->caps_updated = TRUE;
555 gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps)
557 gint video_width, video_height;
558 gint video_par_n, video_par_d; /* video's PAR */
559 gint display_par_n = 1, display_par_d = 1; /* display's PAR */
561 GError *error = NULL;
563 GST_DEBUG_OBJECT (self, "Updating window with caps %" GST_PTR_FORMAT, caps);
565 self->caps_updated = FALSE;
567 GST_D3D11_VIDEO_SINK_LOCK (self);
568 if (!gst_d3d11_video_sink_prepare_window (self)) {
569 GST_D3D11_VIDEO_SINK_UNLOCK (self);
571 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND, (nullptr),
572 ("Failed to open window."));
577 if (!gst_video_info_from_caps (&self->info, caps)) {
578 GST_DEBUG_OBJECT (self,
579 "Could not locate image format from caps %" GST_PTR_FORMAT, caps);
580 GST_D3D11_VIDEO_SINK_UNLOCK (self);
584 video_width = GST_VIDEO_INFO_WIDTH (&self->info);
585 video_height = GST_VIDEO_INFO_HEIGHT (&self->info);
586 video_par_n = GST_VIDEO_INFO_PAR_N (&self->info);
587 video_par_d = GST_VIDEO_INFO_PAR_D (&self->info);
589 /* get aspect ratio from caps if it's present, and
590 * convert video width and height to a display width and height
591 * using wd / hd = wv / hv * PARv / PARd */
593 if (!gst_video_calculate_display_ratio (&num, &den, video_width,
594 video_height, video_par_n, video_par_d, display_par_n,
596 GST_D3D11_VIDEO_SINK_UNLOCK (self);
598 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr),
599 ("Error calculating the output display ratio of the video."));
603 GST_DEBUG_OBJECT (self,
604 "video width/height: %dx%d, calculated display ratio: %d/%d format: %s",
605 video_width, video_height, num, den,
606 gst_video_format_to_string (GST_VIDEO_INFO_FORMAT (&self->info)));
608 /* now find a width x height that respects this display ratio.
609 * prefer those that have one of w/h the same as the incoming video
610 * using wd / hd = num / den
613 /* start with same height, because of interlaced video
614 * check hd / den is an integer scale factor, and scale wd with the PAR
616 if (video_height % den == 0) {
617 GST_DEBUG_OBJECT (self, "keeping video height");
618 GST_VIDEO_SINK_WIDTH (self) = (guint)
619 gst_util_uint64_scale_int (video_height, num, den);
620 GST_VIDEO_SINK_HEIGHT (self) = video_height;
621 } else if (video_width % num == 0) {
622 GST_DEBUG_OBJECT (self, "keeping video width");
623 GST_VIDEO_SINK_WIDTH (self) = video_width;
624 GST_VIDEO_SINK_HEIGHT (self) = (guint)
625 gst_util_uint64_scale_int (video_width, den, num);
627 GST_DEBUG_OBJECT (self, "approximating while keeping video height");
628 GST_VIDEO_SINK_WIDTH (self) = (guint)
629 gst_util_uint64_scale_int (video_height, num, den);
630 GST_VIDEO_SINK_HEIGHT (self) = video_height;
633 GST_DEBUG_OBJECT (self, "scaling to %dx%d",
634 GST_VIDEO_SINK_WIDTH (self), GST_VIDEO_SINK_HEIGHT (self));
635 self->video_width = video_width;
636 self->video_height = video_height;
638 if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0) {
639 GST_D3D11_VIDEO_SINK_UNLOCK (self);
641 GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr),
642 ("Error calculating the output display ratio of the video."));
646 if (self->pending_render_rect) {
647 GstVideoRectangle rect = self->render_rect;
649 self->pending_render_rect = FALSE;
650 gst_d3d11_window_set_render_rectangle (self->window, &rect);
653 if (!gst_d3d11_window_prepare (self->window, GST_VIDEO_SINK_WIDTH (self),
654 GST_VIDEO_SINK_HEIGHT (self), caps, &error)) {
655 GstMessage *error_msg;
657 GST_D3D11_VIDEO_SINK_UNLOCK (self);
659 GST_ERROR_OBJECT (self, "cannot create swapchain");
660 error_msg = gst_message_new_error (GST_OBJECT_CAST (self),
661 error, "Failed to prepare d3d11window");
662 g_clear_error (&error);
663 gst_element_post_message (GST_ELEMENT (self), error_msg);
669 gst_d3d11_window_set_title (self->window, self->title);
670 g_clear_pointer (&self->title, g_free);
673 GST_D3D11_VIDEO_SINK_UNLOCK (self);
679 gst_d3d11_video_sink_key_event (GstD3D11Window * window, const gchar * event,
680 const gchar * key, GstD3D11VideoSink * self)
682 GstEvent *key_event = NULL;
684 if (self->enable_navigation_events) {
685 GST_LOG_OBJECT (self, "send key event %s, key %s", event, key);
686 if (0 == g_strcmp0 ("key-press", event))
688 gst_navigation_event_new_key_press (key,
689 GST_NAVIGATION_MODIFIER_NONE);
690 else if (0 == g_strcmp0 ("key-release", event))
692 gst_navigation_event_new_key_release (key,
693 GST_NAVIGATION_MODIFIER_NONE);
696 gst_navigation_send_event_simple (GST_NAVIGATION (self), key_event);
701 gst_d3d11_video_mouse_key_event (GstD3D11Window * window, const gchar * event,
702 gint button, gdouble x, gdouble y, GstD3D11VideoSink * self)
704 GstEvent *mouse_event = NULL;
706 if (self->enable_navigation_events) {
707 GST_LOG_OBJECT (self,
708 "send mouse event %s, button %d (%.1f, %.1f)", event, button, x, y);
709 if (0 == g_strcmp0 ("mouse-button-press", event))
711 gst_navigation_event_new_mouse_button_press (button, x, y,
712 GST_NAVIGATION_MODIFIER_NONE);
713 else if (0 == g_strcmp0 ("mouse-button-release", event))
715 gst_navigation_event_new_mouse_button_release (button, x, y,
716 GST_NAVIGATION_MODIFIER_NONE);
717 else if (0 == g_strcmp0 ("mouse-move", event))
719 gst_navigation_event_new_mouse_move (x, y,
720 GST_NAVIGATION_MODIFIER_NONE);
723 gst_navigation_send_event_simple (GST_NAVIGATION (self), mouse_event);
728 gst_d3d11_video_sink_start (GstBaseSink * sink)
730 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
732 GST_DEBUG_OBJECT (self, "Start");
734 if (!gst_d3d11_ensure_element_data (GST_ELEMENT_CAST (self), self->adapter,
736 GST_ERROR_OBJECT (sink, "Cannot create d3d11device");
743 /* called with lock */
745 gst_d3d11_video_sink_prepare_window (GstD3D11VideoSink * self)
747 GstD3D11WindowNativeType window_type = GST_D3D11_WINDOW_NATIVE_TYPE_HWND;
752 if (self->draw_on_shared_texture) {
753 GST_INFO_OBJECT (self,
754 "Create dummy window for rendering on shared texture");
755 self->window = gst_d3d11_window_dummy_new (self->device);
759 if (!self->window_id)
760 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (self));
762 if (self->window_id) {
764 gst_d3d11_window_get_native_type_from_handle (self->window_id);
766 if (window_type != GST_D3D11_WINDOW_NATIVE_TYPE_NONE) {
767 GST_DEBUG_OBJECT (self, "Have window handle %" G_GUINTPTR_FORMAT,
769 gst_video_overlay_got_window_handle (GST_VIDEO_OVERLAY (self),
774 GST_DEBUG_OBJECT (self, "Create window (type: %s)",
775 gst_d3d11_window_get_native_type_to_string (window_type));
777 #if GST_D3D11_WINAPI_ONLY_APP
778 if (window_type != GST_D3D11_WINDOW_NATIVE_TYPE_CORE_WINDOW &&
779 window_type != GST_D3D11_WINDOW_NATIVE_TYPE_SWAP_CHAIN_PANEL) {
780 GST_ERROR_OBJECT (self, "Overlay handle must be set before READY state");
785 switch (window_type) {
786 #if (!GST_D3D11_WINAPI_ONLY_APP)
787 case GST_D3D11_WINDOW_NATIVE_TYPE_HWND:
788 self->window = gst_d3d11_window_win32_new (self->device, self->window_id);
791 #if GST_D3D11_WINAPI_APP
792 case GST_D3D11_WINDOW_NATIVE_TYPE_CORE_WINDOW:
793 self->window = gst_d3d11_window_core_window_new (self->device,
796 case GST_D3D11_WINDOW_NATIVE_TYPE_SWAP_CHAIN_PANEL:
797 self->window = gst_d3d11_window_swap_chain_panel_new (self->device,
807 GST_ERROR_OBJECT (self, "Cannot create d3d11window");
811 g_object_set (self->window,
812 "force-aspect-ratio", self->force_aspect_ratio,
813 "fullscreen-toggle-mode", self->fullscreen_toggle_mode,
814 "fullscreen", self->fullscreen,
815 "enable-navigation-events", self->enable_navigation_events, NULL);
817 gst_d3d11_window_set_orientation (self->window, self->selected_method);
819 g_signal_connect (self->window, "key-event",
820 G_CALLBACK (gst_d3d11_video_sink_key_event), self);
821 g_signal_connect (self->window, "mouse-event",
822 G_CALLBACK (gst_d3d11_video_mouse_key_event), self);
828 gst_d3d11_video_sink_stop (GstBaseSink * sink)
830 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
832 GST_DEBUG_OBJECT (self, "Stop");
834 GST_D3D11_VIDEO_SINK_LOCK (self);
836 gst_d3d11_window_unprepare (self->window);
838 gst_clear_object (&self->window);
839 GST_D3D11_VIDEO_SINK_UNLOCK (self);
841 gst_clear_object (&self->device);
843 g_clear_pointer (&self->title, g_free);
849 gst_d3d11_video_sink_propose_allocation (GstBaseSink * sink, GstQuery * query)
851 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
853 GstBufferPool *pool = NULL;
861 gst_query_parse_allocation (query, &caps, &need_pool);
866 if (!gst_video_info_from_caps (&info, caps))
869 /* the normal size of a frame */
873 GstCapsFeatures *features;
874 GstStructure *config;
875 gboolean is_d3d11 = FALSE;
877 features = gst_caps_get_features (caps, 0);
879 && gst_caps_features_contains (features,
880 GST_CAPS_FEATURE_MEMORY_D3D11_MEMORY)) {
881 GST_DEBUG_OBJECT (self, "upstream support d3d11 memory");
882 pool = gst_d3d11_buffer_pool_new (self->device);
885 pool = gst_video_buffer_pool_new ();
888 config = gst_buffer_pool_get_config (pool);
889 gst_buffer_pool_config_add_option (config,
890 GST_BUFFER_POOL_OPTION_VIDEO_META);
892 gst_buffer_pool_config_add_option (config,
893 GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
896 size = GST_VIDEO_INFO_SIZE (&info);
898 GstD3D11AllocationParams *d3d11_params;
901 gst_d3d11_allocation_params_new (self->device,
902 &info, GST_D3D11_ALLOCATION_FLAG_DEFAULT, D3D11_BIND_SHADER_RESOURCE,
905 gst_buffer_pool_config_set_d3d11_allocation_params (config, d3d11_params);
906 gst_d3d11_allocation_params_free (d3d11_params);
909 gst_buffer_pool_config_set_params (config, caps, (guint) size, 2, 0);
911 if (!gst_buffer_pool_set_config (pool, config)) {
912 GST_ERROR_OBJECT (pool, "Couldn't set config");
913 gst_object_unref (pool);
918 /* d3d11 buffer pool will update buffer size based on allocated texture,
919 * get size from config again */
920 config = gst_buffer_pool_get_config (pool);
921 gst_buffer_pool_config_get_params (config, nullptr, &size, nullptr,
923 gst_structure_free (config);
926 /* In case of system memory, we will upload video frame to GPU memory,
927 * (which is copy in any case), so crop meta support for system memory
928 * is almost pointless */
929 gst_query_add_allocation_meta (query,
930 GST_VIDEO_CROP_META_API_TYPE, nullptr);
934 /* We need at least 2 buffers because we hold on to the last one for redrawing
935 * on window-resize event */
936 gst_query_add_allocation_pool (query, pool, size, 2, 0);
938 g_object_unref (pool);
940 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
941 gst_query_add_allocation_meta (query,
942 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, NULL);
949 GST_WARNING_OBJECT (self, "no caps specified");
954 GST_WARNING_OBJECT (self, "invalid caps specified");
962 gst_d3d11_video_sink_query (GstBaseSink * sink, GstQuery * query)
964 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
966 switch (GST_QUERY_TYPE (query)) {
967 case GST_QUERY_CONTEXT:
968 if (gst_d3d11_handle_context_query (GST_ELEMENT (self), query,
977 return GST_BASE_SINK_CLASS (parent_class)->query (sink, query);
981 gst_d3d11_video_sink_unlock (GstBaseSink * sink)
983 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
985 GST_D3D11_VIDEO_SINK_LOCK (self);
987 gst_d3d11_window_unlock (self->window);
988 GST_D3D11_VIDEO_SINK_UNLOCK (self);
994 gst_d3d11_video_sink_unlock_stop (GstBaseSink * sink)
996 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
998 GST_D3D11_VIDEO_SINK_LOCK (self);
1000 gst_d3d11_window_unlock_stop (self->window);
1001 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1007 gst_d3d11_video_sink_event (GstBaseSink * sink, GstEvent * event)
1009 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
1011 switch (GST_EVENT_TYPE (event)) {
1012 case GST_EVENT_TAG:{
1013 GstTagList *taglist;
1014 gchar *title = nullptr;
1015 GstVideoOrientationMethod method = GST_VIDEO_ORIENTATION_IDENTITY;
1017 gst_event_parse_tag (event, &taglist);
1018 gst_tag_list_get_string (taglist, GST_TAG_TITLE, &title);
1021 const gchar *app_name = g_get_application_name ();
1022 std::string title_string;
1025 title_string = std::string (title) + " : " + std::string (app_name);
1027 title_string = std::string (title);
1030 GST_D3D11_VIDEO_SINK_LOCK (self);
1032 gst_d3d11_window_set_title (self->window, title_string.c_str ());
1034 g_free (self->title);
1035 self->title = g_strdup (title_string.c_str ());
1037 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1042 if (gst_video_orientation_from_tag (taglist, &method)) {
1043 GST_D3D11_VIDEO_SINK_LOCK (self);
1044 gst_d3d11_video_sink_set_orientation (self, method, TRUE);
1045 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1053 return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
1056 /* called with lock */
1058 gst_d3d11_video_sink_set_orientation (GstD3D11VideoSink * self,
1059 GstVideoOrientationMethod method, gboolean from_tag)
1061 if (method == GST_VIDEO_ORIENTATION_CUSTOM) {
1062 GST_WARNING_OBJECT (self, "Unsupported custom orientation");
1067 self->tag_method = method;
1069 self->method = method;
1071 if (self->method == GST_VIDEO_ORIENTATION_AUTO) {
1072 self->selected_method = self->tag_method;
1074 self->selected_method = self->method;
1078 gst_d3d11_window_set_orientation (self->window, self->selected_method);
1082 gst_d3d11_video_sink_check_device_update (GstD3D11VideoSink * self,
1086 GstD3D11Memory *dmem;
1087 gboolean update_device = FALSE;
1089 /* We have configured window already, cannot update device */
1093 mem = gst_buffer_peek_memory (buf, 0);
1094 if (!gst_is_d3d11_memory (mem))
1097 dmem = GST_D3D11_MEMORY_CAST (mem);
1098 /* Same device, nothing to do */
1099 if (dmem->device == self->device)
1102 if (self->adapter < 0) {
1103 update_device = TRUE;
1107 g_object_get (dmem->device, "adapter", &adapter, NULL);
1108 /* The same GPU as what user wanted, update */
1109 if (adapter == (guint) self->adapter)
1110 update_device = TRUE;
1116 GST_INFO_OBJECT (self, "Updating device %" GST_PTR_FORMAT " -> %"
1117 GST_PTR_FORMAT, self->device, dmem->device);
1119 gst_object_unref (self->device);
1120 self->device = (GstD3D11Device *) gst_object_ref (dmem->device);
1123 static GstFlowReturn
1124 gst_d3d11_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf)
1126 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
1127 GstFlowReturn ret = GST_FLOW_OK;
1129 gst_d3d11_video_sink_check_device_update (self, buf);
1131 if (self->caps_updated || !self->window) {
1132 GstCaps *caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
1133 gboolean update_ret;
1135 /* shouldn't happen */
1137 return GST_FLOW_NOT_NEGOTIATED;
1139 update_ret = gst_d3d11_video_sink_update_window (self, caps);
1140 gst_caps_unref (caps);
1143 return GST_FLOW_NOT_NEGOTIATED;
1146 gst_d3d11_window_show (self->window);
1148 if (self->draw_on_shared_texture) {
1149 GST_D3D11_VIDEO_SINK_LOCK (self);
1150 self->current_buffer = buf;
1151 self->drawing = TRUE;
1153 GST_LOG_OBJECT (self, "Begin drawing");
1155 /* Application should call draw method on this callback */
1156 g_signal_emit (self, gst_d3d11_video_sink_signals[SIGNAL_BEGIN_DRAW], 0,
1159 GST_LOG_OBJECT (self, "End drawing");
1160 self->drawing = FALSE;
1161 self->current_buffer = nullptr;
1162 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1164 ret = gst_d3d11_window_render (self->window, buf);
1167 if (ret == GST_D3D11_WINDOW_FLOW_CLOSED) {
1168 GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
1169 ("Output window was closed"), (NULL));
1171 ret = GST_FLOW_ERROR;
1177 /* VideoOverlay interface */
1179 gst_d3d11_video_sink_set_window_handle (GstVideoOverlay * overlay,
1182 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (overlay);
1184 GST_DEBUG ("set window handle %" G_GUINTPTR_FORMAT, window_id);
1186 self->window_id = window_id;
1190 gst_d3d11_video_sink_set_render_rectangle (GstVideoOverlay * overlay, gint x,
1191 gint y, gint width, gint height)
1193 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (overlay);
1195 GST_DEBUG_OBJECT (self,
1196 "render rect x: %d, y: %d, width: %d, height %d", x, y, width, height);
1198 GST_D3D11_VIDEO_SINK_LOCK (self);
1200 GstVideoRectangle rect;
1207 self->render_rect = rect;
1209 gst_d3d11_window_set_render_rectangle (self->window, &rect);
1211 self->render_rect.x = x;
1212 self->render_rect.y = y;
1213 self->render_rect.w = width;
1214 self->render_rect.h = height;
1215 self->pending_render_rect = TRUE;
1218 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1222 gst_d3d11_video_sink_expose (GstVideoOverlay * overlay)
1224 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (overlay);
1226 GST_D3D11_VIDEO_SINK_LOCK (self);
1227 if (self->window && self->window->swap_chain)
1228 gst_d3d11_window_render (self->window, nullptr);
1229 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1233 gst_d3d11_video_sink_video_overlay_init (GstVideoOverlayInterface * iface)
1235 iface->set_window_handle = gst_d3d11_video_sink_set_window_handle;
1236 iface->set_render_rectangle = gst_d3d11_video_sink_set_render_rectangle;
1237 iface->expose = gst_d3d11_video_sink_expose;
1240 /* Navigation interface */
1242 gst_d3d11_video_sink_navigation_send_event (GstNavigation * navigation,
1245 GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (navigation);
1247 /* TODO: add support for translating native coordinate and video coordinate
1248 * when force-aspect-ratio is set */
1252 gst_event_ref (event);
1253 handled = gst_pad_push_event (GST_VIDEO_SINK_PAD (self), event);
1256 gst_element_post_message (GST_ELEMENT_CAST (self),
1257 gst_navigation_message_new_event (GST_OBJECT_CAST (self), event));
1259 gst_event_unref (event);
1264 gst_d3d11_video_sink_navigation_init (GstNavigationInterface * iface)
1266 iface->send_event_simple = gst_d3d11_video_sink_navigation_send_event;
1270 gst_d3d11_video_sink_draw_action (GstD3D11VideoSink * self,
1271 gpointer shared_handle, guint texture_misc_flags,
1272 guint64 acquire_key, guint64 release_key)
1275 g_return_val_if_fail (shared_handle != NULL, FALSE);
1277 if (!self->draw_on_shared_texture) {
1278 GST_ERROR_OBJECT (self, "Invalid draw call, we are drawing on window");
1282 if (!shared_handle) {
1283 GST_ERROR_OBJECT (self, "Invalid handle");
1287 GST_D3D11_VIDEO_SINK_LOCK (self);
1288 if (!self->drawing || !self->current_buffer) {
1289 GST_WARNING_OBJECT (self, "Nothing to draw");
1290 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1294 GST_LOG_OBJECT (self, "Drawing on shared handle %p, MiscFlags: 0x%x"
1295 ", acquire key: %" G_GUINT64_FORMAT ", release key: %"
1296 G_GUINT64_FORMAT, shared_handle, texture_misc_flags, acquire_key,
1299 ret = gst_d3d11_window_render_on_shared_handle (self->window,
1300 self->current_buffer, shared_handle, texture_misc_flags, acquire_key,
1302 GST_D3D11_VIDEO_SINK_UNLOCK (self);
1304 return ret == GST_FLOW_OK;