98ade7fa3bef2726a5a586bf58197766cd8db232
[platform/upstream/gstreamer.git] / ext / wpe / gstwpesrc.cpp
1 /* Copyright (C) <2018> Philippe Normand <philn@igalia.com>
2  * Copyright (C) <2018> Žan Doberšek <zdobersek@igalia.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-wpesrc
22  * @title: wpesrc
23  *
24  * The wpesrc element is used to produce a video texture representing a web page
25  * rendered off-screen by WPE.
26  *
27  * ## Example launch line
28  *
29  * |[
30  * gst-launch-1.0 -v wpesrc location="https://gstreamer.freedesktop.org" ! queue ! glimagesink
31  * ]|
32  * Shows the GStreamer website homepage
33  *
34  * |[
35  * gst-play-1.0 --videosink gtkglsink wpe://https://gstreamer.freedesktop.org
36  * ]|
37  * Shows the GStreamer website homepage as played with GstPlayer in a GTK+ window.
38  *
39  * |[
40  * gst-launch-1.0  glvideomixer name=m sink_1::zorder=0 ! glimagesink wpesrc location="file:///home/phil/Downloads/plunk/index.html" draw-background=0 ! m. videotestsrc ! queue ! glupload ! glcolorconvert ! m.
41  * ]|
42  * Composite WPE with a video stream in a single OpenGL scene.
43  *
44  * |[
45  * gst-launch-1.0 glvideomixer name=m sink_1::zorder=0 sink_0::height=818 sink_0::width=1920 ! gtkglsink wpesrc location="file:///home/phil/Downloads/plunk/index.html" draw-background=0 ! m. uridecodebin uri="http://192.168.1.44/Sintel.2010.1080p.mkv" name=d d. ! queue ! glupload ! glcolorconvert ! m.
46  * ]|
47  * Composite WPE with a video stream, sink_0 pad properties have to match the video dimensions.
48  */
49
50 /*
51  * TODO:
52  * - Audio support (requires an AudioSession implementation in WebKit and a WPEBackend-fdo API for it)
53  * - DMABuf support (requires changes in WPEBackend-fdo to expose DMABuf planes and fds)
54  * - Custom EGLMemory allocator
55  * - Better navigation events handling (would require a new GstNavigation API)
56  */
57
58 #ifdef HAVE_CONFIG_H
59 #include <config.h>
60 #endif
61
62 #include "gstwpesrc.h"
63 #include <gst/gl/gl.h>
64 #include <gst/gl/egl/gstglmemoryegl.h>
65 #include <gst/gl/wayland/gstgldisplay_wayland.h>
66 #include <gst/video/video.h>
67 #include <xkbcommon/xkbcommon.h>
68
69 #include "WPEThreadedView.h"
70
71 GST_DEBUG_CATEGORY (wpe_src_debug);
72 #define GST_CAT_DEFAULT wpe_src_debug
73
74 #define DEFAULT_WIDTH 1920
75 #define DEFAULT_HEIGHT 1080
76 #define DEFAULT_FPS_N 30
77 #define DEFAULT_FPS_D 1
78
79 enum
80 {
81   PROP_0,
82   PROP_LOCATION,
83   PROP_DRAW_BACKGROUND
84 };
85
86 enum
87 {
88   SIGNAL_CONFIGURE_WEB_VIEW,
89   SIGNAL_LOAD_BYTES,
90   LAST_SIGNAL
91 };
92 static guint gst_wpe_src_signals[LAST_SIGNAL] = { 0 };
93
94 struct _GstWpeSrc
95 {
96   GstGLBaseSrc parent;
97
98   WPEThreadedView *view;
99
100   /* properties */
101   gchar *location;
102   gboolean draw_background;
103
104   GBytes *bytes;
105 };
106
107 static void gst_wpe_src_uri_handler_init (gpointer iface, gpointer data);
108
109 #define gst_wpe_src_parent_class parent_class
110 G_DEFINE_TYPE_WITH_CODE (GstWpeSrc, gst_wpe_src, GST_TYPE_GL_BASE_SRC,
111     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_wpe_src_uri_handler_init));
112
113 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
114     GST_PAD_SRC,
115     GST_PAD_ALWAYS,
116     GST_STATIC_CAPS ("video/x-raw(memory:GLMemory), "
117         "format = (string) RGBA, "
118         "width = " GST_VIDEO_SIZE_RANGE ", "
119         "height = " GST_VIDEO_SIZE_RANGE ", "
120         "framerate = " GST_VIDEO_FPS_RANGE ", "
121         "pixel-aspect-ratio = (fraction)1/1," "texture-target = (string)2D")
122     );
123
124 static gboolean
125 gst_wpe_src_fill_memory (GstGLBaseSrc * bsrc, GstGLMemory * memory)
126 {
127   GstWpeSrc *src = GST_WPE_SRC (bsrc);
128   const GstGLFuncs *gl;
129   guint tex_id;
130   GstEGLImage *locked_image;
131
132   if (!gst_gl_context_check_feature (GST_GL_CONTEXT (bsrc->context),
133           "EGL_KHR_image_base")) {
134     GST_ERROR_OBJECT (src, "EGL_KHR_image_base is not supported");
135     return FALSE;
136   }
137
138   GST_OBJECT_LOCK (src);
139
140   gl = bsrc->context->gl_vtable;
141   tex_id = gst_gl_memory_get_texture_id (memory);
142   locked_image = src->view->image ();
143
144   if (!locked_image) {
145     GST_OBJECT_UNLOCK (src);
146     return TRUE;
147   }
148
149   gl->ActiveTexture (GL_TEXTURE0 + memory->plane);
150   gl->BindTexture (GL_TEXTURE_2D, tex_id);
151   gl->EGLImageTargetTexture2D (GL_TEXTURE_2D,
152       gst_egl_image_get_image (locked_image));
153   gl->Flush ();
154   GST_OBJECT_UNLOCK (src);
155   return TRUE;
156 }
157
158 static gboolean
159 gst_wpe_src_gl_start (GstGLBaseSrc * base_src)
160 {
161   GstWpeSrc *src = GST_WPE_SRC (base_src);
162   gboolean result = TRUE;
163
164   GST_INFO_OBJECT (src, "Starting up");
165   GST_OBJECT_LOCK (src);
166   src->view = new WPEThreadedView;
167   result = src->view->initialize (src, base_src->context, base_src->display,
168       GST_VIDEO_INFO_WIDTH (&base_src->out_info),
169       GST_VIDEO_INFO_HEIGHT (&base_src->out_info));
170
171   if (src->bytes != NULL) {
172     src->view->loadData (src->bytes);
173     g_bytes_unref (src->bytes);
174     src->bytes = NULL;
175   }
176
177   GST_OBJECT_UNLOCK (src);
178   if (!result) {
179     GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
180         ("WPEBackend-FDO EGL display initialisation failed"), (NULL));
181   }
182   return result;
183 }
184
185 static void
186 gst_wpe_src_gl_stop (GstGLBaseSrc * base_src)
187 {
188   GstWpeSrc *src = GST_WPE_SRC (base_src);
189   if (src->view) {
190     delete src->view;
191     src->view = NULL;
192   }
193 }
194
195 static GstCaps *
196 gst_wpe_src_fixate (GstBaseSrc * base_src, GstCaps * caps)
197 {
198   GstWpeSrc *src = GST_WPE_SRC (base_src);
199   GstStructure *structure;
200   gint width, height;
201
202   caps = gst_caps_make_writable (caps);
203   structure = gst_caps_get_structure (caps, 0);
204
205   gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
206   gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
207
208   if (gst_structure_has_field (structure, "framerate"))
209     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
210         DEFAULT_FPS_N, DEFAULT_FPS_D);
211   else
212     gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, DEFAULT_FPS_N,
213         DEFAULT_FPS_D, NULL);
214
215   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (base_src, caps);
216   GST_INFO_OBJECT (base_src, "Fixated caps to %" GST_PTR_FORMAT, caps);
217
218   if (src->view) {
219     gst_structure_get (structure, "width", G_TYPE_INT, &width, "height", G_TYPE_INT, &height, NULL);
220     src->view->resize (width, height);
221   }
222   return caps;
223 }
224
225 void
226 gst_wpe_src_configure_web_view (GstWpeSrc * src, WebKitWebView * webview)
227 {
228   GValue args[2] = { {0}, {0} };
229
230   g_value_init (&args[0], GST_TYPE_ELEMENT);
231   g_value_set_object (&args[0], src);
232   g_value_init (&args[1], G_TYPE_OBJECT);
233   g_value_set_object (&args[1], webview);
234
235   g_signal_emitv (args, gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW], 0,
236       NULL);
237
238   g_value_unset (&args[0]);
239   g_value_unset (&args[1]);
240 }
241
242 static void
243 gst_wpe_src_load_bytes (GstWpeSrc * src, GBytes * bytes)
244 {
245   if (src->view && GST_STATE (GST_ELEMENT_CAST (src)) > GST_STATE_NULL)
246     src->view->loadData (bytes);
247   else
248     src->bytes = g_bytes_ref (bytes);
249 }
250
251 static gboolean
252 gst_wpe_src_set_location (GstWpeSrc * src, const gchar * location,
253     GError ** error)
254 {
255   g_free (src->location);
256   src->location = g_strdup (location);
257   if (src->view)
258     src->view->loadUri (src->location);
259
260   return TRUE;
261 }
262
263 static void
264 gst_wpe_src_set_draw_background (GstWpeSrc * src, gboolean draw_background)
265 {
266   if (src->view)
267     src->view->setDrawBackground (draw_background);
268   src->draw_background = draw_background;
269 }
270
271 static void
272 gst_wpe_src_set_property (GObject * object, guint prop_id, const GValue * value,
273     GParamSpec * pspec)
274 {
275   GstWpeSrc *src = GST_WPE_SRC (object);
276
277   switch (prop_id) {
278     case PROP_LOCATION:
279     {
280       const gchar *location;
281
282       location = g_value_get_string (value);
283       if (location == NULL) {
284         GST_WARNING_OBJECT (src, "location property cannot be NULL");
285         return;
286       }
287
288       if (!gst_wpe_src_set_location (src, location, NULL)) {
289         GST_WARNING_OBJECT (src, "badly formatted location");
290         return;
291       }
292       break;
293     }
294     case PROP_DRAW_BACKGROUND:
295       gst_wpe_src_set_draw_background (src, g_value_get_boolean (value));
296       break;
297     default:
298       break;
299   }
300 }
301
302 static void
303 gst_wpe_src_get_property (GObject * object, guint prop_id, GValue * value,
304     GParamSpec * pspec)
305 {
306   GstWpeSrc *src = GST_WPE_SRC (object);
307
308   switch (prop_id) {
309     case PROP_LOCATION:
310       g_value_set_string (value, src->location);
311       break;
312     case PROP_DRAW_BACKGROUND:
313       g_value_set_boolean (value, src->draw_background);
314       break;
315     default:
316       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317       break;
318   }
319 }
320
321 static gboolean
322 gst_wpe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
323 {
324   gboolean ret = FALSE;
325   GstWpeSrc *src = GST_WPE_SRC (parent);
326
327   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION) {
328     const gchar *key;
329     gint button;
330     gdouble x, y;
331
332     GST_DEBUG_OBJECT (src, "Processing event %" GST_PTR_FORMAT, event);
333     if (!src->view) {
334       return FALSE;
335     }
336     switch (gst_navigation_event_get_type (event)) {
337       case GST_NAVIGATION_EVENT_KEY_PRESS:
338       case GST_NAVIGATION_EVENT_KEY_RELEASE:
339         if (gst_navigation_event_parse_key_event (event, &key)) {
340           /* FIXME: This is wrong... The GstNavigation API should pass
341              hardware-level informations, not high-level keysym strings */
342           uint32_t keysym =
343               (uint32_t) xkb_keysym_from_name (key, XKB_KEYSYM_NO_FLAGS);
344           struct wpe_input_keyboard_event wpe_event;
345           wpe_event.key_code = keysym;
346           wpe_event.pressed =
347               gst_navigation_event_get_type (event) ==
348               GST_NAVIGATION_EVENT_KEY_PRESS;
349           wpe_view_backend_dispatch_keyboard_event (src->view->backend (),
350               &wpe_event);
351           ret = TRUE;
352         }
353         break;
354       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
355       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:
356         if (gst_navigation_event_parse_mouse_button_event (event, &button, &x,
357                 &y)) {
358           struct wpe_input_pointer_event wpe_event;
359           wpe_event.type = wpe_input_pointer_event_type_button;
360           wpe_event.x = (int) x;
361           wpe_event.y = (int) y;
362           if (button == 1) {
363             wpe_event.modifiers = wpe_input_pointer_modifier_button1;
364           } else if (button == 2) {
365             wpe_event.modifiers = wpe_input_pointer_modifier_button2;
366           } else if (button == 3) {
367             wpe_event.modifiers = wpe_input_pointer_modifier_button3;
368           } else if (button == 4) {
369             wpe_event.modifiers = wpe_input_pointer_modifier_button4;
370           } else if (button == 5) {
371             wpe_event.modifiers = wpe_input_pointer_modifier_button5;
372           }
373           wpe_event.button = button;
374           wpe_event.state = 1;
375           wpe_event.state =
376               gst_navigation_event_get_type (event) ==
377               GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
378           wpe_view_backend_dispatch_pointer_event (src->view->backend (),
379               &wpe_event);
380           ret = TRUE;
381         }
382         break;
383       case GST_NAVIGATION_EVENT_MOUSE_MOVE:
384         if (gst_navigation_event_parse_mouse_move_event (event, &x, &y)) {
385           struct wpe_input_pointer_event wpe_event;
386           wpe_event.type = wpe_input_pointer_event_type_motion;
387           wpe_event.x = (int) x;
388           wpe_event.y = (int) y;
389           wpe_view_backend_dispatch_pointer_event (src->view->backend (),
390               &wpe_event);
391           ret = TRUE;
392         }
393         break;
394       default:
395         break;
396     }
397     /* FIXME: No touch events handling support in GstNavigation */
398   }
399
400   if (!ret) {
401     ret = gst_pad_event_default (pad, parent, event);
402   }
403   return ret;
404 }
405
406 static void
407 gst_wpe_src_init (GstWpeSrc * src)
408 {
409   GstPad *pad = gst_element_get_static_pad (GST_ELEMENT_CAST (src), "src");
410
411   gst_pad_set_event_function (pad, gst_wpe_src_event);
412   gst_object_unref (pad);
413
414   src->draw_background = TRUE;
415
416   gst_base_src_set_live (GST_BASE_SRC_CAST (src), TRUE);
417 }
418
419 static GstURIType
420 gst_wpe_src_uri_get_type (GType)
421 {
422   return GST_URI_SRC;
423 }
424
425 static const gchar *const *
426 gst_wpe_src_get_protocols (GType)
427 {
428   static const char *protocols[] = { "wpe", NULL };
429   return protocols;
430 }
431
432 static gchar *
433 gst_wpe_src_get_uri (GstURIHandler * handler)
434 {
435   GstWpeSrc *src = GST_WPE_SRC (handler);
436   return g_strdup_printf ("wpe://%s", src->location);
437 }
438
439 static gboolean
440 gst_wpe_src_set_uri (GstURIHandler * handler, const gchar * uri,
441     GError ** error)
442 {
443   GstWpeSrc *src = GST_WPE_SRC (handler);
444
445   return gst_wpe_src_set_location (src, uri + 6, error);
446 }
447
448 static void
449 gst_wpe_src_uri_handler_init (gpointer iface_ptr, gpointer data)
450 {
451   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) iface_ptr;
452
453   iface->get_type = gst_wpe_src_uri_get_type;
454   iface->get_protocols = gst_wpe_src_get_protocols;
455   iface->get_uri = gst_wpe_src_get_uri;
456   iface->set_uri = gst_wpe_src_set_uri;
457 }
458
459 static void
460 gst_wpe_src_class_init (GstWpeSrcClass * klass)
461 {
462   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
463   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
464   GstGLBaseSrcClass *gl_base_src_class = GST_GL_BASE_SRC_CLASS (klass);
465   GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
466
467   gobject_class->set_property = gst_wpe_src_set_property;
468   gobject_class->get_property = gst_wpe_src_get_property;
469
470   g_object_class_install_property (gobject_class, PROP_LOCATION,
471       g_param_spec_string ("location", "location",
472           "The URL to display",
473           "", (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
474   g_object_class_install_property (gobject_class, PROP_DRAW_BACKGROUND,
475       g_param_spec_boolean ("draw-background", "Draws the background",
476           "Whether to draw the WebView background", TRUE,
477           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
478
479   gst_element_class_set_static_metadata (gstelement_class,
480       "WPE source", "Source/Video",
481       "Creates a video stream from a WPE browser",
482       "Philippe Normand <philn@igalia.com>, Žan Doberšek <zdobersek@igalia.com>");
483
484   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
485
486   base_src_class->fixate = GST_DEBUG_FUNCPTR (gst_wpe_src_fixate);
487
488   gl_base_src_class->supported_gl_api =
489       static_cast < GstGLAPI >
490       (GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2);
491   gl_base_src_class->gl_start = GST_DEBUG_FUNCPTR (gst_wpe_src_gl_start);
492   gl_base_src_class->gl_stop = GST_DEBUG_FUNCPTR (gst_wpe_src_gl_stop);
493   gl_base_src_class->fill_gl_memory =
494       GST_DEBUG_FUNCPTR (gst_wpe_src_fill_memory);
495
496   /**
497    * GstWpeSrc::configure-web-view:
498    * @src: the object which received the signal
499    * @webview: the webView
500    *
501    * Allow application to configure the webView settings.
502    */
503   gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW] =
504       g_signal_new ("configure-web-view", G_TYPE_FROM_CLASS (klass),
505       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
506       G_TYPE_NONE, 1, G_TYPE_OBJECT);
507
508   /**
509    * GstWpeSrc::load-bytes:
510    * @src: the object which received the signal
511    * @bytes: the GBytes data to load
512    *
513    * Load the specified bytes into the internal webView.
514    */
515   gst_wpe_src_signals[SIGNAL_LOAD_BYTES] =
516       g_signal_new_class_handler ("load-bytes", G_TYPE_FROM_CLASS (klass),
517       static_cast < GSignalFlags > (G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
518       G_CALLBACK (gst_wpe_src_load_bytes), NULL, NULL,
519       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_BYTES);
520 }
521
522 static gboolean
523 plugin_init (GstPlugin * plugin)
524 {
525   GST_DEBUG_CATEGORY_INIT (wpe_src_debug, "wpesrc", 0, "WPE Source");
526
527   return gst_element_register (plugin, "wpesrc", GST_RANK_NONE,
528       GST_TYPE_WPE_SRC);
529 }
530
531 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
532     wpe, "WPE src plugin", plugin_init, VERSION, GST_LICENSE, PACKAGE,
533     GST_PACKAGE_ORIGIN)