d3d11videosink: Display title of content if possible
authorSeungha Yang <seungha@centricular.com>
Fri, 17 Sep 2021 17:27:51 +0000 (02:27 +0900)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Sat, 18 Sep 2021 16:02:15 +0000 (16:02 +0000)
Update title text of window (currently it's always "Direct3D11 renderer")
when we are rendering on internal HWND, not external HWND.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/2529>

sys/d3d11/gstd3d11videosink.cpp
sys/d3d11/gstd3d11window.cpp
sys/d3d11/gstd3d11window.h
sys/d3d11/gstd3d11window_win32.cpp

index cbc0db9..9cc9c1a 100644 (file)
@@ -41,6 +41,7 @@
 #include "gstd3d11videosink.h"
 #include "gstd3d11videoprocessor.h"
 #include "gstd3d11pluginutils.h"
+#include <string>
 
 #if GST_D3D11_WINAPI_APP
 #include "gstd3d11window_corewindow.h"
@@ -132,6 +133,8 @@ struct _GstD3D11VideoSink
   gboolean drawing;
   GstBuffer *current_buffer;
   GRecMutex draw_lock;
+
+  gchar *title;
 };
 
 static void gst_d3d11_videosink_set_property (GObject * object, guint prop_id,
@@ -164,6 +167,8 @@ static gboolean gst_d3d11_video_sink_query (GstBaseSink * sink,
     GstQuery * query);
 static gboolean gst_d3d11_video_sink_unlock (GstBaseSink * sink);
 static gboolean gst_d3d11_video_sink_unlock_stop (GstBaseSink * sink);
+static gboolean gst_d3d11_video_sink_event (GstBaseSink * sink,
+    GstEvent * event);
 
 static GstFlowReturn
 gst_d3d11_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf);
@@ -320,6 +325,7 @@ gst_d3d11_video_sink_class_init (GstD3D11VideoSinkClass * klass)
   basesink_class->unlock = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_unlock);
   basesink_class->unlock_stop =
       GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_unlock_stop);
+  basesink_class->event = GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_event);
 
   videosink_class->show_frame =
       GST_DEBUG_FUNCPTR (gst_d3d11_video_sink_show_frame);
@@ -432,6 +438,7 @@ gst_d3d11_video_sink_finalize (GObject * object)
   GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object);
 
   g_rec_mutex_clear (&self->draw_lock);
+  g_free (self->title);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -642,6 +649,11 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps)
 
   self->processor_in_use = FALSE;
 
+  if (self->title) {
+    gst_d3d11_window_set_title (self->window, self->title);
+    g_clear_pointer (&self->title, g_free);
+  }
+
   return TRUE;
 
   /* ERRORS */
@@ -810,6 +822,8 @@ gst_d3d11_video_sink_stop (GstBaseSink * sink)
   gst_clear_object (&self->device);
   gst_clear_object (&self->window);
 
+  g_clear_pointer (&self->title, g_free);
+
   return TRUE;
 }
 
@@ -963,6 +977,47 @@ gst_d3d11_video_sink_unlock_stop (GstBaseSink * sink)
 }
 
 static gboolean
+gst_d3d11_video_sink_event (GstBaseSink * sink, GstEvent * event)
+{
+  GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink);
+
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_TAG:{
+      GstTagList *taglist;
+      gchar *title = nullptr;
+
+      gst_event_parse_tag (event, &taglist);
+      gst_tag_list_get_string (taglist, GST_TAG_TITLE, &title);
+
+      if (title) {
+        const gchar *app_name = g_get_application_name ();
+        std::string title_string;
+
+        if (app_name) {
+          title_string = std::string (title) + " : " + std::string (app_name);
+        } else {
+          title_string = std::string (title);
+        }
+
+        if (self->window) {
+          gst_d3d11_window_set_title (self->window, title_string.c_str ());
+        } else {
+          g_free (self->title);
+          self->title = g_strdup (title_string.c_str ());
+        }
+
+        g_free (title);
+      }
+      break;
+    }
+    default:
+      break;
+  }
+
+  return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
+}
+
+static gboolean
 gst_d3d11_video_sink_upload_frame (GstD3D11VideoSink * self, GstBuffer * inbuf,
     GstBuffer * outbuf)
 {
index 85506b1..6eb53f3 100644 (file)
@@ -788,6 +788,19 @@ gst_d3d11_window_set_render_rectangle (GstD3D11Window * window,
     klass->set_render_rectangle (window, rect);
 }
 
+void
+gst_d3d11_window_set_title (GstD3D11Window * window, const gchar * title)
+{
+  GstD3D11WindowClass *klass;
+
+  g_return_if_fail (GST_IS_D3D11_WINDOW (window));
+
+  klass = GST_D3D11_WINDOW_GET_CLASS (window);
+
+  if (klass->set_title)
+    klass->set_title (window, title);
+}
+
 static gboolean
 gst_d3d11_window_buffer_ensure_processor_input (GstD3D11Window * self,
     GstBuffer * buffer, ID3D11VideoProcessorInputView ** in_view)
index 257f23b..8d35b4e 100644 (file)
@@ -170,6 +170,9 @@ struct _GstD3D11WindowClass
 
   void          (*set_render_rectangle)   (GstD3D11Window * window,
                                            const GstVideoRectangle * rect);
+
+  void          (*set_title)              (GstD3D11Window * window,
+                                           const gchar *title);
 };
 
 GType         gst_d3d11_window_get_type             (void);
@@ -179,6 +182,9 @@ void          gst_d3d11_window_show                 (GstD3D11Window * window);
 void          gst_d3d11_window_set_render_rectangle (GstD3D11Window * window,
                                                      const GstVideoRectangle * rect);
 
+void          gst_d3d11_window_set_title            (GstD3D11Window * window,
+                                                     const gchar *title);
+
 gboolean      gst_d3d11_window_prepare              (GstD3D11Window * window,
                                                      guint display_width,
                                                      guint display_height,
index 6448391..6261c97 100644 (file)
@@ -129,6 +129,8 @@ static void gst_d3d11_window_win32_unprepare (GstD3D11Window * window);
 static void
 gst_d3d11_window_win32_set_render_rectangle (GstD3D11Window * window,
     const GstVideoRectangle * rect);
+static void gst_d3d11_window_win32_set_title (GstD3D11Window * window,
+    const gchar * title);
 
 static void
 gst_d3d11_window_win32_class_init (GstD3D11WindowWin32Class * klass)
@@ -154,6 +156,8 @@ gst_d3d11_window_win32_class_init (GstD3D11WindowWin32Class * klass)
       GST_DEBUG_FUNCPTR (gst_d3d11_window_win32_unprepare);
   window_class->set_render_rectangle =
       GST_DEBUG_FUNCPTR (gst_d3d11_window_win32_set_render_rectangle);
+  window_class->set_title =
+      GST_DEBUG_FUNCPTR (gst_d3d11_window_win32_set_title);
 }
 
 static void
@@ -281,6 +285,22 @@ gst_d3d11_window_win32_set_render_rectangle (GstD3D11Window * window,
 }
 
 static void
+gst_d3d11_window_win32_set_title (GstD3D11Window * window, const gchar * title)
+{
+  GstD3D11WindowWin32 *self = GST_D3D11_WINDOW_WIN32 (window);
+
+  /* Do this only when we are rendring on our own HWND */
+  if (!self->external_hwnd && self->internal_hwnd) {
+    gunichar2 *str = g_utf8_to_utf16 (title, -1, nullptr, nullptr, nullptr);
+
+    if (str) {
+      SetWindowTextW (self->internal_hwnd, (LPCWSTR) str);
+      g_free (str);
+    }
+  }
+}
+
+static void
 gst_d3d11_window_win32_finalize (GObject * object)
 {
   GstD3D11WindowWin32 *self = GST_D3D11_WINDOW_WIN32 (object);