• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
3  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4  * Copyright (C) 2002,2007 David A. Schleef <ds@schleef.org>
5  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
6  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:element-gltestsrc
26  * @title: gltestsrc
27  *
28  * The gltestsrc element is used to produce test video texture.
29  * The video test produced can be controlled with the "pattern"
30  * property.
31  *
32  * ## Example launch line
33  *
34  * |[
35  * gst-launch-1.0 -v gltestsrc pattern=smpte ! glimagesink
36  * ]|
37  * Shows original SMPTE color bars in a window.
38  *
39  */
40 
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 #include <gst/gl/gstglfuncs.h>
46 #include <gst/gst-i18n-plugin.h>
47 
48 #include "gstglelements.h"
49 #include "gstgltestsrc.h"
50 #include "gltestsrc.h"
51 
52 GST_DEBUG_CATEGORY_STATIC (gl_test_src_debug);
53 #define GST_CAT_DEFAULT gl_test_src_debug
54 
55 enum
56 {
57   PROP_0,
58   PROP_PATTERN,
59   PROP_IS_LIVE
60       /* FILL ME */
61 };
62 
63 /* *INDENT-OFF* */
64 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
65     GST_PAD_SRC,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), "
68         "format = (string) RGBA, "
69         "width = " GST_VIDEO_SIZE_RANGE ", "
70         "height = " GST_VIDEO_SIZE_RANGE ", "
71         "framerate = " GST_VIDEO_FPS_RANGE ","
72         "texture-target = (string) 2D")
73     );
74 /* *INDENT-ON* */
75 
76 #define gst_gl_test_src_parent_class parent_class
77 G_DEFINE_TYPE (GstGLTestSrc, gst_gl_test_src, GST_TYPE_GL_BASE_SRC);
78 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (gltestsrc, "gltestsrc",
79     GST_RANK_NONE, GST_TYPE_GL_TEST_SRC, gl_element_init (plugin));
80 
81 static void gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc,
82     int pattern_type);
83 static void gst_gl_test_src_set_property (GObject * object, guint prop_id,
84     const GValue * value, GParamSpec * pspec);
85 static void gst_gl_test_src_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87 
88 static GstCaps *gst_gl_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
89 static gboolean gst_gl_test_src_is_seekable (GstBaseSrc * psrc);
90 static gboolean gst_gl_test_src_callback (gpointer stuff);
91 static gboolean gst_gl_test_src_gl_start (GstGLBaseSrc * src);
92 static void gst_gl_test_src_gl_stop (GstGLBaseSrc * src);
93 static gboolean gst_gl_test_src_fill_memory (GstGLBaseSrc * src,
94     GstGLMemory * memory);
95 
96 #define GST_TYPE_GL_TEST_SRC_PATTERN (gst_gl_test_src_pattern_get_type ())
97 static GType
gst_gl_test_src_pattern_get_type(void)98 gst_gl_test_src_pattern_get_type (void)
99 {
100   static GType gl_test_src_pattern_type = 0;
101   static const GEnumValue pattern_types[] = {
102     {GST_GL_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
103     {GST_GL_TEST_SRC_SNOW, "Random (television snow)", "snow"},
104     {GST_GL_TEST_SRC_BLACK, "100% Black", "black"},
105     {GST_GL_TEST_SRC_WHITE, "100% White", "white"},
106     {GST_GL_TEST_SRC_RED, "Red", "red"},
107     {GST_GL_TEST_SRC_GREEN, "Green", "green"},
108     {GST_GL_TEST_SRC_BLUE, "Blue", "blue"},
109     {GST_GL_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
110     {GST_GL_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
111     {GST_GL_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
112     {GST_GL_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
113     {GST_GL_TEST_SRC_CIRCULAR, "Circular", "circular"},
114     {GST_GL_TEST_SRC_BLINK, "Blink", "blink"},
115     {GST_GL_TEST_SRC_MANDELBROT, "Mandelbrot Fractal", "mandelbrot"},
116     {0, NULL, NULL}
117   };
118 
119   if (!gl_test_src_pattern_type) {
120     gl_test_src_pattern_type =
121         g_enum_register_static ("GstGLTestSrcPattern", pattern_types);
122   }
123   return gl_test_src_pattern_type;
124 }
125 
126 static void
gst_gl_test_src_class_init(GstGLTestSrcClass * klass)127 gst_gl_test_src_class_init (GstGLTestSrcClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstBaseSrcClass *gstbasesrc_class;
131   GstGLBaseSrcClass *gstglbasesrc_class;
132   GstElementClass *element_class;
133 
134   GST_DEBUG_CATEGORY_INIT (gl_test_src_debug, "gltestsrc", 0,
135       "Video Test Source");
136 
137   gobject_class = G_OBJECT_CLASS (klass);
138   gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
139   gstglbasesrc_class = GST_GL_BASE_SRC_CLASS (klass);
140   element_class = GST_ELEMENT_CLASS (klass);
141 
142   gobject_class->set_property = gst_gl_test_src_set_property;
143   gobject_class->get_property = gst_gl_test_src_get_property;
144 
145   g_object_class_install_property (gobject_class, PROP_PATTERN,
146       g_param_spec_enum ("pattern", "Pattern",
147           "Type of test pattern to generate", GST_TYPE_GL_TEST_SRC_PATTERN,
148           GST_GL_TEST_SRC_SMPTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
149   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
150       g_param_spec_boolean ("is-live", "Is Live",
151           "Whether to act as a live source", FALSE,
152           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 
154   gst_element_class_set_metadata (element_class, "Video test source",
155       "Source/Video", "Creates a test video stream",
156       "David A. Schleef <ds@schleef.org>");
157 
158   gst_element_class_add_static_pad_template (element_class, &src_factory);
159 
160   gstbasesrc_class->is_seekable = gst_gl_test_src_is_seekable;
161   gstbasesrc_class->fixate = gst_gl_test_src_fixate;
162 
163   gstglbasesrc_class->supported_gl_api =
164       GST_GL_API_OPENGL | GST_GL_API_OPENGL3 | GST_GL_API_GLES2;
165   gstglbasesrc_class->gl_start = gst_gl_test_src_gl_start;
166   gstglbasesrc_class->gl_stop = gst_gl_test_src_gl_stop;
167   gstglbasesrc_class->fill_gl_memory = gst_gl_test_src_fill_memory;
168 
169   gst_type_mark_as_plugin_api (GST_TYPE_GL_TEST_SRC_PATTERN, 0);
170 }
171 
172 static void
gst_gl_test_src_init(GstGLTestSrc * src)173 gst_gl_test_src_init (GstGLTestSrc * src)
174 {
175   gst_gl_test_src_set_pattern (src, GST_GL_TEST_SRC_SMPTE);
176 }
177 
178 static GstCaps *
gst_gl_test_src_fixate(GstBaseSrc * bsrc,GstCaps * caps)179 gst_gl_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
180 {
181   GstStructure *structure;
182 
183   GST_DEBUG ("fixate");
184 
185   caps = gst_caps_make_writable (caps);
186 
187   structure = gst_caps_get_structure (caps, 0);
188 
189   gst_structure_fixate_field_nearest_int (structure, "width", 320);
190   gst_structure_fixate_field_nearest_int (structure, "height", 240);
191   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
192 
193   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
194 
195   return caps;
196 }
197 
198 static void
gst_gl_test_src_set_pattern(GstGLTestSrc * gltestsrc,gint pattern_type)199 gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc, gint pattern_type)
200 {
201   gltestsrc->set_pattern = pattern_type;
202 }
203 
204 static void
gst_gl_test_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)205 gst_gl_test_src_set_property (GObject * object, guint prop_id,
206     const GValue * value, GParamSpec * pspec)
207 {
208   GstGLTestSrc *src = GST_GL_TEST_SRC (object);
209 
210   switch (prop_id) {
211     case PROP_PATTERN:
212       gst_gl_test_src_set_pattern (src, g_value_get_enum (value));
213       break;
214     case PROP_IS_LIVE:
215       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
216       break;
217     default:
218       break;
219   }
220 }
221 
222 static void
gst_gl_test_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)223 gst_gl_test_src_get_property (GObject * object, guint prop_id,
224     GValue * value, GParamSpec * pspec)
225 {
226   GstGLTestSrc *src = GST_GL_TEST_SRC (object);
227 
228   switch (prop_id) {
229     case PROP_PATTERN:
230       g_value_set_enum (value, src->set_pattern);
231       break;
232     case PROP_IS_LIVE:
233       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
234       break;
235     default:
236       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237       break;
238   }
239 }
240 
241 static gboolean
gst_gl_test_src_is_seekable(GstBaseSrc * psrc)242 gst_gl_test_src_is_seekable (GstBaseSrc * psrc)
243 {
244   /* we're seekable... */
245   return TRUE;
246 }
247 
248 static gboolean
gst_gl_test_src_callback(gpointer stuff)249 gst_gl_test_src_callback (gpointer stuff)
250 {
251   GstGLTestSrc *src = GST_GL_TEST_SRC (stuff);
252   GstGLBaseSrc *glbasesrc = GST_GL_BASE_SRC (src);
253   const struct SrcFuncs *funcs;
254 
255   funcs = src->src_funcs;
256 
257   if (!funcs || src->set_pattern != src->active_pattern) {
258     if (src->src_impl && funcs)
259       funcs->free (src->src_impl);
260     src->src_funcs = funcs =
261         gst_gl_test_src_get_src_funcs_for_pattern (src->set_pattern);
262     if (funcs == NULL) {
263       GST_ERROR_OBJECT (src, "Could not find an implementation of the "
264           "requested pattern");
265       return FALSE;
266     }
267     src->src_impl = funcs->new (src);
268     if (!funcs->init (src->src_impl, glbasesrc->context, &glbasesrc->out_info)) {
269       GST_ERROR_OBJECT (src, "Failed to initialize pattern");
270       return FALSE;
271     }
272     src->active_pattern = src->set_pattern;
273   }
274 
275   return funcs->fill_bound_fbo (src->src_impl);
276 }
277 
278 static gboolean
gst_gl_test_src_fill_memory(GstGLBaseSrc * src,GstGLMemory * memory)279 gst_gl_test_src_fill_memory (GstGLBaseSrc * src, GstGLMemory * memory)
280 {
281   GstGLTestSrc *test_src = GST_GL_TEST_SRC (src);
282   return gst_gl_framebuffer_draw_to_texture (test_src->fbo, memory,
283       gst_gl_test_src_callback, test_src);
284 }
285 
286 static gboolean
gst_gl_test_src_gl_start(GstGLBaseSrc * bsrc)287 gst_gl_test_src_gl_start (GstGLBaseSrc * bsrc)
288 {
289   GstGLTestSrc *src = GST_GL_TEST_SRC (bsrc);
290   src->fbo = gst_gl_framebuffer_new_with_default_depth (bsrc->context,
291       GST_VIDEO_INFO_WIDTH (&bsrc->out_info),
292       GST_VIDEO_INFO_HEIGHT (&bsrc->out_info));
293   return TRUE;
294 }
295 
296 static void
gst_gl_test_src_gl_stop(GstGLBaseSrc * bsrc)297 gst_gl_test_src_gl_stop (GstGLBaseSrc * bsrc)
298 {
299   GstGLTestSrc *src = GST_GL_TEST_SRC (bsrc);
300 
301   if (src->fbo)
302     gst_object_unref (src->fbo);
303   src->fbo = NULL;
304 
305   if (src->src_impl)
306     src->src_funcs->free (src->src_impl);
307   src->src_impl = NULL;
308   src->src_funcs = NULL;
309 }
310