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/video/video.h>
64 #include <gst/gl/gl.h>
65 #include <gst/gl/egl/gstglmemoryegl.h>
66 #include <gst/gl/wayland/gstgldisplay_wayland.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 #define SUPPORTED_GL_APIS static_cast<GstGLAPI>(GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2)
80 
81 enum
82 {
83   PROP_0,
84   PROP_LOCATION,
85   PROP_DRAW_BACKGROUND
86 };
87 
88 enum
89 {
90   SIGNAL_CONFIGURE_WEB_VIEW,
91   LAST_SIGNAL
92 };
93 static guint gst_wpe_src_signals[LAST_SIGNAL] = { 0 };
94 
95 struct _GstWpeSrc
96 {
97   GstPushSrc parent;
98   GstGLDisplay *display;
99   GstGLContext *context, *other_context;
100   GstVideoInfo out_info;
101   GstCaps *out_caps;
102   GstGLMemoryAllocator *allocator;
103   GstGLVideoAllocationParams *gl_alloc_params;
104   guint64 n_frames;
105   gchar *location;
106   gboolean draw_background;
107   gboolean negotiated;
108   WPEThreadedView *view;
109 };
110 
111 static void gst_wpe_src_uri_handler_init (gpointer iface, gpointer data);
112 
113 #define gst_wpe_src_parent_class parent_class
114 G_DEFINE_TYPE_WITH_CODE (GstWpeSrc, gst_wpe_src, GST_TYPE_PUSH_SRC,
115     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_wpe_src_uri_handler_init));
116 
117 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
118     GST_PAD_SRC,
119     GST_PAD_ALWAYS,
120     GST_STATIC_CAPS ("video/x-raw(memory:GLMemory), "
121         "format = (string) RGBA, "
122         "width = " GST_VIDEO_SIZE_RANGE ", "
123         "height = " GST_VIDEO_SIZE_RANGE ", "
124         "framerate = " GST_VIDEO_FPS_RANGE ", "
125         "pixel-aspect-ratio = (fraction)1/1")
126     );
127 
128 static GstFlowReturn
gst_wpe_src_create(GstPushSrc * psrc,GstBuffer ** buffer)129 gst_wpe_src_create (GstPushSrc * psrc, GstBuffer ** buffer)
130 {
131   GstWpeSrc *src = GST_WPE_SRC (psrc);
132   GstGLSyncMeta *sync_meta;
133   GstEGLImage *img = src->view->image ();
134   GstGLVideoAllocationParams *alloc_params = src->gl_alloc_params;
135   GstGLFormat formats[1] { GST_GL_RGBA };
136   gpointer imgs[1] { NULL };
137 
138   if (G_UNLIKELY (!src->negotiated || !src->context))
139     goto not_negotiated;
140 
141   g_return_val_if_fail(img != NULL, GST_FLOW_ERROR);
142 
143   *buffer = gst_buffer_new ();
144   imgs[0] = (gpointer) img;
145   alloc_params->parent.gl_handle = img;
146   alloc_params->plane = 0;
147   gst_gl_memory_setup_buffer (src->allocator, *buffer, alloc_params, formats, imgs, 1);
148 
149   sync_meta = gst_buffer_get_gl_sync_meta (*buffer);
150   if (sync_meta)
151     gst_gl_sync_meta_set_sync_point (sync_meta, src->context);
152 
153   GST_BUFFER_OFFSET (*buffer) = src->n_frames;
154   src->n_frames++;
155   GST_BUFFER_OFFSET_END (*buffer) = src->n_frames;
156 
157   return GST_FLOW_OK;
158 
159 not_negotiated:
160   {
161     GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
162         ("format wasn't negotiated before get function"));
163     return GST_FLOW_NOT_NEGOTIATED;
164   }
165 }
166 
167 static gboolean
gst_wpe_src_start(GstBaseSrc * base_src)168 gst_wpe_src_start (GstBaseSrc * base_src)
169 {
170   GstWpeSrc *src = GST_WPE_SRC (base_src);
171 
172   GST_INFO_OBJECT (src, "Starting up");
173   GST_OBJECT_LOCK (src);
174 
175   src->n_frames = 0;
176   src->negotiated = FALSE;
177   src->allocator = GST_GL_MEMORY_ALLOCATOR (gst_allocator_find (GST_GL_MEMORY_EGL_ALLOCATOR_NAME));
178   src->gl_alloc_params = NULL;
179   src->view = new WPEThreadedView;
180 
181   GST_OBJECT_UNLOCK (src);
182   return TRUE;
183 }
184 
185 static gboolean
gst_wpe_src_stop(GstBaseSrc * base_src)186 gst_wpe_src_stop (GstBaseSrc * base_src)
187 {
188   GstWpeSrc *src = GST_WPE_SRC (base_src);
189 
190   GST_INFO_OBJECT (src, "Stopping");
191   GST_OBJECT_LOCK (src);
192 
193   if (src->gl_alloc_params) {
194     gst_gl_allocation_params_free ((GstGLAllocationParams *)
195         src->gl_alloc_params);
196     src->gl_alloc_params = NULL;
197   }
198 
199   gst_caps_replace (&src->out_caps, NULL);
200 
201   if (src->context)
202     g_clear_object (&src->context);
203 
204   delete src->view;
205   src->view = NULL;
206 
207   if (src->allocator)
208     g_clear_object (&src->allocator);
209 
210   GST_OBJECT_UNLOCK (src);
211   return TRUE;
212 }
213 
214 static void
gst_wpe_src_get_times(GstBaseSrc * base_src,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)215 gst_wpe_src_get_times (GstBaseSrc * base_src, GstBuffer * buffer,
216     GstClockTime * start, GstClockTime * end)
217 {
218   GstClockTime timestamp = GST_BUFFER_PTS (buffer);
219   GstClockTime duration = GST_BUFFER_DURATION (buffer);
220 
221   *end = timestamp + duration;
222   *start = timestamp;
223 
224   GST_LOG_OBJECT (base_src,
225       "Got times start: %" GST_TIME_FORMAT " end: %" GST_TIME_FORMAT,
226       GST_TIME_ARGS (*start), GST_TIME_ARGS (*end));
227 }
228 
229 static gboolean
gst_wpe_src_query(GstBaseSrc * base_src,GstQuery * query)230 gst_wpe_src_query (GstBaseSrc * base_src, GstQuery * query)
231 {
232   gboolean res = FALSE;
233   GstWpeSrc *src = GST_WPE_SRC (base_src);
234 
235   switch (GST_QUERY_TYPE (query)) {
236     case GST_QUERY_CONTEXT:
237     {
238       if (gst_gl_handle_context_query ((GstElement *) src, query,
239               src->display, src->context, src->other_context)) {
240 
241         return TRUE;
242       }
243       break;
244     }
245     case GST_QUERY_CONVERT:
246     {
247       GstFormat src_fmt, dest_fmt;
248       gint64 src_val, dest_val;
249 
250       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
251       res =
252           gst_video_info_convert (&src->out_info, src_fmt, src_val, dest_fmt,
253           &dest_val);
254       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
255 
256       return res;
257     }
258     default:
259       res = GST_BASE_SRC_CLASS (parent_class)->query (base_src, query);
260       break;
261   }
262 
263   return res;
264 }
265 
266 static GstCaps *
gst_wpe_src_fixate(GstBaseSrc * base_src,GstCaps * caps)267 gst_wpe_src_fixate (GstBaseSrc * base_src, GstCaps * caps)
268 {
269   GstStructure *structure;
270 
271   caps = gst_caps_make_writable (caps);
272   structure = gst_caps_get_structure (caps, 0);
273 
274   gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
275   gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
276 
277   if (gst_structure_has_field (structure, "framerate"))
278     gst_structure_fixate_field_nearest_fraction (structure, "framerate",
279         DEFAULT_FPS_N, DEFAULT_FPS_D);
280   else
281     gst_structure_set (structure, "framerate", GST_TYPE_FRACTION, DEFAULT_FPS_N,
282         DEFAULT_FPS_D, NULL);
283 
284   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (base_src, caps);
285   GST_INFO_OBJECT (base_src, "Fixated caps to %" GST_PTR_FORMAT, caps);
286   return caps;
287 }
288 
289 static gboolean
gst_wpe_src_set_caps(GstBaseSrc * base_src,GstCaps * caps)290 gst_wpe_src_set_caps (GstBaseSrc * base_src, GstCaps * caps)
291 {
292   GstWpeSrc *src = GST_WPE_SRC (base_src);
293 
294   GST_INFO_OBJECT (base_src, "Caps set to %" GST_PTR_FORMAT, caps);
295   if (!gst_video_info_from_caps (&src->out_info, caps))
296     goto wrong_caps;
297 
298   if (src->view) {
299     src->view->resize (GST_VIDEO_INFO_WIDTH (&src->out_info),
300         GST_VIDEO_INFO_HEIGHT (&src->out_info));
301   }
302 
303   src->negotiated = TRUE;
304   gst_caps_replace (&src->out_caps, caps);
305   return TRUE;
306 
307 wrong_caps:
308   {
309     GST_WARNING ("wrong caps");
310     return FALSE;
311   }
312 }
313 
314 static void
gst_wpe_src_set_context(GstElement * element,GstContext * context)315 gst_wpe_src_set_context (GstElement * element, GstContext * context)
316 {
317   GstWpeSrc *src = GST_WPE_SRC (element);
318 
319   gst_gl_handle_set_context (element, context, &src->display,
320       &src->other_context);
321 
322   if (src->display)
323     gst_gl_display_filter_gl_api (src->display, SUPPORTED_GL_APIS);
324 
325   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
326 }
327 
328 static gboolean
_find_local_gl_context(GstWpeSrc * src)329 _find_local_gl_context (GstWpeSrc * src)
330 {
331   if (gst_gl_query_local_gl_context (GST_ELEMENT (src), GST_PAD_SRC,
332           &src->context))
333     return TRUE;
334   return FALSE;
335 }
336 
337 static void
_src_initialize_wpe_view(GstGLContext * context,GstWpeSrc * src)338 _src_initialize_wpe_view (GstGLContext * context, GstWpeSrc * src)
339 {
340   src->view->initialize (src, context, src->display,
341       GST_VIDEO_INFO_WIDTH (&src->out_info),
342       GST_VIDEO_INFO_HEIGHT (&src->out_info));
343 }
344 
345 void
gst_wpe_src_configure_web_view(GstWpeSrc * src,WebKitWebView * webview)346 gst_wpe_src_configure_web_view (GstWpeSrc * src, WebKitWebView * webview)
347 {
348   GValue args[2] = { {0}, {0} };
349 
350   g_value_init (&args[0], GST_TYPE_ELEMENT);
351   g_value_set_object (&args[0], src);
352   g_value_init (&args[1], G_TYPE_OBJECT);
353   g_value_set_object (&args[1], webview);
354 
355   g_signal_emitv (args, gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW], 0,
356       NULL);
357 
358   g_value_unset (&args[0]);
359   g_value_unset (&args[1]);
360 }
361 
362 static gboolean
gst_wpe_src_decide_allocation(GstBaseSrc * basesrc,GstQuery * query)363 gst_wpe_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
364 {
365   GstWpeSrc *src = GST_WPE_SRC (basesrc);
366   GstBufferPool *pool = NULL;
367   GstStructure *config;
368   GstCaps *caps;
369   guint min, max, size;
370   gboolean update_pool;
371   GError *error = NULL;
372   GstAllocationParams *alloc_params = NULL;
373   guint plane = 1;
374   GstVideoAlignment *valign = NULL;
375   GstGLTextureTarget target = GST_GL_TEXTURE_TARGET_2D;
376   GstGLFormat tex_format = GST_GL_RGBA;
377 
378   if (!gst_gl_ensure_element_data (src, &src->display, &src->other_context))
379     return FALSE;
380 
381   gst_gl_display_filter_gl_api (src->display, SUPPORTED_GL_APIS);
382 
383   _find_local_gl_context (src);
384 
385   if (!src->context) {
386     GST_OBJECT_LOCK (src->display);
387     do {
388       if (src->context) {
389         gst_object_unref (src->context);
390         src->context = NULL;
391       }
392 
393       src->context =
394           gst_gl_display_get_gl_context_for_thread (src->display, NULL);
395       if (!src->context) {
396         if (!gst_gl_display_create_context (src->display, src->other_context,
397                 &src->context, &error)) {
398           GST_OBJECT_UNLOCK (src->display);
399           goto context_error;
400         }
401       }
402     } while (!gst_gl_display_add_context (src->display, src->context));
403     GST_OBJECT_UNLOCK (src->display);
404   }
405 
406   if ((gst_gl_context_get_gl_api (src->context) & SUPPORTED_GL_APIS) == 0)
407     goto unsupported_gl_api;
408 
409   gst_gl_context_thread_add (src->context,
410       (GstGLContextThreadFunc) _src_initialize_wpe_view, src);
411 
412   gst_query_parse_allocation (query, &caps, NULL);
413 
414   if (gst_query_get_n_allocation_pools (query) > 0) {
415     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
416     update_pool = TRUE;
417   } else {
418     GstVideoInfo vinfo;
419 
420     gst_video_info_init (&vinfo);
421     gst_video_info_from_caps (&vinfo, caps);
422     size = vinfo.size;
423     min = max = 1;
424     update_pool = FALSE;
425   }
426 
427   if (!pool || !GST_IS_GL_BUFFER_POOL (pool)) {
428     /* can't use this pool */
429     if (pool)
430       gst_object_unref (pool);
431     pool = gst_gl_buffer_pool_new (src->context);
432   }
433   config = gst_buffer_pool_get_config (pool);
434 
435   gst_buffer_pool_config_set_params (config, caps, size, min, max);
436   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
437   if (gst_query_find_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, NULL))
438     gst_buffer_pool_config_add_option (config,
439         GST_BUFFER_POOL_OPTION_GL_SYNC_META);
440 
441   gst_buffer_pool_set_config (pool, config);
442 
443   src->gl_alloc_params = gst_gl_video_allocation_params_new_wrapped_gl_handle
444       (src->context,
445       alloc_params,
446       &src->out_info, plane, valign, target, tex_format, NULL, NULL, NULL);
447 
448   if (update_pool)
449     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
450   else
451     gst_query_add_allocation_pool (query, pool, size, min, max);
452 
453   gst_object_unref (pool);
454 
455   return TRUE;
456 
457 unsupported_gl_api:
458   {
459     GstGLAPI gl_api = gst_gl_context_get_gl_api (src->context);
460     gchar *gl_api_str = gst_gl_api_to_string (gl_api);
461     gchar *supported_gl_api_str = gst_gl_api_to_string (SUPPORTED_GL_APIS);
462     GST_ELEMENT_ERROR (src, RESOURCE, BUSY,
463         ("GL API's not compatible context: %s supported: %s", gl_api_str,
464             supported_gl_api_str), (NULL));
465 
466     g_free (supported_gl_api_str);
467     g_free (gl_api_str);
468     return FALSE;
469   }
470 context_error:
471   {
472     if (error) {
473       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, ("%s", error->message),
474           (NULL));
475       g_clear_error (&error);
476     } else {
477       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
478     }
479     if (src->context)
480       gst_object_unref (src->context);
481     src->context = NULL;
482     return FALSE;
483   }
484 }
485 
486 static GstStateChangeReturn
gst_wpe_src_change_state(GstElement * element,GstStateChange transition)487 gst_wpe_src_change_state (GstElement * element, GstStateChange transition)
488 {
489   GstWpeSrc *src = GST_WPE_SRC (element);
490   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
491 
492   GST_DEBUG_OBJECT (src, "changing state: %s => %s",
493       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
494       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
495 
496   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
497   if (ret == GST_STATE_CHANGE_FAILURE)
498     return ret;
499 
500   switch (transition) {
501     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
502       if (src->display != NULL && !GST_IS_GL_DISPLAY_WAYLAND (src->display)) {
503         GST_ERROR_OBJECT (src,
504             "wpesrc currently only supports Wayland GstGLDisplays %"
505             GST_PTR_FORMAT, src->display);
506         ret = GST_STATE_CHANGE_FAILURE;
507       }
508       break;
509     case GST_STATE_CHANGE_READY_TO_NULL:
510       if (src->other_context)
511         g_clear_object (&src->other_context);
512 
513       if (src->display)
514         g_clear_object (&src->display);
515       break;
516     default:
517       break;
518   }
519 
520   return ret;
521 }
522 
523 static gboolean
gst_wpe_src_set_location(GstWpeSrc * src,const gchar * location,GError ** error)524 gst_wpe_src_set_location (GstWpeSrc * src, const gchar * location,
525     GError ** error)
526 {
527   g_free (src->location);
528   src->location = g_strdup (location);
529   if (src->view)
530     src->view->loadUri (src->location);
531 
532   return TRUE;
533 }
534 
535 static void
gst_wpe_src_set_draw_background(GstWpeSrc * src,gboolean draw_background)536 gst_wpe_src_set_draw_background (GstWpeSrc * src, gboolean draw_background)
537 {
538   if (src->view)
539     src->view->setDrawBackground (draw_background);
540   src->draw_background = draw_background;
541 }
542 
543 static void
gst_wpe_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)544 gst_wpe_src_set_property (GObject * object, guint prop_id, const GValue * value,
545     GParamSpec * pspec)
546 {
547   GstWpeSrc *src = GST_WPE_SRC (object);
548 
549   switch (prop_id) {
550     case PROP_LOCATION:
551     {
552       const gchar *location;
553 
554       location = g_value_get_string (value);
555       if (location == NULL) {
556         GST_WARNING_OBJECT (src, "location property cannot be NULL");
557         return;
558       }
559 
560       if (!gst_wpe_src_set_location (src, location, NULL)) {
561         GST_WARNING_OBJECT (src, "badly formatted location");
562         return;
563       }
564       break;
565     }
566     case PROP_DRAW_BACKGROUND:
567       gst_wpe_src_set_draw_background (src, g_value_get_boolean (value));
568       break;
569     default:
570       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571       break;
572   }
573 }
574 
575 static void
gst_wpe_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)576 gst_wpe_src_get_property (GObject * object, guint prop_id, GValue * value,
577     GParamSpec * pspec)
578 {
579   GstWpeSrc *src = GST_WPE_SRC (object);
580 
581   switch (prop_id) {
582     case PROP_LOCATION:
583       g_value_set_string (value, src->location);
584       break;
585     case PROP_DRAW_BACKGROUND:
586       g_value_set_boolean (value, src->draw_background);
587       break;
588     default:
589       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
590       break;
591   }
592 }
593 
594 static gboolean
gst_wpe_src_event(GstPad * pad,GstObject * parent,GstEvent * event)595 gst_wpe_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
596 {
597   gboolean ret = FALSE;
598   GstWpeSrc *src = GST_WPE_SRC (parent);
599 
600   if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION) {
601     const gchar *key;
602     gint button;
603     gdouble x, y;
604 
605     GST_DEBUG_OBJECT (src, "Processing event %" GST_PTR_FORMAT, event);
606     switch (gst_navigation_event_get_type (event)) {
607       case GST_NAVIGATION_EVENT_KEY_PRESS:
608       case GST_NAVIGATION_EVENT_KEY_RELEASE:
609         if (gst_navigation_event_parse_key_event (event, &key)) {
610           /* FIXME: This is wrong... The GstNavigation API should pass
611              hardware-level informations, not high-level keysym strings */
612           uint32_t keysym =
613               (uint32_t) xkb_keysym_from_name (key, XKB_KEYSYM_NO_FLAGS);
614           struct wpe_input_keyboard_event wpe_event;
615           wpe_event.key_code = keysym;
616           wpe_event.pressed =
617               gst_navigation_event_get_type (event) ==
618               GST_NAVIGATION_EVENT_KEY_PRESS;
619           wpe_view_backend_dispatch_keyboard_event (src->view->backend (),
620               &wpe_event);
621           ret = TRUE;
622         }
623         break;
624       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
625       case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:
626         if (gst_navigation_event_parse_mouse_button_event (event, &button, &x,
627                 &y)) {
628           struct wpe_input_pointer_event wpe_event;
629           wpe_event.type = wpe_input_pointer_event_type_button;
630           wpe_event.x = (int) x;
631           wpe_event.y = (int) y;
632           if (button == 1) {
633             wpe_event.modifiers = wpe_input_pointer_modifier_button1;
634           } else if (button == 2) {
635             wpe_event.modifiers = wpe_input_pointer_modifier_button2;
636           } else if (button == 3) {
637             wpe_event.modifiers = wpe_input_pointer_modifier_button3;
638           } else if (button == 4) {
639             wpe_event.modifiers = wpe_input_pointer_modifier_button4;
640           } else if (button == 5) {
641             wpe_event.modifiers = wpe_input_pointer_modifier_button5;
642           }
643           wpe_event.button = button;
644           wpe_event.state = 1;
645           wpe_event.state =
646               gst_navigation_event_get_type (event) ==
647               GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS;
648           wpe_view_backend_dispatch_pointer_event (src->view->backend (),
649               &wpe_event);
650           ret = TRUE;
651         }
652         break;
653       case GST_NAVIGATION_EVENT_MOUSE_MOVE:
654         if (gst_navigation_event_parse_mouse_move_event (event, &x, &y)) {
655           struct wpe_input_pointer_event wpe_event;
656           wpe_event.type = wpe_input_pointer_event_type_motion;
657           wpe_event.x = (int) x;
658           wpe_event.y = (int) y;
659           wpe_view_backend_dispatch_pointer_event (src->view->backend (),
660               &wpe_event);
661           ret = TRUE;
662         }
663         break;
664       default:
665         break;
666     }
667     /* FIXME: No touch events handling support in GstNavigation */
668   }
669 
670   if (!ret) {
671     ret = gst_pad_event_default (pad, parent, event);
672   }
673   return ret;
674 }
675 
676 static void
gst_wpe_src_init(GstWpeSrc * src)677 gst_wpe_src_init (GstWpeSrc * src)
678 {
679   GstBaseSrc *base_src = GST_BASE_SRC (src);
680   GstPad *pad = gst_element_get_static_pad (GST_ELEMENT_CAST (src), "src");
681 
682   gst_pad_set_event_function (pad, gst_wpe_src_event);
683   gst_object_unref (pad);
684 
685   src->n_frames = 0;
686   src->draw_background = TRUE;
687 
688   gst_base_src_set_format (base_src, GST_FORMAT_TIME);
689   gst_base_src_set_live (base_src, TRUE);
690   gst_base_src_set_do_timestamp (base_src, TRUE);
691 }
692 
693 static GstURIType
gst_wpe_src_uri_get_type(GType)694 gst_wpe_src_uri_get_type (GType)
695 {
696   return GST_URI_SRC;
697 }
698 
699 static const gchar *const *
gst_wpe_src_get_protocols(GType)700 gst_wpe_src_get_protocols (GType)
701 {
702   static const char *protocols[] = { "wpe", NULL };
703   return protocols;
704 }
705 
706 static gchar *
gst_wpe_src_get_uri(GstURIHandler * handler)707 gst_wpe_src_get_uri (GstURIHandler * handler)
708 {
709   GstWpeSrc *src = GST_WPE_SRC (handler);
710   return g_strdup_printf ("wpe://%s", src->location);
711 }
712 
713 static gboolean
gst_wpe_src_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)714 gst_wpe_src_set_uri (GstURIHandler * handler, const gchar * uri,
715     GError ** error)
716 {
717   GstWpeSrc *src = GST_WPE_SRC (handler);
718 
719   return gst_wpe_src_set_location (src, uri + 6, error);
720 }
721 
722 static void
gst_wpe_src_uri_handler_init(gpointer iface_ptr,gpointer data)723 gst_wpe_src_uri_handler_init (gpointer iface_ptr, gpointer data)
724 {
725   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) iface_ptr;
726 
727   iface->get_type = gst_wpe_src_uri_get_type;
728   iface->get_protocols = gst_wpe_src_get_protocols;
729   iface->get_uri = gst_wpe_src_get_uri;
730   iface->set_uri = gst_wpe_src_set_uri;
731 }
732 
733 static void
gst_wpe_src_class_init(GstWpeSrcClass * klass)734 gst_wpe_src_class_init (GstWpeSrcClass * klass)
735 {
736   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
737   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
738   GstPushSrcClass *push_src_class = GST_PUSH_SRC_CLASS (klass);
739   GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
740 
741   gobject_class->set_property = gst_wpe_src_set_property;
742   gobject_class->get_property = gst_wpe_src_get_property;
743 
744   g_object_class_install_property (gobject_class, PROP_LOCATION,
745       g_param_spec_string ("location", "location",
746           "The URL to display",
747           "", (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
748   g_object_class_install_property (gobject_class, PROP_DRAW_BACKGROUND,
749       g_param_spec_boolean ("draw-background", "Draws the background",
750           "Whether to draw the WebView background", TRUE,
751           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
752 
753   gst_element_class_set_static_metadata (gstelement_class,
754       "WPE source", "Source/Video",
755       "Creates a video stream from a WPE browser",
756       "Philippe Normand <philn@igalia.com>, Žan Doberšek <zdobersek@igalia.com>");
757 
758   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
759 
760   gstelement_class->set_context = GST_DEBUG_FUNCPTR (gst_wpe_src_set_context);
761   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_wpe_src_change_state);
762 
763   base_src_class->fixate = GST_DEBUG_FUNCPTR (gst_wpe_src_fixate);
764   base_src_class->set_caps = GST_DEBUG_FUNCPTR (gst_wpe_src_set_caps);
765   base_src_class->start = GST_DEBUG_FUNCPTR (gst_wpe_src_start);
766   base_src_class->stop = GST_DEBUG_FUNCPTR (gst_wpe_src_stop);
767   base_src_class->get_times = GST_DEBUG_FUNCPTR (gst_wpe_src_get_times);
768   base_src_class->query = GST_DEBUG_FUNCPTR (gst_wpe_src_query);
769   base_src_class->decide_allocation =
770       GST_DEBUG_FUNCPTR (gst_wpe_src_decide_allocation);
771 
772   push_src_class->create = GST_DEBUG_FUNCPTR (gst_wpe_src_create);
773 
774   /**
775    * GstWpeSrc::configure-web-view:
776    * @src: the object which received the signal
777    * @webview: the webView
778    *
779    * Allow application to configure the webView settings.
780    */
781   gst_wpe_src_signals[SIGNAL_CONFIGURE_WEB_VIEW] =
782       g_signal_new ("configure-web-view", G_TYPE_FROM_CLASS (klass),
783       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
784       G_TYPE_NONE, 1, G_TYPE_OBJECT);
785 }
786 
787 static gboolean
plugin_init(GstPlugin * plugin)788 plugin_init (GstPlugin * plugin)
789 {
790   GST_DEBUG_CATEGORY_INIT (wpe_src_debug, "wpesrc", 0, "WPE Source");
791 
792   return gst_element_register (plugin, "wpesrc", GST_RANK_NONE,
793       GST_TYPE_WPE_SRC);
794 }
795 
796 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR,
797     wpe, "WPE src plugin", plugin_init, VERSION, GST_LICENSE, PACKAGE,
798     GST_PACKAGE_ORIGIN)
799