wpe: Move wpesrc to wpevideosrc and add a wrapper bin `wpesrc`
[platform/upstream/gstreamer.git] / ext / wpe / gstwpesrcbin.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  * FIXME The wpesrc element is used to produce a video texture representing a
25  * web page rendered off-screen by WPE.
26  *
27  */
28
29 #include "gstwpesrcbin.h"
30 #include "gstwpevideosrc.h"
31 #include "WPEThreadedView.h"
32
33 struct _GstWpeSrc
34 {
35   GstBin parent;
36
37   GstElement *video_src;
38 };
39
40 enum
41 {
42  PROP_0,
43  PROP_LOCATION,
44  PROP_DRAW_BACKGROUND
45 };
46
47 enum
48 {
49  SIGNAL_LOAD_BYTES,
50  LAST_SIGNAL
51 };
52
53 static guint gst_wpe_video_src_signals[LAST_SIGNAL] = { 0 };
54
55 static void gst_wpe_src_uri_handler_init (gpointer iface, gpointer data);
56
57 #define gst_wpe_src_parent_class parent_class
58 G_DEFINE_TYPE_WITH_CODE (GstWpeSrc, gst_wpe_src, GST_TYPE_BIN,
59     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_wpe_src_uri_handler_init));
60
61 static GstStaticPadTemplate video_src_factory =
62 GST_STATIC_PAD_TEMPLATE ("video_src", GST_PAD_SRC, GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("video/x-raw(memory:GLMemory), "
64                      "format = (string) RGBA, "
65                      "width = " GST_VIDEO_SIZE_RANGE ", "
66                      "height = " GST_VIDEO_SIZE_RANGE ", "
67                      "framerate = " GST_VIDEO_FPS_RANGE ", "
68                      "pixel-aspect-ratio = (fraction)1/1,"
69                      "texture-target = (string)2D"
70 #if ENABLE_SHM_BUFFER_SUPPORT
71                      "; video/x-raw, "
72                      "format = (string) BGRA, "
73                      "width = " GST_VIDEO_SIZE_RANGE ", "
74                      "height = " GST_VIDEO_SIZE_RANGE ", "
75                      "framerate = " GST_VIDEO_FPS_RANGE ", "
76                      "pixel-aspect-ratio = (fraction)1/1"
77 #endif
78                      ));
79
80 static void
81 gst_wpe_src_load_bytes (GstWpeVideoSrc * src, GBytes * bytes)
82 {
83   GstWpeSrc *self = GST_WPE_SRC (src);
84
85   if (self->video_src)
86     g_signal_emit_by_name (self->video_src, "load-bytes", bytes, NULL);
87 }
88
89 static void
90 gst_wpe_src_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec)
92 {
93   GstWpeSrc *self = GST_WPE_SRC (object);
94
95   if (self->video_src)
96     g_object_get_property (G_OBJECT (self->video_src), pspec->name, value);
97 }
98
99 static void
100 gst_wpe_src_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec)
102 {
103   GstWpeSrc *self = GST_WPE_SRC (object);
104
105   if (self->video_src)
106     g_object_set_property (G_OBJECT (self->video_src), pspec->name, value);
107 }
108
109 static GstURIType
110 gst_wpe_src_uri_get_type (GType)
111 {
112   return GST_URI_SRC;
113 }
114
115 static const gchar *const *
116 gst_wpe_src_get_protocols (GType)
117 {
118   static const char *protocols[] = { "wpe", NULL };
119   return protocols;
120 }
121
122 static gchar *
123 gst_wpe_src_get_uri (GstURIHandler * handler)
124 {
125   GstWpeSrc *src = GST_WPE_SRC (handler);
126   const gchar *location;
127   g_object_get (src->video_src, "location", &location, NULL);
128   return g_strdup_printf ("wpe://%s", location);
129 }
130
131 static gboolean
132 gst_wpe_src_set_uri (GstURIHandler * handler, const gchar * uri,
133     GError ** error)
134 {
135   GstWpeSrc *src = GST_WPE_SRC (handler);
136
137   g_object_set (src->video_src, "location", uri + 6, NULL);
138   return TRUE;
139 }
140
141 static void
142 gst_wpe_src_uri_handler_init (gpointer iface_ptr, gpointer data)
143 {
144   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) iface_ptr;
145
146   iface->get_type = gst_wpe_src_uri_get_type;
147   iface->get_protocols = gst_wpe_src_get_protocols;
148   iface->get_uri = gst_wpe_src_get_uri;
149   iface->set_uri = gst_wpe_src_set_uri;
150 }
151
152 static void
153 gst_wpe_src_init (GstWpeSrc * src)
154 {
155   src->video_src = gst_element_factory_make ("wpevideosrc", NULL);
156
157   gst_bin_add (GST_BIN_CAST (src), src->video_src);
158
159   GstPad *pad =
160       gst_element_get_static_pad (GST_ELEMENT_CAST (src->video_src), "src");
161
162   GstPad *ghost_pad = gst_ghost_pad_new_from_template ("video_src", pad,
163       gst_static_pad_template_get (&video_src_factory));
164   GstProxyPad *proxy_pad =
165       gst_proxy_pad_get_internal (GST_PROXY_PAD (ghost_pad));
166   gst_pad_set_active (GST_PAD_CAST (proxy_pad), TRUE);
167   gst_object_unref (proxy_pad);
168
169   gst_element_add_pad (GST_ELEMENT_CAST (src), GST_PAD_CAST (ghost_pad));
170
171   gst_object_unref (pad);
172 }
173
174 static void
175 gst_wpe_src_class_init (GstWpeSrcClass * klass)
176 {
177   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
178   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
179
180   gobject_class->set_property = gst_wpe_src_set_property;
181   gobject_class->get_property = gst_wpe_src_get_property;
182
183   g_object_class_install_property (gobject_class, PROP_LOCATION,
184       g_param_spec_string ("location", "location", "The URL to display", "",
185           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
186   g_object_class_install_property (gobject_class, PROP_DRAW_BACKGROUND,
187       g_param_spec_boolean ("draw-background", "Draws the background",
188           "Whether to draw the WebView background", TRUE,
189           (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
190
191   gst_element_class_set_static_metadata (element_class, "WPE source",
192       "Source/Video", "Creates a video stream from a WPE browser",
193       "Philippe Normand <philn@igalia.com>, Žan Doberšek "
194       "<zdobersek@igalia.com>");
195
196   /**
197    * GstWpeSrc::load-bytes:
198    * @src: the object which received the signal
199    * @bytes: the GBytes data to load
200    *
201    * Load the specified bytes into the internal webView.
202    */
203   gst_wpe_video_src_signals[SIGNAL_LOAD_BYTES] =
204       g_signal_new_class_handler ("load-bytes", G_TYPE_FROM_CLASS (klass),
205       static_cast < GSignalFlags > (G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
206       G_CALLBACK (gst_wpe_src_load_bytes), NULL, NULL, NULL, G_TYPE_NONE, 1,
207       G_TYPE_BYTES);
208
209   gst_element_class_set_static_metadata (element_class, "WPE source",
210       "Source/Video", "Creates a video stream from a WPE browser",
211       "Philippe Normand <philn@igalia.com>, Žan Doberšek "
212       "<zdobersek@igalia.com>");
213
214   gst_element_class_add_static_pad_template (element_class, &video_src_factory);
215 }