gstgtkglsink.c 9.72 KB
Newer Older
Matthew Waters's avatar
Matthew Waters committed
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
/*
 * GStreamer
 * Copyright (C) 2015 Matthew Waters <matthew@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.
 */

/**
 * SECTION:gstgtkglsink
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstgtkglsink.h"
31
#include "gtkgstglwidget.h"
Matthew Waters's avatar
Matthew Waters committed
32 33 34 35

GST_DEBUG_CATEGORY (gst_debug_gtk_gl_sink);
#define GST_CAT_DEFAULT gst_debug_gtk_gl_sink

36
static gboolean gst_gtk_gl_sink_start (GstBaseSink * bsink);
Matthew Waters's avatar
Matthew Waters committed
37 38 39 40
static gboolean gst_gtk_gl_sink_stop (GstBaseSink * bsink);
static gboolean gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query);
static gboolean gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink,
    GstQuery * query);
41 42
static GstCaps *gst_gtk_gl_sink_get_caps (GstBaseSink * bsink,
    GstCaps * filter);
Matthew Waters's avatar
Matthew Waters committed
43 44

static GstStaticPadTemplate gst_gtk_gl_sink_template =
45
    GST_STATIC_PAD_TEMPLATE ("sink",
Matthew Waters's avatar
Matthew Waters committed
46 47 48
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
49 50 51 52
        (GST_CAPS_FEATURE_MEMORY_GL_MEMORY, "RGBA") "; "
        GST_VIDEO_CAPS_MAKE_WITH_FEATURES
        (GST_CAPS_FEATURE_MEMORY_GL_MEMORY ", "
            GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION, "RGBA")));
Matthew Waters's avatar
Matthew Waters committed
53 54 55

#define gst_gtk_gl_sink_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstGtkGLSink, gst_gtk_gl_sink,
56
    GST_TYPE_GTK_BASE_SINK, GST_DEBUG_CATEGORY_INIT (gst_debug_gtk_gl_sink,
57
        "gtkglsink", 0, "Gtk GL Video Sink"));
Matthew Waters's avatar
Matthew Waters committed
58 59 60 61 62 63

static void
gst_gtk_gl_sink_class_init (GstGtkGLSinkClass * klass)
{
  GstElementClass *gstelement_class;
  GstBaseSinkClass *gstbasesink_class;
64
  GstGtkBaseSinkClass *gstgtkbasesink_class;
Matthew Waters's avatar
Matthew Waters committed
65 66 67

  gstelement_class = (GstElementClass *) klass;
  gstbasesink_class = (GstBaseSinkClass *) klass;
68
  gstgtkbasesink_class = (GstGtkBaseSinkClass *) klass;
Matthew Waters's avatar
Matthew Waters committed
69

70 71 72 73
  gstbasesink_class->query = gst_gtk_gl_sink_query;
  gstbasesink_class->propose_allocation = gst_gtk_gl_sink_propose_allocation;
  gstbasesink_class->start = gst_gtk_gl_sink_start;
  gstbasesink_class->stop = gst_gtk_gl_sink_stop;
74
  gstbasesink_class->get_caps = gst_gtk_gl_sink_get_caps;
75 76 77

  gstgtkbasesink_class->create_widget = gtk_gst_gl_widget_new;
  gstgtkbasesink_class->window_title = "Gtk+ GL renderer";
Matthew Waters's avatar
Matthew Waters committed
78

79 80
  gst_element_class_set_metadata (gstelement_class, "Gtk GL Video Sink",
      "Sink/Video", "A video sink that renders to a GtkWidget using OpenGL",
Matthew Waters's avatar
Matthew Waters committed
81 82
      "Matthew Waters <matthew@centricular.com>");

83 84
  gst_element_class_add_static_pad_template (gstelement_class,
      &gst_gtk_gl_sink_template);
Matthew Waters's avatar
Matthew Waters committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
}

static void
gst_gtk_gl_sink_init (GstGtkGLSink * gtk_sink)
{
}

static gboolean
gst_gtk_gl_sink_query (GstBaseSink * bsink, GstQuery * query)
{
  GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
  gboolean res = FALSE;

  switch (GST_QUERY_TYPE (query)) {
    case GST_QUERY_CONTEXT:
    {
      const gchar *context_type;
      GstContext *context, *old_context;

104
      res = gst_gl_handle_context_query ((GstElement *) gtk_sink, query,
Matthew Waters's avatar
Matthew Waters committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
          &gtk_sink->display, &gtk_sink->gtk_context);

      if (gtk_sink->display)
        gst_gl_display_filter_gl_api (gtk_sink->display, GST_GL_API_OPENGL3);

      gst_query_parse_context_type (query, &context_type);

      if (g_strcmp0 (context_type, "gst.gl.local_context") == 0) {
        GstStructure *s;

        gst_query_parse_context (query, &old_context);

        if (old_context)
          context = gst_context_copy (old_context);
        else
          context = gst_context_new ("gst.gl.local_context", FALSE);

        s = gst_context_writable_structure (context);
123
        gst_structure_set (s, "context", GST_TYPE_GL_CONTEXT, gtk_sink->context,
Matthew Waters's avatar
Matthew Waters committed
124 125 126 127
            NULL);
        gst_query_set_context (query, context);
        gst_context_unref (context);

128
        res = gtk_sink->context != NULL;
Matthew Waters's avatar
Matthew Waters committed
129 130
      }
      GST_LOG_OBJECT (gtk_sink, "context query of type %s %i", context_type,
131 132
          res);
      break;
Matthew Waters's avatar
Matthew Waters committed
133 134 135 136 137 138 139 140 141
    }
    default:
      res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
      break;
  }

  return res;
}

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
static void
_size_changed_cb (GtkWidget * widget, GdkRectangle * rectangle,
    GstGtkGLSink * gtk_sink)
{
  gint scale_factor, width, height;
  gboolean reconfigure;

  scale_factor = gtk_widget_get_scale_factor (widget);
  width = scale_factor * gtk_widget_get_allocated_width (widget);
  height = scale_factor * gtk_widget_get_allocated_height (widget);

  GST_OBJECT_LOCK (gtk_sink);
  reconfigure =
      (width != gtk_sink->display_width || height != gtk_sink->display_height);
  gtk_sink->display_width = width;
  gtk_sink->display_height = height;
  GST_OBJECT_UNLOCK (gtk_sink);

  if (reconfigure) {
    GST_DEBUG_OBJECT (gtk_sink, "Sending reconfigure event on sinkpad.");
    gst_pad_push_event (GST_BASE_SINK (gtk_sink)->sinkpad,
        gst_event_new_reconfigure ());
  }
}

Matthew Waters's avatar
Matthew Waters committed
167
static gboolean
168
gst_gtk_gl_sink_start (GstBaseSink * bsink)
Matthew Waters's avatar
Matthew Waters committed
169
{
170
  GstGtkBaseSink *base_sink = GST_GTK_BASE_SINK (bsink);
Matthew Waters's avatar
Matthew Waters committed
171
  GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
172
  GtkGstGLWidget *gst_widget;
Matthew Waters's avatar
Matthew Waters committed
173

174
  if (!GST_BASE_SINK_CLASS (parent_class)->start (bsink))
Matthew Waters's avatar
Matthew Waters committed
175 176
    return FALSE;

177 178
  /* After this point, gtk_sink->widget will always be set */
  gst_widget = GTK_GST_GL_WIDGET (base_sink->widget);
179

180 181 182 183 184
  /* Track the allocation size */
  g_signal_connect (gst_widget, "size-allocate", G_CALLBACK (_size_changed_cb),
      gtk_sink);
  _size_changed_cb (GTK_WIDGET (gst_widget), NULL, gtk_sink);

185
  if (!gtk_gst_gl_widget_init_winsys (gst_widget))
186 187
    return FALSE;

188 189 190
  gtk_sink->display = gtk_gst_gl_widget_get_display (gst_widget);
  gtk_sink->context = gtk_gst_gl_widget_get_context (gst_widget);
  gtk_sink->gtk_context = gtk_gst_gl_widget_get_gtk_context (gst_widget);
Matthew Waters's avatar
Matthew Waters committed
191

192 193
  if (!gtk_sink->display || !gtk_sink->context || !gtk_sink->gtk_context)
    return FALSE;
194

Matthew Waters's avatar
Matthew Waters committed
195 196 197
  return TRUE;
}

198 199
static gboolean
gst_gtk_gl_sink_stop (GstBaseSink * bsink)
Matthew Waters's avatar
Matthew Waters committed
200
{
201
  GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
Matthew Waters's avatar
Matthew Waters committed
202

203 204 205
  if (gtk_sink->display) {
    gst_object_unref (gtk_sink->display);
    gtk_sink->display = NULL;
206 207
  }

208 209 210 211
  if (gtk_sink->context) {
    gst_object_unref (gtk_sink->context);
    gtk_sink->context = NULL;
  }
212

213 214 215 216
  if (gtk_sink->gtk_context) {
    gst_object_unref (gtk_sink->gtk_context);
    gtk_sink->gtk_context = NULL;
  }
217

218
  return GST_BASE_SINK_CLASS (parent_class)->stop (bsink);
Matthew Waters's avatar
Matthew Waters committed
219 220 221 222 223 224
}

static gboolean
gst_gtk_gl_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
{
  GstGtkGLSink *gtk_sink = GST_GTK_GL_SINK (bsink);
225
  GstBufferPool *pool = NULL;
Matthew Waters's avatar
Matthew Waters committed
226 227 228 229
  GstStructure *config;
  GstCaps *caps;
  guint size;
  gboolean need_pool;
230 231
  GstStructure *allocation_meta = NULL;
  gint display_width, display_height;
Matthew Waters's avatar
Matthew Waters committed
232 233 234 235 236 237 238 239 240

  if (!gtk_sink->display || !gtk_sink->context)
    return FALSE;

  gst_query_parse_allocation (query, &caps, &need_pool);

  if (caps == NULL)
    goto no_caps;

241
  if (need_pool) {
Matthew Waters's avatar
Matthew Waters committed
242 243 244 245 246 247 248 249 250 251 252 253 254
    GstVideoInfo info;

    if (!gst_video_info_from_caps (&info, caps))
      goto invalid_caps;

    GST_DEBUG_OBJECT (gtk_sink, "create new pool");
    pool = gst_gl_buffer_pool_new (gtk_sink->context);

    /* the normal size of a frame */
    size = info.size;

    config = gst_buffer_pool_get_config (pool);
    gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
255 256 257
    gst_buffer_pool_config_add_option (config,
        GST_BUFFER_POOL_OPTION_GL_SYNC_META);

Matthew Waters's avatar
Matthew Waters committed
258 259
    if (!gst_buffer_pool_set_config (pool, config))
      goto config_failed;
260 261

    /* we need at least 2 buffer because we hold on to the last one */
Matthew Waters's avatar
Matthew Waters committed
262 263 264 265
    gst_query_add_allocation_pool (query, pool, size, 2, 0);
    gst_object_unref (pool);
  }

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  GST_OBJECT_LOCK (gtk_sink);
  display_width = gtk_sink->display_width;
  display_height = gtk_sink->display_height;
  GST_OBJECT_UNLOCK (gtk_sink);

  if (display_width != 0 && display_height != 0) {
    GST_DEBUG_OBJECT (gtk_sink, "sending alloc query with size %dx%d",
        display_width, display_height);
    allocation_meta = gst_structure_new ("GstVideoOverlayCompositionMeta",
        "width", G_TYPE_UINT, display_width,
        "height", G_TYPE_UINT, display_height, NULL);
  }

  gst_query_add_allocation_meta (query,
      GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);

  if (allocation_meta)
    gst_structure_free (allocation_meta);

Matthew Waters's avatar
Matthew Waters committed
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
  /* we also support various metadata */
  gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, 0);

  if (gtk_sink->context->gl_vtable->FenceSync)
    gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);

  return TRUE;

  /* ERRORS */
no_caps:
  {
    GST_DEBUG_OBJECT (bsink, "no caps specified");
    return FALSE;
  }
invalid_caps:
  {
    GST_DEBUG_OBJECT (bsink, "invalid caps specified");
    return FALSE;
  }
config_failed:
  {
    GST_DEBUG_OBJECT (bsink, "failed setting config");
    return FALSE;
  }
}
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

static GstCaps *
gst_gtk_gl_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
{
  GstCaps *tmp = NULL;
  GstCaps *result = NULL;

  tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));

  if (filter) {
    GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
        filter);

    result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
    gst_caps_unref (tmp);
  } else {
    result = tmp;
  }

  result = gst_gl_overlay_compositor_add_caps (result);

  GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);

  return result;
}