ebdeae652807abb1436969f6db5df233d3cb76cc
[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  * Starting from WPEBackend-FDO 1.6.x, software rendering support is available. This
28  * features allows wpesrc to be used on machines without GPU, and/or for testing
29  * purpose. To enable it, set the `LIBGL_ALWAYS_SOFTWARE=true` environment
30  * variable and make sure `video/x-raw, format=BGRA` caps are negotiated by the
31  * wpesrc element.
32  *
33  * As the webview loading is usually not instantaneous, the wpesrc element emits
34  * messages indicating the load progress, in percent. The value is an estimate
35  * based on the total number of bytes expected to be received for a document,
36  * including all its possible subresources and child documents. The application
37  * can handle these `element` messages synchronously for instance, in order to
38  * display a progress bar or other visual load indicator. The load percent value
39  * is stored in the message structure as a double value named
40  * `estimated-load-progress` and the structure name is `wpe-stats`.
41  *
42  * ## Example launch lines
43  *
44  * ```shell
45  * gst-launch-1.0 -v wpesrc location="https://gstreamer.freedesktop.org" ! queue ! glimagesink
46  * ```
47  * Shows the GStreamer website homepage
48  *
49  * ```shell
50  * LIBGL_ALWAYS_SOFTWARE=true gst-launch-1.0 -v wpesrc num-buffers=50 location="https://gstreamer.freedesktop.org" \
51  *   videoconvert ! pngenc ! multifilesink location=/tmp/snapshot-%05d.png
52  * ```
53  * Saves the first 50 video frames generated for the GStreamer website as PNG files in /tmp.
54  *
55  * ```shell
56  * gst-play-1.0 --videosink gtkglsink wpe://https://gstreamer.freedesktop.org
57  * ```
58  * Shows the GStreamer website homepage as played with GstPlayer in a GTK+ window.
59  *
60  * ```shell
61  * gst-launch-1.0  glvideomixer name=m sink_1::zorder=0 ! glimagesink wpesrc location="file:///tmp/asset.html" draw-background=0 \
62  *   ! m. videotestsrc ! queue ! glupload ! glcolorconvert ! m.
63  * ```
64  * Composite WPE with a video stream in a single OpenGL scene.
65  *
66  * ```shell
67  * gst-launch-1.0 glvideomixer name=m sink_1::zorder=0 sink_0::height=818 sink_0::width=1920 ! gtkglsink \
68  *    wpesrc location="file:///tmp/asset.html" draw-background=0 ! m.
69  *    uridecodebin uri="http://example.com/Sintel.2010.1080p.mkv" name=d d. ! queue ! glupload ! glcolorconvert ! m.
70  * ```
71  * Composite WPE with a video stream, sink_0 pad properties have to match the video dimensions.
72  *
73  * Since: 1.16
74  */
75
76 /*
77  * TODO:
78  * - Audio support (requires an AudioSession implementation in WebKit and a WPEBackend-fdo API for it)
79  * - DMABuf support (requires changes in WPEBackend-fdo to expose DMABuf planes and fds)
80  * - Custom EGLMemory allocator
81  * - Better navigation events handling (would require a new GstNavigation API)
82  */
83
84 #ifdef HAVE_CONFIG_H
85 #include <config.h>
86 #endif
87
88 #include "gstwpesrc.h"
89 #include <gst/gl/gl.h>
90 #include <gst/gl/egl/gstglmemoryegl.h>
91 #include <gst/gl/wayland/gstgldisplay_wayland.h>
92 #include <gst/video/video.h>
93 #include <xkbcommon/xkbcommon.h>
94
95 #include "WPEThreadedView.h"
96
97 GST_DEBUG_CATEGORY (wpe_src_debug);
98 #define GST_CAT_DEFAULT wpe_src_debug
99
100 #define DEFAULT_WIDTH 1920
101 #define DEFAULT_HEIGHT 1080
102 #define DEFAULT_FPS_N 30
103 #define DEFAULT_FPS_D 1
104
105 enum
106 {
107   PROP_0,
108   PROP_LOCATION,
109   PROP_DRAW_BACKGROUND
110 };
111
112 enum
113 {
114   SIGNAL_CONFIGURE_WEB_VIEW,
115   SIGNAL_LOAD_BYTES,
116   LAST_SIGNAL
117 };
118 static guint gst_wpe_src_signals[LAST_SIGNAL] = { 0 };
119
120 struct _GstWpeSrc
121 {
122   GstGLBaseSrc parent;
123
124   /* properties */
125   gchar *location;
126   gboolean draw_background;
127
128   GBytes *bytes;
129   gboolean gl_enabled;
130
131   gint64 n_frames;              /* total frames sent */
132
133   WPEView *view;
134
135   GMutex lock;
136 };
137
138 #define WPE_LOCK(o) g_mutex_lock(&(o)->lock)
139 #define WPE_UNLOCK(o) g_mutex_unlock(&(o)->lock)
140
141 static void gst_wpe_src_uri_handler_init (gpointer iface, gpointer data);
142
143 #define gst_wpe_src_parent_class parent_class
144 G_DEFINE_TYPE_WITH_CODE (GstWpeSrc, gst_wpe_src, GST_TYPE_GL_BASE_SRC,
145     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_wpe_src_uri_handler_init));
146
147 #if ENABLE_SHM_BUFFER_SUPPORT
148 #define WPE_RAW_CAPS "; video/x-raw, "          \
149   "format = (string) BGRA, "                    \
150   "width = " GST_VIDEO_SIZE_RANGE ", "          \
151   "height = " GST_VIDEO_SIZE_RANGE ", "         \
152   "framerate = " GST_VIDEO_FPS_RANGE ", "       \
153   "pixel-aspect-ratio = (fraction)1/1"
154 #else
155 #define WPE_RAW_CAPS ""
156 #endif
157
158 #define WPE_BASIC_CAPS "video/x-raw(memory:GLMemory), " \
159   "format = (string) RGBA, "                            \
160   "width = " GST_VIDEO_SIZE_RANGE ", "                  \
161   "height = " GST_VIDEO_SIZE_RANGE ", "                 \
162   "framerate = " GST_VIDEO_FPS_RANGE ", "               \
163   "pixel-aspect-ratio = (fraction)1/1, texture-target = (string)2D"
164
165 #define WPE_SRC_CAPS WPE_BASIC_CAPS WPE_RAW_CAPS
166 #define WPE_SRC_DOC_CAPS WPE_BASIC_CAPS "; video/x-raw, format = (string) BGRA"
167
168 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
169     GST_PAD_SRC,
170     GST_PAD_ALWAYS,
171     GST_STATIC_CAPS (WPE_SRC_CAPS));
172
173 static GstFlowReturn
174 gst_wpe_src_create (GstBaseSrc * bsrc, guint64 offset, guint length, GstBuffer ** buf)
175 {
176   GstGLBaseSrc *gl_src = GST_GL_BASE_SRC (bsrc);
177   GstWpeSrc *src = GST_WPE_SRC (bsrc);
178   GstFlowReturn ret = GST_FLOW_ERROR;
179   GstBuffer *locked_buffer;
180   GstClockTime next_time;
181   gint64 ts_offset = 0;
182
183   WPE_LOCK (src);
184   if (src->gl_enabled) {
185     WPE_UNLOCK (src);
186     return GST_CALL_PARENT_WITH_DEFAULT (GST_BASE_SRC_CLASS, create, (bsrc, offset, length, buf), ret);
187   }
188
189   locked_buffer = src->view->buffer ();
190   if (locked_buffer == NULL) {
191     WPE_UNLOCK (src);
192     GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
193         ("WPE View did not render a buffer"), (NULL));
194     return ret;
195   }
196   *buf = gst_buffer_copy_deep (locked_buffer);
197
198   g_object_get(gl_src, "timestamp-offset", &ts_offset, NULL);
199
200   /* The following code mimics the behaviour of GLBaseSrc::fill */
201   GST_BUFFER_TIMESTAMP (*buf) = ts_offset + gl_src->running_time;
202   GST_BUFFER_OFFSET (*buf) = src->n_frames;
203   src->n_frames++;
204   GST_BUFFER_OFFSET_END (*buf) = src->n_frames;
205   if (gl_src->out_info.fps_n) {
206     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
207         gl_src->out_info.fps_d, gl_src->out_info.fps_n);
208     GST_BUFFER_DURATION (*buf) = next_time - gl_src->running_time;
209   } else {
210     next_time = ts_offset;
211     GST_BUFFER_DURATION (*buf) = GST_CLOCK_TIME_NONE;
212   }
213
214   GST_LOG_OBJECT (src, "Created buffer from SHM %" GST_PTR_FORMAT, *buf);
215
216   gl_src->running_time = next_time;
217
218   ret = GST_FLOW_OK;
219   WPE_UNLOCK (src);
220   return ret;
221 }
222
223 static gboolean
224 gst_wpe_src_fill_memory (GstGLBaseSrc * bsrc, GstGLMemory * memory)
225 {
226   GstWpeSrc *src = GST_WPE_SRC (bsrc);
227   const GstGLFuncs *gl;
228   guint tex_id;
229   GstEGLImage *locked_image;
230
231   if (!gst_gl_context_check_feature (GST_GL_CONTEXT (bsrc->context),
232           "EGL_KHR_image_base")) {
233     GST_ERROR_OBJECT (src, "EGL_KHR_image_base is not supported");
234     return FALSE;
235   }
236
237   WPE_LOCK (src);
238
239   gl = bsrc->context->gl_vtable;
240   tex_id = gst_gl_memory_get_texture_id (memory);
241   locked_image = src->view->image ();
242
243   if (!locked_image) {
244     WPE_UNLOCK (src);
245     return TRUE;
246   }
247
248   gl->ActiveTexture (GL_TEXTURE0 + memory->plane);
249   gl->BindTexture (GL_TEXTURE_2D, tex_id);
250   gl->EGLImageTargetTexture2D (GL_TEXTURE_2D,
251       gst_egl_image_get_image (locked_image));
252   gl->Flush ();
253   WPE_UNLOCK (src);
254   return TRUE;
255 }
256
257 static gboolean
258 gst_wpe_src_start (GstWpeSrc * src)
259 {
260   GstGLContext *context = NULL;
261   GstGLDisplay *display = NULL;
262   GstGLBaseSrc *base_src = GST_GL_BASE_SRC (src);
263   gboolean created_view = FALSE;
264
265   GST_INFO_OBJECT (src, "Starting up");
266   WPE_LOCK (src);
267
268   if (src->gl_enabled) {
269     context = base_src->context;
270     display = base_src->display;
271   }
272
273   GST_DEBUG_OBJECT (src, "Will %sfill GLMemories", src->gl_enabled ? "" : "NOT ");
274
275   auto & thread = WPEContextThread::singleton ();
276
277   if (!src->view) {
278     src->view = thread.createWPEView (src, context, display,
279         GST_VIDEO_INFO_WIDTH (&base_src->out_info),
280         GST_VIDEO_INFO_HEIGHT (&base_src->out_info));
281     created_view = TRUE;
282     GST_DEBUG_OBJECT (src, "created view %p", src->view);
283   }
284
285   if (!src->view) {
286     WPE_UNLOCK (src);
287     GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
288         ("WPEBackend-FDO EGL display initialisation failed"), (NULL));
289     return FALSE;
290   }
291
292   if (src->bytes != NULL) {
293     src->view->loadData (src->bytes);
294     g_bytes_unref (src->bytes);
295     src->bytes = NULL;
296   }
297
298   if (created_view) {
299     src->n_frames = 0;
300   }
301   WPE_UNLOCK (src);
302   return TRUE;
303 }
304
305 static gboolean
306 gst_wpe_src_decide_allocation (GstBaseSrc * base_src, GstQuery * query)
307 {
308   GstGLBaseSrc *gl_src = GST_GL_BASE_SRC (base_src);
309   GstWpeSrc *src = GST_WPE_SRC (base_src);
310   GstCapsFeatures *caps_features;
311
312   WPE_LOCK (src);
313   caps_features = gst_caps_get_features (gl_src->out_caps, 0);
314   if (caps_features != NULL && gst_caps_features_contains (caps_features, GST_CAPS_FEATURE_MEMORY_GL_MEMORY)) {
315     src->gl_enabled = TRUE;
316   } else {
317     src->gl_enabled = FALSE;
318   }
319
320   if (src->gl_enabled) {
321     WPE_UNLOCK (src);
322     return GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SRC_CLASS, decide_allocation, (base_src, query), FALSE);
323   }
324   WPE_UNLOCK (src);
325   return gst_wpe_src_start (src);
326 }
327
328 static gboolean
329 gst_wpe_src_gl_start (GstGLBaseSrc * base_src)
330 {
331   GstWpeSrc *src = GST_WPE_SRC (base_src);
332   return gst_wpe_src_start (src);
333 }
334
335 static void
336 gst_wpe_src_stop_unlocked (GstWpeSrc * src)
337 {
338   if (src->view) {
339     GST_DEBUG_OBJECT (src, "deleting view %p", src->view);
340     delete src->view;
341     src->view = NULL;
342   }
343 }
344
345 static void
346 gst_wpe_src_gl_stop (GstGLBaseSrc * base_src)
347 {
348   GstWpeSrc *src = GST_WPE_SRC (base_src);
349
350   WPE_LOCK (src);
351   gst_wpe_src_stop_unlocked (src);
352   WPE_UNLOCK (src);
353 }
354
355 static gboolean
356 gst_wpe_src_stop (GstBaseSrc * base_src)
357 {
358   GstWpeSrc *src = GST_WPE_SRC (base_src);
359
360   /* we can call this always, GstGLBaseSrc is smart enough to not crash if
361    * gst_gl_base_src_gl_start() has not been called from chaining up
362    * gst_wpe_src_decide_allocation() */
363   if (!GST_CALL_PARENT_WITH_DEFAULT(GST_BASE_SRC_CLASS, stop, (base_src), FALSE))
364     return FALSE;
365
366   WPE_LOCK (src);
367
368   /* if gl-enabled, gst_wpe_src_stop_unlocked() would have already been called
369    * inside gst_wpe_src_gl_stop() from the base class stopping the OpenGL
370    * context */
371   if (!src->gl_enabled)
372     gst_wpe_src_stop_unlocked (src);
373
374   WPE_UNLOCK (src);
375   return TRUE;
376 }
377
378 static GstCaps *
379 gst_wpe_src_fixate (GstBaseSrc * base_src, GstCaps * caps)
380 {
381   GstWpeSrc *src = GST_WPE_SRC (base_src);
382   GstStructure *structure;
383   gint width, height;
384
385   caps = gst_caps_make_writable (caps);
386   structure = gst_caps_get_structure (caps, 0);
387
388   gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
389   gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
390
391   if (gst_structure_has_field (structure, "framerate"))
392     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
393         DEFAULT_FPS_N, DEFAULT_FPS_D);
394   else
395     gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, DEFAULT_FPS_N,
396         DEFAULT_FPS_D, NULL);
397
398   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (base_src, caps);
399   GST_INFO_OBJECT (base_src, "Fixated caps to %" GST_PTR_FORMAT, caps);
400
401   if (src->view) {
402     gst_structure_get (structure, "width", G_TYPE_INT, &width, "height", G_TYPE_INT, &height, NULL);
403     src->view->resize (width, height);
404   }
405   return caps;
406 }
407
408 void
409 gst_wpe_src_configure_web_view (GstWpeSrc * src, WebKitWebView * webview)
410 {
411   GValue args[2] = { {0}, {0} };
412
413   g_value_init (&args[0], GST_TYPE_ELEMENT);
414   g_value_set_object (&args[0], src);
415   g_value_init (&args[1], G_TYPE_OBJECT);
416   g_value_set_object (&args[1], webview);
417
418   g_signal_emitv (args, gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW], 0,
419       NULL);
420
421   g_value_unset (&args[0]);
422   g_value_unset (&args[1]);
423 }
424
425 static void
426 gst_wpe_src_load_bytes (GstWpeSrc * src, GBytes * bytes)
427 {
428   if (src->view && GST_STATE (GST_ELEMENT_CAST (src)) > GST_STATE_NULL) {
429     src->view->loadData (bytes);
430   } else {
431     if (src->bytes)
432       g_bytes_unref (src->bytes);
433     src->bytes = g_bytes_ref (bytes);
434   }
435 }
436
437 static gboolean
438 gst_wpe_src_set_location (GstWpeSrc * src, const gchar * location,
439     GError ** error)
440 {
441   g_free (src->location);
442   src->location = g_strdup (location);
443   if (src->view)
444     src->view->loadUri (src->location);
445
446   return TRUE;
447 }
448
449 static void
450 gst_wpe_src_set_draw_background (GstWpeSrc * src, gboolean draw_background)
451 {
452   if (src->view)
453     src->view->setDrawBackground (draw_background);
454   src->draw_background = draw_background;
455 }
456
457 static void
458 gst_wpe_src_set_property (GObject * object, guint prop_id, const GValue * value,
459     GParamSpec * pspec)
460 {
461   GstWpeSrc *src = GST_WPE_SRC (object);
462
463   switch (prop_id) {
464     case PROP_LOCATION:
465     {
466       const gchar *location;
467
468       location = g_value_get_string (value);
469       if (location == NULL) {
470         GST_WARNING_OBJECT (src, "location property cannot be NULL");
471         return;
472       }
473
474       if (!gst_wpe_src_set_location (src, location, NULL)) {
475         GST_WARNING_OBJECT (src, "badly formatted location");
476         return;
477       }
478       break;
479     }
480     case PROP_DRAW_BACKGROUND:
481       gst_wpe_src_set_draw_background (src, g_value_get_boolean (value));
482       break;
483     default:
484       break;
485   }
486 }
487
488 static void
489 gst_wpe_src_get_property (GObject * object, guint prop_id, GValue * value,
490     GParamSpec * pspec)
491 {
492   GstWpeSrc *src = GST_WPE_SRC (object);
493
494   switch (prop_id) {
495     case PROP_LOCATION:
496       g_value_set_string (value, src->location);
497       break;
498     case PROP_DRAW_BACKGROUND:
499       g_value_set_boolean (value, src->draw_background);
500       break;
501     default:
502       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
503       break;
504   }
505 }
506
507 static gboolean
508 gst_wpe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
509 {
510   gboolean ret = FALSE;
511   GstWpeSrc *src = GST_WPE_SRC (parent);
512
513   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION) {
514     const gchar *key;
515     gint button;
516     gdouble x, y, delta_x, delta_y;
517
518     GST_DEBUG_OBJECT (src, "Processing event %" GST_PTR_FORMAT, event);
519     if (!src->view) {
520       return FALSE;
521     }
522     switch (gst_navigation_event_get_type (event)) {
523       case GST_NAVIGATION_EVENT_KEY_PRESS:
524       case GST_NAVIGATION_EVENT_KEY_RELEASE:
525         if (gst_navigation_event_parse_key_event (event, &key)) {
526           /* FIXME: This is wrong... The GstNavigation API should pass
527              hardware-level information, not high-level keysym strings */
528           uint32_t keysym =
529               (uint32_t) xkb_keysym_from_name (key, XKB_KEYSYM_NO_FLAGS);
530           struct wpe_input_keyboard_event wpe_event;
531           wpe_event.key_code = keysym;
532           wpe_event.pressed =
533               gst_navigation_event_get_type (event) ==
534               GST_NAVIGATION_EVENT_KEY_PRESS;
535           src->view->dispatchKeyboardEvent (wpe_event);
536           ret = TRUE;
537         }
538         break;
539       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
540       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:
541         if (gst_navigation_event_parse_mouse_button_event (event, &button, &x,
542                 &y)) {
543           struct wpe_input_pointer_event wpe_event;
544           wpe_event.time = GST_TIME_AS_MSECONDS (GST_EVENT_TIMESTAMP (event));
545           wpe_event.type = wpe_input_pointer_event_type_button;
546           wpe_event.x = (int) x;
547           wpe_event.y = (int) y;
548           if (button == 1) {
549             wpe_event.modifiers = wpe_input_pointer_modifier_button1;
550           } else if (button == 2) {
551             wpe_event.modifiers = wpe_input_pointer_modifier_button2;
552           } else if (button == 3) {
553             wpe_event.modifiers = wpe_input_pointer_modifier_button3;
554           } else if (button == 4) {
555             wpe_event.modifiers = wpe_input_pointer_modifier_button4;
556           } else if (button == 5) {
557             wpe_event.modifiers = wpe_input_pointer_modifier_button5;
558           }
559           wpe_event.button = button;
560           wpe_event.state =
561               gst_navigation_event_get_type (event) ==
562               GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
563           src->view->dispatchPointerEvent (wpe_event);
564           ret = TRUE;
565         }
566         break;
567       case GST_NAVIGATION_EVENT_MOUSE_MOVE:
568         if (gst_navigation_event_parse_mouse_move_event (event, &x, &y)) {
569           struct wpe_input_pointer_event wpe_event;
570           wpe_event.time = GST_TIME_AS_MSECONDS (GST_EVENT_TIMESTAMP (event));
571           wpe_event.type = wpe_input_pointer_event_type_motion;
572           wpe_event.x = (int) x;
573           wpe_event.y = (int) y;
574           src->view->dispatchPointerEvent (wpe_event);
575           ret = TRUE;
576         }
577         break;
578       case GST_NAVIGATION_EVENT_MOUSE_SCROLL:
579         if (gst_navigation_event_parse_mouse_scroll_event (event, &x, &y,
580                 &delta_x, &delta_y)) {
581 #if WPE_CHECK_VERSION(1, 6, 0)
582           struct wpe_input_axis_2d_event wpe_event;
583           if (delta_x) {
584             wpe_event.x_axis = delta_x;
585           } else {
586             wpe_event.y_axis = delta_y;
587           }
588           wpe_event.base.time =
589               GST_TIME_AS_MSECONDS (GST_EVENT_TIMESTAMP (event));
590           wpe_event.base.type =
591               static_cast < wpe_input_axis_event_type >
592               (wpe_input_axis_event_type_mask_2d |
593               wpe_input_axis_event_type_motion_smooth);
594           wpe_event.base.x = (int) x;
595           wpe_event.base.y = (int) y;
596           src->view->dispatchAxisEvent (wpe_event.base);
597 #else
598           struct wpe_input_axis_event wpe_event;
599           if (delta_x) {
600             wpe_event.axis = 1;
601             wpe_event.value = delta_x;
602           } else {
603             wpe_event.axis = 0;
604             wpe_event.value = delta_y;
605           }
606           wpe_event.time = GST_TIME_AS_MSECONDS (GST_EVENT_TIMESTAMP (event));
607           wpe_event.type = wpe_input_axis_event_type_motion;
608           wpe_event.x = (int) x;
609           wpe_event.y = (int) y;
610           src->view->dispatchAxisEvent (wpe_event);
611 #endif
612           ret = TRUE;
613         }
614         break;
615       default:
616         break;
617     }
618     /* FIXME: No touch events handling support in GstNavigation */
619   }
620
621   if (!ret) {
622     ret = gst_pad_event_default (pad, parent, event);
623   } else {
624     gst_event_unref (event);
625   }
626   return ret;
627 }
628
629 static void
630 gst_wpe_src_init (GstWpeSrc * src)
631 {
632   GstPad *pad = gst_element_get_static_pad (GST_ELEMENT_CAST (src), "src");
633
634   gst_pad_set_event_function (pad, gst_wpe_src_event);
635   gst_object_unref (pad);
636
637   src->draw_background = TRUE;
638
639   gst_base_src_set_live (GST_BASE_SRC_CAST (src), TRUE);
640
641   g_mutex_init (&src->lock);
642 }
643
644 static void
645 gst_wpe_src_finalize (GObject * object)
646 {
647   GstWpeSrc *src = GST_WPE_SRC (object);
648
649   g_mutex_clear (&src->lock);
650
651   G_OBJECT_CLASS (parent_class)->finalize (object);
652 }
653
654 static GstURIType
655 gst_wpe_src_uri_get_type (GType)
656 {
657   return GST_URI_SRC;
658 }
659
660 static const gchar *const *
661 gst_wpe_src_get_protocols (GType)
662 {
663   static const char *protocols[] = { "wpe", NULL };
664   return protocols;
665 }
666
667 static gchar *
668 gst_wpe_src_get_uri (GstURIHandler * handler)
669 {
670   GstWpeSrc *src = GST_WPE_SRC (handler);
671   return g_strdup_printf ("wpe://%s", src->location);
672 }
673
674 static gboolean
675 gst_wpe_src_set_uri (GstURIHandler * handler, const gchar * uri,
676     GError ** error)
677 {
678   GstWpeSrc *src = GST_WPE_SRC (handler);
679
680   return gst_wpe_src_set_location (src, uri + 6, error);
681 }
682
683 static void
684 gst_wpe_src_uri_handler_init (gpointer iface_ptr, gpointer data)
685 {
686   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) iface_ptr;
687
688   iface->get_type = gst_wpe_src_uri_get_type;
689   iface->get_protocols = gst_wpe_src_get_protocols;
690   iface->get_uri = gst_wpe_src_get_uri;
691   iface->set_uri = gst_wpe_src_set_uri;
692 }
693
694 static void
695 gst_wpe_src_class_init (GstWpeSrcClass * klass)
696 {
697   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
698   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
699   GstGLBaseSrcClass *gl_base_src_class = GST_GL_BASE_SRC_CLASS (klass);
700   GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
701   GstPadTemplate *tmpl;
702   GstCaps *doc_caps;
703
704   gobject_class->set_property = gst_wpe_src_set_property;
705   gobject_class->get_property = gst_wpe_src_get_property;
706   gobject_class->finalize = gst_wpe_src_finalize;
707
708   g_object_class_install_property (gobject_class, PROP_LOCATION,
709       g_param_spec_string ("location", "location",
710           "The URL to display",
711           "", (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
712   g_object_class_install_property (gobject_class, PROP_DRAW_BACKGROUND,
713       g_param_spec_boolean ("draw-background", "Draws the background",
714           "Whether to draw the WebView background", TRUE,
715           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
716
717   gst_element_class_set_static_metadata (gstelement_class,
718       "WPE source", "Source/Video",
719       "Creates a video stream from a WPE browser",
720       "Philippe Normand <philn@igalia.com>, Žan Doberšek <zdobersek@igalia.com>");
721
722   tmpl = gst_static_pad_template_get (&src_factory);
723   gst_element_class_add_pad_template (gstelement_class, tmpl);
724
725   base_src_class->fixate = GST_DEBUG_FUNCPTR (gst_wpe_src_fixate);
726   base_src_class->create = GST_DEBUG_FUNCPTR (gst_wpe_src_create);
727   base_src_class->decide_allocation = GST_DEBUG_FUNCPTR (gst_wpe_src_decide_allocation);
728   base_src_class->stop = GST_DEBUG_FUNCPTR (gst_wpe_src_stop);
729
730   gl_base_src_class->supported_gl_api =
731       static_cast < GstGLAPI >
732       (GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2);
733   gl_base_src_class->gl_start = GST_DEBUG_FUNCPTR (gst_wpe_src_gl_start);
734   gl_base_src_class->gl_stop = GST_DEBUG_FUNCPTR (gst_wpe_src_gl_stop);
735   gl_base_src_class->fill_gl_memory =
736       GST_DEBUG_FUNCPTR (gst_wpe_src_fill_memory);
737
738   doc_caps = gst_caps_from_string (WPE_SRC_DOC_CAPS);
739   gst_pad_template_set_documentation_caps (tmpl, doc_caps);
740   gst_clear_caps (&doc_caps);
741
742   /**
743    * GstWpeSrc::configure-web-view:
744    * @src: the object which received the signal
745    * @webview: the webView
746    *
747    * Allow application to configure the webView settings.
748    */
749   gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW] =
750       g_signal_new ("configure-web-view", G_TYPE_FROM_CLASS (klass),
751       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT);
752
753   /**
754    * GstWpeSrc::load-bytes:
755    * @src: the object which received the signal
756    * @bytes: the GBytes data to load
757    *
758    * Load the specified bytes into the internal webView.
759    */
760   gst_wpe_src_signals[SIGNAL_LOAD_BYTES] =
761       g_signal_new_class_handler ("load-bytes", G_TYPE_FROM_CLASS (klass),
762       static_cast < GSignalFlags > (G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
763       G_CALLBACK (gst_wpe_src_load_bytes), NULL, NULL, NULL,
764       G_TYPE_NONE, 1, G_TYPE_BYTES);
765 }
766
767 static gboolean
768 plugin_init (GstPlugin * plugin)
769 {
770   GST_DEBUG_CATEGORY_INIT (wpe_src_debug, "wpesrc", 0, "WPE Source");
771
772   return gst_element_register (plugin, "wpesrc", GST_RANK_NONE,
773       GST_TYPE_WPE_SRC);
774 }
775
776 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
777     wpe, "WPE src plugin", plugin_init, VERSION, GST_LICENSE, PACKAGE,
778     GST_PACKAGE_ORIGIN)