• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) <2018, 2019, 2020> 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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include "WPEThreadedView.h"
25 #include "gstwpe.h"
26 #include "gstwpesrcbin.h"
27 
28 #include <gst/gl/gl.h>
29 #include <gst/gl/egl/gsteglimage.h>
30 #include <gst/gl/egl/gstgldisplay_egl.h>
31 #include <wayland-server.h>
32 
33 #include <cstdio>
34 #include <mutex>
35 
36 #include <wpe/unstable/fdo-shm.h>
37 
38 GST_DEBUG_CATEGORY_EXTERN (wpe_view_debug);
39 #define GST_CAT_DEFAULT wpe_view_debug
40 
41 class GMutexHolder {
42 public:
GMutexHolder(GMutex & mutex)43     GMutexHolder(GMutex& mutex)
44         : m(mutex)
45     {
46         g_mutex_lock(&m);
47     }
~GMutexHolder()48     ~GMutexHolder()
49     {
50         g_mutex_unlock(&m);
51     }
52 
53 private:
54     GMutex& m;
55 };
56 
57 static WPEContextThread *s_view = NULL;
58 
singleton()59 WPEContextThread& WPEContextThread::singleton()
60 {
61     static gsize initialized = 0;
62 
63     if (g_once_init_enter (&initialized)) {
64         s_view = new WPEContextThread;
65 
66         g_once_init_leave (&initialized, 1);
67     }
68 
69     return *s_view;
70 }
71 
WPEContextThread()72 WPEContextThread::WPEContextThread()
73 {
74     g_mutex_init(&threading.mutex);
75     g_cond_init(&threading.cond);
76 
77     {
78         GMutexHolder lock(threading.mutex);
79         threading.thread = g_thread_new("WPEContextThread", s_viewThread, this);
80         while (!threading.ready) {
81             g_cond_wait(&threading.cond, &threading.mutex);
82         }
83         GST_DEBUG("thread spawned");
84     }
85 }
86 
~WPEContextThread()87 WPEContextThread::~WPEContextThread()
88 {
89     if (threading.thread) {
90         g_thread_unref(threading.thread);
91         threading.thread = nullptr;
92     }
93 
94     g_mutex_clear(&threading.mutex);
95     g_cond_clear(&threading.cond);
96 }
97 
98 template<typename Function>
dispatch(Function func)99 void WPEContextThread::dispatch(Function func)
100 {
101     struct Job {
102         Job(Function& f)
103             : func(f)
104         {
105             g_mutex_init(&mutex);
106             g_cond_init(&cond);
107             dispatched = FALSE;
108         }
109         ~Job() {
110             g_mutex_clear(&mutex);
111             g_cond_clear(&cond);
112         }
113 
114         void dispatch() {
115             GMutexHolder lock(mutex);
116             func();
117             dispatched = TRUE;
118             g_cond_signal(&cond);
119         }
120 
121         void waitCompletion() {
122             GMutexHolder lock(mutex);
123             while(!dispatched) {
124                 g_cond_wait(&cond, &mutex);
125             }
126         }
127 
128         Function& func;
129         GMutex mutex;
130         GCond cond;
131         gboolean dispatched;
132     };
133 
134     struct Job job(func);
135     GSource* source = g_idle_source_new();
136     g_source_set_callback(source, [](gpointer data) -> gboolean {
137         auto* job = static_cast<struct Job*>(data);
138         job->dispatch();
139         return G_SOURCE_REMOVE;
140     }, &job, nullptr);
141     g_source_set_priority(source, G_PRIORITY_DEFAULT);
142     g_source_attach(source, glib.context);
143     job.waitCompletion();
144     g_source_unref(source);
145 }
146 
s_viewThread(gpointer data)147 gpointer WPEContextThread::s_viewThread(gpointer data)
148 {
149     auto& view = *static_cast<WPEContextThread*>(data);
150 
151     view.glib.context = g_main_context_new();
152     view.glib.loop = g_main_loop_new(view.glib.context, FALSE);
153 
154     g_main_context_push_thread_default(view.glib.context);
155 
156     {
157         GSource* source = g_idle_source_new();
158         g_source_set_callback(source,
159             [](gpointer data) -> gboolean {
160                 auto& view = *static_cast<WPEContextThread*>(data);
161                 GMutexHolder lock(view.threading.mutex);
162                 view.threading.ready = TRUE;
163                 g_cond_signal(&view.threading.cond);
164                 return G_SOURCE_REMOVE;
165             },
166             &view, nullptr);
167         g_source_attach(source, view.glib.context);
168         g_source_unref(source);
169     }
170 
171     g_main_loop_run(view.glib.loop);
172 
173     g_main_loop_unref(view.glib.loop);
174     view.glib.loop = nullptr;
175 
176     g_main_context_pop_thread_default(view.glib.context);
177     g_main_context_unref(view.glib.context);
178     view.glib.context = nullptr;
179     return nullptr;
180 }
181 
182 #ifdef G_OS_UNIX
183 static void
initialize_web_extensions(WebKitWebContext * context)184 initialize_web_extensions (WebKitWebContext *context)
185 {
186     const gchar *local_path = gst_wpe_get_devenv_extension_path ();
187     const gchar *path = g_file_test (local_path, G_FILE_TEST_IS_DIR) ? local_path : G_STRINGIFY (WPE_EXTENSION_INSTALL_DIR);
188     GST_INFO ("Loading WebExtension from %s", path);
189     webkit_web_context_set_web_extensions_directory (context, path);
190 }
191 
192 static void
webkit_extension_gerror_msg_received(GstWpeSrc * src,GVariant * params)193 webkit_extension_gerror_msg_received (GstWpeSrc *src, GVariant *params)
194 {
195     GstStructure *structure;
196     GstMessage *forwarded;
197     const gchar *src_path, *src_type, *src_name, *error_domain, *msg, *debug_str, *details_str;
198     gint message_type;
199     guint32 error_code;
200 
201     g_variant_get (params, "(issssusss)",
202        &message_type,
203        &src_type,
204        &src_name,
205        &src_path,
206        &error_domain,
207        &error_code,
208        &msg,
209        &debug_str,
210        &details_str
211     );
212 
213     GError *error = g_error_new(g_quark_from_string(error_domain), error_code, "%s", msg);
214     GstStructure *details = (details_str[0] != '\0') ? gst_structure_new_from_string(details_str) : NULL;
215     gchar * our_message = g_strdup_printf(
216         "`%s` posted from %s running inside the web page",
217         debug_str, src_path
218     );
219 
220 
221     if (message_type == GST_MESSAGE_ERROR) {
222         forwarded =
223             gst_message_new_error_with_details(GST_OBJECT(src), error,
224                                                our_message, details);
225     } else if (message_type == GST_MESSAGE_WARNING) {
226         forwarded =
227             gst_message_new_warning_with_details(GST_OBJECT(src), error,
228                                                  our_message, details);
229     } else {
230         forwarded =
231             gst_message_new_info_with_details(GST_OBJECT(src), error, our_message, details);
232     }
233 
234     structure = gst_structure_new ("WpeForwarded",
235         "message", GST_TYPE_MESSAGE, forwarded,
236         "wpe-original-src-name", G_TYPE_STRING, src_name,
237         "wpe-original-src-type", G_TYPE_STRING, src_type,
238         "wpe-original-src-path", G_TYPE_STRING, src_path,
239         NULL
240     );
241 
242     g_free (our_message);
243     gst_element_post_message(GST_ELEMENT(src), gst_message_new_custom(GST_MESSAGE_ELEMENT,
244                                                                       GST_OBJECT(src), structure));
245     g_error_free(error);
246     gst_message_unref (forwarded);
247 }
248 
249 static void
webkit_extension_bus_message_received(GstWpeSrc * src,GVariant * params)250 webkit_extension_bus_message_received (GstWpeSrc *src, GVariant *params)
251 {
252     GstStructure *original_structure, *structure;
253     const gchar *src_name, *src_type, *src_path, *struct_str;
254     GstMessageType message_type;
255     GstMessage *forwarded;
256 
257     g_variant_get (params, "(issss)",
258        &message_type,
259        &src_name,
260        &src_type,
261        &src_path,
262        &struct_str
263     );
264 
265     original_structure = (struct_str[0] != '\0') ? gst_structure_new_from_string(struct_str) : NULL;
266     if (!original_structure)
267     {
268         if (struct_str[0] != '\0')
269             GST_ERROR_OBJECT(src, "Could not deserialize: %s", struct_str);
270         original_structure = gst_structure_new_empty("wpesrc");
271 
272     }
273 
274     forwarded = gst_message_new_custom(message_type,
275         GST_OBJECT (src), original_structure);
276     structure = gst_structure_new ("WpeForwarded",
277         "message", GST_TYPE_MESSAGE, forwarded,
278         "wpe-original-src-name", G_TYPE_STRING, src_name,
279         "wpe-original-src-type", G_TYPE_STRING, src_type,
280         "wpe-original-src-path", G_TYPE_STRING, src_path,
281         NULL
282     );
283 
284     gst_element_post_message(GST_ELEMENT(src), gst_message_new_custom(GST_MESSAGE_ELEMENT,
285                                                                       GST_OBJECT(src), structure));
286 
287     gst_message_unref (forwarded);
288 }
289 
290 static gboolean
webkit_extension_msg_received(WebKitWebContext * context,WebKitUserMessage * message,GstWpeSrc * src)291 webkit_extension_msg_received (WebKitWebContext  *context,
292                WebKitUserMessage *message,
293                GstWpeSrc           *src)
294 {
295     const gchar *name = webkit_user_message_get_name (message);
296     GVariant *params = webkit_user_message_get_parameters (message);
297     gboolean res = TRUE;
298 
299     if (!g_strcmp0(name, "gstwpe.new_stream")) {
300         guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
301         const gchar *capsstr = g_variant_get_string (g_variant_get_child_value (params, 1), NULL);
302         GstCaps *caps = gst_caps_from_string (capsstr);
303         const gchar *stream_id = g_variant_get_string (g_variant_get_child_value (params, 2), NULL);
304         gst_wpe_src_new_audio_stream(src, id, caps, stream_id);
305         gst_caps_unref (caps);
306     } else if (!g_strcmp0(name, "gstwpe.set_shm")) {
307         auto fdlist = webkit_user_message_get_fd_list (message);
308         gint id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
309         gst_wpe_src_set_audio_shm (src, fdlist, id);
310     } else if (!g_strcmp0(name, "gstwpe.new_buffer")) {
311         guint32 id = g_variant_get_uint32 (g_variant_get_child_value (params, 0));
312         guint64 size = g_variant_get_uint64 (g_variant_get_child_value (params, 1));
313         gst_wpe_src_push_audio_buffer (src, id, size);
314 
315         webkit_user_message_send_reply(message, webkit_user_message_new ("gstwpe.buffer_processed", NULL));
316     } else if (!g_strcmp0(name, "gstwpe.pause")) {
317         guint32 id = g_variant_get_uint32 (params);
318 
319         gst_wpe_src_pause_audio_stream (src, id);
320     } else if (!g_strcmp0(name, "gstwpe.stop")) {
321         guint32 id = g_variant_get_uint32 (params);
322 
323         gst_wpe_src_stop_audio_stream (src, id);
324     } else if (!g_strcmp0(name, "gstwpe.bus_gerror_message")) {
325         webkit_extension_gerror_msg_received (src, params);
326     } else if (!g_strcmp0(name, "gstwpe.bus_message")) {
327         webkit_extension_bus_message_received (src, params);
328     } else {
329         res = FALSE;
330         g_error("Unknown event: %s", name);
331     }
332 
333     return res;
334 }
335 #endif
336 
createWPEView(GstWpeVideoSrc * src,GstGLContext * context,GstGLDisplay * display,int width,int height)337 WPEView* WPEContextThread::createWPEView(GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
338 {
339     GST_DEBUG("context %p display %p, size (%d,%d)", context, display, width, height);
340 
341     static std::once_flag s_loaderFlag;
342     std::call_once(s_loaderFlag,
343         [] {
344             wpe_loader_init("libWPEBackend-fdo-1.0.so");
345         });
346 
347     WPEView* view = nullptr;
348     dispatch([&]() mutable {
349         if (!glib.web_context) {
350             auto *manager = webkit_website_data_manager_new_ephemeral();
351             glib.web_context =
352                 webkit_web_context_new_with_website_data_manager(manager);
353             g_object_unref(manager);
354         }
355         view = new WPEView(glib.web_context, src, context, display, width, height);
356     });
357 
358     if (view && view->hasUri()) {
359         GST_DEBUG("waiting load to finish");
360         view->waitLoadCompletion();
361         GST_DEBUG("done");
362     }
363 
364     return view;
365 }
366 
s_loadFailed(WebKitWebView *,WebKitLoadEvent,gchar * failing_uri,GError * error,gpointer data)367 static gboolean s_loadFailed(WebKitWebView*, WebKitLoadEvent, gchar* failing_uri, GError* error, gpointer data)
368 {
369     GstWpeVideoSrc* src = GST_WPE_VIDEO_SRC(data);
370 
371     if (g_error_matches(error, WEBKIT_NETWORK_ERROR, WEBKIT_NETWORK_ERROR_CANCELLED)) {
372         GST_INFO_OBJECT (src, "Loading cancelled.");
373 
374         return FALSE;
375     }
376 
377     GST_ELEMENT_ERROR (GST_ELEMENT_CAST(src), RESOURCE, FAILED, (NULL), ("Failed to load %s (%s)", failing_uri, error->message));
378     return FALSE;
379 }
380 
s_loadFailedWithTLSErrors(WebKitWebView *,gchar * failing_uri,GTlsCertificate *,GTlsCertificateFlags,gpointer data)381 static gboolean s_loadFailedWithTLSErrors(WebKitWebView*,  gchar* failing_uri, GTlsCertificate*, GTlsCertificateFlags, gpointer data)
382 {
383     // Defer to load-failed.
384     return FALSE;
385 }
386 
s_loadProgressChaned(GObject * object,GParamSpec *,gpointer data)387 static void s_loadProgressChaned(GObject* object, GParamSpec*, gpointer data)
388 {
389     GstElement* src = GST_ELEMENT_CAST (data);
390     // The src element is locked already so we can't call
391     // gst_element_post_message(). Instead retrieve the bus manually and use it
392     // directly.
393     GstBus* bus = GST_ELEMENT_BUS (src);
394     double estimatedProgress;
395     g_object_get(object, "estimated-load-progress", &estimatedProgress, nullptr);
396     gst_object_ref (bus);
397     gst_bus_post (bus, gst_message_new_element(GST_OBJECT_CAST(src), gst_structure_new("wpe-stats", "estimated-load-progress", G_TYPE_DOUBLE, estimatedProgress * 100, nullptr)));
398     gst_object_unref (bus);
399 }
400 
WPEView(WebKitWebContext * web_context,GstWpeVideoSrc * src,GstGLContext * context,GstGLDisplay * display,int width,int height)401 WPEView::WPEView(WebKitWebContext* web_context, GstWpeVideoSrc* src, GstGLContext* context, GstGLDisplay* display, int width, int height)
402 {
403 #ifdef G_OS_UNIX
404 {
405         GstObject *parent = gst_object_get_parent (GST_OBJECT (src));
406 
407         if (parent && GST_IS_WPE_SRC (parent)) {
408             audio.init_ext_sigid = g_signal_connect (web_context,
409                               "initialize-web-extensions",
410                               G_CALLBACK (initialize_web_extensions),
411                               NULL);
412             audio.extension_msg_sigid = g_signal_connect (web_context,
413                                 "user-message-received",
414                                 G_CALLBACK (webkit_extension_msg_received),
415                                 parent);
416             GST_INFO_OBJECT (parent, "Enabled audio");
417         }
418 
419         gst_clear_object (&parent);
420 }
421 #endif // G_OS_UNIX
422 
423     g_mutex_init(&threading.ready_mutex);
424     g_cond_init(&threading.ready_cond);
425     threading.ready = FALSE;
426 
427     g_mutex_init(&images_mutex);
428     if (context)
429         gst.context = GST_GL_CONTEXT(gst_object_ref(context));
430     if (display) {
431         gst.display = GST_GL_DISPLAY(gst_object_ref(display));
432     }
433 
434     wpe.width = width;
435     wpe.height = height;
436 
437     if (context && display) {
438       if (gst_gl_context_get_gl_platform(context) == GST_GL_PLATFORM_EGL) {
439         gst.display_egl = gst_gl_display_egl_from_gl_display (gst.display);
440       } else {
441         GST_DEBUG ("Available GStreamer GL Context is not EGL - not creating an EGL display from it");
442       }
443     }
444 
445     if (gst.display_egl) {
446         EGLDisplay eglDisplay = (EGLDisplay)gst_gl_display_get_handle (GST_GL_DISPLAY(gst.display_egl));
447         GST_DEBUG("eglDisplay %p", eglDisplay);
448 
449         m_isValid = wpe_fdo_initialize_for_egl_display(eglDisplay);
450         GST_DEBUG("FDO EGL display initialisation result: %d", m_isValid);
451     } else {
452         m_isValid = wpe_fdo_initialize_shm();
453         GST_DEBUG("FDO SHM initialisation result: %d", m_isValid);
454     }
455     if (!m_isValid)
456         return;
457 
458     if (gst.display_egl) {
459         wpe.exportable = wpe_view_backend_exportable_fdo_egl_create(&s_exportableEGLClient, this, wpe.width, wpe.height);
460     } else {
461         wpe.exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, wpe.width, wpe.height);
462     }
463 
464     auto* wpeViewBackend = wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable);
465     auto* viewBackend = webkit_web_view_backend_new(wpeViewBackend, (GDestroyNotify) wpe_view_backend_exportable_fdo_destroy, wpe.exportable);
466     wpe_view_backend_add_activity_state(wpeViewBackend, wpe_view_activity_state_visible | wpe_view_activity_state_focused | wpe_view_activity_state_in_window);
467 
468     webkit.view = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
469         "web-context", web_context,
470         "backend", viewBackend,
471         nullptr));
472 
473     g_signal_connect(webkit.view, "load-failed", G_CALLBACK(s_loadFailed), src);
474     g_signal_connect(webkit.view, "load-failed-with-tls-errors", G_CALLBACK(s_loadFailedWithTLSErrors), src);
475     g_signal_connect(webkit.view, "notify::estimated-load-progress", G_CALLBACK(s_loadProgressChaned), src);
476 
477     auto* settings = webkit_web_view_get_settings(webkit.view);
478     webkit_settings_set_enable_webaudio(settings, TRUE);
479 
480     gst_wpe_video_src_configure_web_view(src, webkit.view);
481 
482     gchar* location;
483     gboolean drawBackground = TRUE;
484     g_object_get(src, "location", &location, "draw-background", &drawBackground, nullptr);
485     setDrawBackground(drawBackground);
486     if (location) {
487         loadUriUnlocked(location);
488         g_free(location);
489     }
490 }
491 
~WPEView()492 WPEView::~WPEView()
493 {
494     GstEGLImage *egl_pending = NULL;
495     GstEGLImage *egl_committed = NULL;
496     GstBuffer *shm_pending = NULL;
497     GstBuffer *shm_committed = NULL;
498     GST_TRACE ("%p destroying", this);
499 
500     g_mutex_clear(&threading.ready_mutex);
501     g_cond_clear(&threading.ready_cond);
502 
503     {
504         GMutexHolder lock(images_mutex);
505 
506         if (egl.pending) {
507             egl_pending = egl.pending;
508             egl.pending = nullptr;
509         }
510         if (egl.committed) {
511             egl_committed = egl.committed;
512             egl.committed = nullptr;
513         }
514         if (shm.pending) {
515             GST_TRACE ("%p freeing shm pending %" GST_PTR_FORMAT, this, shm.pending);
516             shm_pending = shm.pending;
517             shm.pending = nullptr;
518         }
519         if (shm.committed) {
520             GST_TRACE ("%p freeing shm commited %" GST_PTR_FORMAT, this, shm.committed);
521             shm_committed = shm.committed;
522             shm.committed = nullptr;
523         }
524     }
525 
526     if (egl_pending)
527         gst_egl_image_unref (egl_pending);
528     if (egl_committed)
529         gst_egl_image_unref (egl_committed);
530     if (shm_pending)
531         gst_buffer_unref (shm_pending);
532     if (shm_committed)
533         gst_buffer_unref (shm_committed);
534 
535     if (audio.init_ext_sigid) {
536         WebKitWebContext* web_context = webkit_web_view_get_context (webkit.view);
537 
538         g_signal_handler_disconnect(web_context, audio.init_ext_sigid);
539         g_signal_handler_disconnect(web_context, audio.extension_msg_sigid);
540         audio.init_ext_sigid = 0;
541         audio.extension_msg_sigid = 0;
542     }
543 
544     WPEContextThread::singleton().dispatch([&]() {
545         if (webkit.view) {
546             g_object_unref(webkit.view);
547             webkit.view = nullptr;
548         }
549     });
550 
551     if (gst.display_egl) {
552         gst_object_unref(gst.display_egl);
553         gst.display_egl = nullptr;
554     }
555 
556     if (gst.display) {
557         gst_object_unref(gst.display);
558         gst.display = nullptr;
559     }
560 
561     if (gst.context) {
562         gst_object_unref(gst.context);
563         gst.context = nullptr;
564     }
565     if (webkit.uri) {
566         g_free(webkit.uri);
567         webkit.uri = nullptr;
568     }
569 
570     g_mutex_clear(&images_mutex);
571     GST_TRACE ("%p destroyed", this);
572 }
573 
notifyLoadFinished()574 void WPEView::notifyLoadFinished()
575 {
576     GMutexHolder lock(threading.ready_mutex);
577     if (!threading.ready) {
578         threading.ready = TRUE;
579         g_cond_signal(&threading.ready_cond);
580     }
581 }
582 
waitLoadCompletion()583 void WPEView::waitLoadCompletion()
584 {
585     GMutexHolder lock(threading.ready_mutex);
586     while (!threading.ready)
587         g_cond_wait(&threading.ready_cond, &threading.ready_mutex);
588 }
589 
image()590 GstEGLImage* WPEView::image()
591 {
592     GstEGLImage* ret = nullptr;
593     bool dispatchFrameComplete = false;
594     GstEGLImage *prev_image = NULL;
595 
596     {
597         GMutexHolder lock(images_mutex);
598 
599         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", egl.pending,
600                   GST_IS_EGL_IMAGE(egl.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.pending)) : 0,
601                   egl.committed,
602                   GST_IS_EGL_IMAGE(egl.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(egl.committed)) : 0);
603 
604         if (egl.pending) {
605             prev_image = egl.committed;
606             egl.committed = egl.pending;
607             egl.pending = nullptr;
608 
609             dispatchFrameComplete = true;
610         }
611 
612         if (egl.committed)
613             ret = egl.committed;
614     }
615 
616     if (prev_image)
617         gst_egl_image_unref(prev_image);
618 
619     if (dispatchFrameComplete)
620         frameComplete();
621 
622     return ret;
623 }
624 
buffer()625 GstBuffer* WPEView::buffer()
626 {
627     GstBuffer* ret = nullptr;
628     bool dispatchFrameComplete = false;
629     GstBuffer *prev_image = NULL;
630 
631     {
632         GMutexHolder lock(images_mutex);
633 
634         GST_TRACE("pending %" GST_PTR_FORMAT " (%d) committed %" GST_PTR_FORMAT " (%d)", shm.pending,
635                   GST_IS_BUFFER(shm.pending) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.pending)) : 0,
636                   shm.committed,
637                   GST_IS_BUFFER(shm.committed) ? GST_MINI_OBJECT_REFCOUNT_VALUE(GST_MINI_OBJECT_CAST(shm.committed)) : 0);
638 
639         if (shm.pending) {
640             prev_image = shm.committed;
641             shm.committed = shm.pending;
642             shm.pending = nullptr;
643 
644             dispatchFrameComplete = true;
645         }
646 
647         if (shm.committed)
648             ret = shm.committed;
649     }
650 
651     if (prev_image)
652         gst_buffer_unref(prev_image);
653 
654     if (dispatchFrameComplete)
655         frameComplete();
656 
657     return ret;
658 }
659 
resize(int width,int height)660 void WPEView::resize(int width, int height)
661 {
662     GST_DEBUG("resize to %dx%d", width, height);
663     wpe.width = width;
664     wpe.height = height;
665 
666     s_view->dispatch([&]() {
667         if (wpe.exportable && wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable))
668           wpe_view_backend_dispatch_set_size(wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable), wpe.width, wpe.height);
669     });
670 }
671 
frameComplete()672 void WPEView::frameComplete()
673 {
674     GST_TRACE("frame complete");
675     s_view->dispatch([&]() {
676         GST_TRACE("dispatching");
677         wpe_view_backend_exportable_fdo_dispatch_frame_complete(wpe.exportable);
678     });
679 }
680 
loadUriUnlocked(const gchar * uri)681 void WPEView::loadUriUnlocked(const gchar* uri)
682 {
683     if (webkit.uri)
684         g_free(webkit.uri);
685 
686     GST_DEBUG("loading %s", uri);
687     webkit.uri = g_strdup(uri);
688     webkit_web_view_load_uri(webkit.view, webkit.uri);
689 }
690 
loadUri(const gchar * uri)691 void WPEView::loadUri(const gchar* uri)
692 {
693     s_view->dispatch([&]() {
694         loadUriUnlocked(uri);
695     });
696 }
697 
loadData(GBytes * bytes)698 void WPEView::loadData(GBytes* bytes)
699 {
700     s_view->dispatch([this, bytes = g_bytes_ref(bytes)]() {
701         webkit_web_view_load_bytes(webkit.view, bytes, nullptr, nullptr, nullptr);
702         g_bytes_unref(bytes);
703     });
704 }
705 
setDrawBackground(gboolean drawsBackground)706 void WPEView::setDrawBackground(gboolean drawsBackground)
707 {
708     GST_DEBUG("%s background rendering", drawsBackground ? "Enabling" : "Disabling");
709     WebKitColor color;
710     webkit_color_parse(&color, drawsBackground ? "white" : "transparent");
711     webkit_web_view_set_background_color(webkit.view, &color);
712 }
713 
releaseImage(gpointer imagePointer)714 void WPEView::releaseImage(gpointer imagePointer)
715 {
716     s_view->dispatch([&]() {
717         GST_TRACE("Dispatch release exported image %p", imagePointer);
718         wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(wpe.exportable,
719                                                                             static_cast<struct wpe_fdo_egl_exported_image*>(imagePointer));
720     });
721 }
722 
723 struct ImageContext {
724     WPEView* view;
725     gpointer image;
726 };
727 
handleExportedImage(gpointer image)728 void WPEView::handleExportedImage(gpointer image)
729 {
730     ImageContext* imageContext = g_slice_new(ImageContext);
731     imageContext->view = this;
732     imageContext->image = static_cast<gpointer>(image);
733     EGLImageKHR eglImage = wpe_fdo_egl_exported_image_get_egl_image(static_cast<struct wpe_fdo_egl_exported_image*>(image));
734 
735     auto* gstImage = gst_egl_image_new_wrapped(gst.context, eglImage, GST_GL_RGBA, imageContext, s_releaseImage);
736     {
737       GMutexHolder lock(images_mutex);
738 
739       GST_TRACE("EGLImage %p wrapped in GstEGLImage %" GST_PTR_FORMAT, eglImage, gstImage);
740       gst_clear_mini_object ((GstMiniObject **) &egl.pending);
741       egl.pending = gstImage;
742 
743       notifyLoadFinished();
744     }
745 }
746 
747 struct SHMBufferContext {
748     WPEView* view;
749     struct wpe_fdo_shm_exported_buffer* buffer;
750 };
751 
releaseSHMBuffer(gpointer data)752 void WPEView::releaseSHMBuffer(gpointer data)
753 {
754     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
755     s_view->dispatch([&]() {
756         auto* buffer = static_cast<struct wpe_fdo_shm_exported_buffer*>(context->buffer);
757         GST_TRACE("Dispatch release exported buffer %p", buffer);
758         wpe_view_backend_exportable_fdo_dispatch_release_shm_exported_buffer(wpe.exportable, buffer);
759     });
760 }
761 
s_releaseSHMBuffer(gpointer data)762 void WPEView::s_releaseSHMBuffer(gpointer data)
763 {
764     SHMBufferContext* context = static_cast<SHMBufferContext*>(data);
765     context->view->releaseSHMBuffer(data);
766     g_slice_free(SHMBufferContext, context);
767 }
768 
handleExportedBuffer(struct wpe_fdo_shm_exported_buffer * buffer)769 void WPEView::handleExportedBuffer(struct wpe_fdo_shm_exported_buffer* buffer)
770 {
771     struct wl_shm_buffer* shmBuffer = wpe_fdo_shm_exported_buffer_get_shm_buffer(buffer);
772     auto format = wl_shm_buffer_get_format(shmBuffer);
773     if (format != WL_SHM_FORMAT_ARGB8888 && format != WL_SHM_FORMAT_XRGB8888) {
774         GST_ERROR("Unsupported pixel format: %d", format);
775         return;
776     }
777 
778     int32_t width = wl_shm_buffer_get_width(shmBuffer);
779     int32_t height = wl_shm_buffer_get_height(shmBuffer);
780     gint stride = wl_shm_buffer_get_stride(shmBuffer);
781     gsize size = width * height * 4;
782     auto* data = static_cast<uint8_t*>(wl_shm_buffer_get_data(shmBuffer));
783 
784     SHMBufferContext* bufferContext = g_slice_new(SHMBufferContext);
785     bufferContext->view = this;
786     bufferContext->buffer = buffer;
787 
788     auto* gstBuffer = gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_READONLY, data, size, 0, size, bufferContext, s_releaseSHMBuffer);
789     gsize offsets[1];
790     gint strides[1];
791     offsets[0] = 0;
792     strides[0] = stride;
793     gst_buffer_add_video_meta_full(gstBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_FORMAT_BGRA, width, height, 1, offsets, strides);
794 
795     {
796         GMutexHolder lock(images_mutex);
797         GST_TRACE("SHM buffer %p wrapped in buffer %" GST_PTR_FORMAT, buffer, gstBuffer);
798         gst_clear_buffer (&shm.pending);
799         shm.pending = gstBuffer;
800         notifyLoadFinished();
801     }
802 }
803 
804 struct wpe_view_backend_exportable_fdo_egl_client WPEView::s_exportableEGLClient = {
805     // export_egl_image
806     nullptr,
__anona8b0c3340c02(void* data, struct wpe_fdo_egl_exported_image* image) 807     [](void* data, struct wpe_fdo_egl_exported_image* image) {
808         auto& view = *static_cast<WPEView*>(data);
809         view.handleExportedImage(static_cast<gpointer>(image));
810     },
811     nullptr,
812     // padding
813     nullptr, nullptr
814 };
815 
816 struct wpe_view_backend_exportable_fdo_client WPEView::s_exportableClient = {
817     nullptr,
818     nullptr,
819     // export_shm_buffer
__anona8b0c3340d02(void* data, struct wpe_fdo_shm_exported_buffer* buffer) 820     [](void* data, struct wpe_fdo_shm_exported_buffer* buffer) {
821         auto& view = *static_cast<WPEView*>(data);
822         view.handleExportedBuffer(buffer);
823     },
824     nullptr,
825     nullptr,
826 };
827 
s_releaseImage(GstEGLImage * image,gpointer data)828 void WPEView::s_releaseImage(GstEGLImage* image, gpointer data)
829 {
830     ImageContext* context = static_cast<ImageContext*>(data);
831     context->view->releaseImage(context->image);
832     g_slice_free(ImageContext, context);
833 }
834 
backend() const835 struct wpe_view_backend* WPEView::backend() const
836 {
837     return wpe.exportable ? wpe_view_backend_exportable_fdo_get_view_backend(wpe.exportable) : nullptr;
838 }
839 
dispatchKeyboardEvent(struct wpe_input_keyboard_event & wpe_event)840 void WPEView::dispatchKeyboardEvent(struct wpe_input_keyboard_event& wpe_event)
841 {
842     s_view->dispatch([&]() {
843         wpe_view_backend_dispatch_keyboard_event(backend(), &wpe_event);
844     });
845 }
846 
dispatchPointerEvent(struct wpe_input_pointer_event & wpe_event)847 void WPEView::dispatchPointerEvent(struct wpe_input_pointer_event& wpe_event)
848 {
849     s_view->dispatch([&]() {
850         wpe_view_backend_dispatch_pointer_event(backend(), &wpe_event);
851     });
852 }
853 
dispatchAxisEvent(struct wpe_input_axis_event & wpe_event)854 void WPEView::dispatchAxisEvent(struct wpe_input_axis_event& wpe_event)
855 {
856     s_view->dispatch([&]() {
857         wpe_view_backend_dispatch_axis_event(backend(), &wpe_event);
858     });
859 }
860