1 /* Generic video mixer plugin
2 *
3 * GStreamer
4 * Copyright (C) 2009 Julien Isorce <julien.isorce@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <gst/video/video.h>
28
29 #include <string.h>
30
31 #include "gstglmixer.h"
32
33 #define GST_CAT_DEFAULT gst_gl_mixer_debug
34 GST_DEBUG_CATEGORY (gst_gl_mixer_debug);
35
36 static void gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
37 GValue * value, GParamSpec * pspec);
38 static void gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
39 const GValue * value, GParamSpec * pspec);
40 static gboolean gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
41 GstVideoAggregator * vagg, GstBuffer * buffer,
42 GstVideoFrame * prepared_frame);
43 static void gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
44 GstVideoAggregator * vagg, GstVideoFrame * prepared_frame);
45
46 enum
47 {
48 PROP_PAD_0
49 };
50
51 struct _GstGLMixerPrivate
52 {
53 gboolean negotiated;
54
55 gboolean gl_resource_ready;
56 GMutex gl_resource_lock;
57 GCond gl_resource_cond;
58 };
59
60 #define gst_gl_mixer_parent_class parent_class
61 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstGLMixer, gst_gl_mixer,
62 GST_TYPE_GL_BASE_MIXER);
63
64 G_DEFINE_TYPE (GstGLMixerPad, gst_gl_mixer_pad, GST_TYPE_GL_BASE_MIXER_PAD);
65
66 static void
gst_gl_mixer_pad_class_init(GstGLMixerPadClass * klass)67 gst_gl_mixer_pad_class_init (GstGLMixerPadClass * klass)
68 {
69 GObjectClass *gobject_class = (GObjectClass *) klass;
70 GstVideoAggregatorPadClass *vaggpad_class =
71 (GstVideoAggregatorPadClass *) klass;
72
73 gobject_class->set_property = gst_gl_mixer_pad_set_property;
74 gobject_class->get_property = gst_gl_mixer_pad_get_property;
75
76 vaggpad_class->prepare_frame = gst_gl_mixer_pad_prepare_frame;
77 vaggpad_class->clean_frame = gst_gl_mixer_pad_clean_frame;
78 }
79
80 static void
gst_gl_mixer_pad_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)81 gst_gl_mixer_pad_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec)
83 {
84 switch (prop_id) {
85 default:
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 break;
88 }
89 }
90
91 static void
gst_gl_mixer_pad_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)92 gst_gl_mixer_pad_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec)
94 {
95 switch (prop_id) {
96 default:
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98 break;
99 }
100 }
101
102 static gboolean
gst_gl_mixer_pad_prepare_frame(GstVideoAggregatorPad * vpad,GstVideoAggregator * vagg,GstBuffer * buffer,GstVideoFrame * prepared_frame)103 gst_gl_mixer_pad_prepare_frame (GstVideoAggregatorPad * vpad,
104 GstVideoAggregator * vagg, GstBuffer * buffer,
105 GstVideoFrame * prepared_frame)
106 {
107 GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
108 GstGLMixer *mix = GST_GL_MIXER (vagg);
109 GstVideoInfo gl_info;
110 GstGLSyncMeta *sync_meta;
111
112 pad->current_texture = 0;
113
114 gst_video_info_set_format (&gl_info,
115 GST_VIDEO_FORMAT_RGBA,
116 GST_VIDEO_INFO_WIDTH (&vpad->info), GST_VIDEO_INFO_HEIGHT (&vpad->info));
117
118 sync_meta = gst_buffer_get_gl_sync_meta (buffer);
119 if (sync_meta)
120 gst_gl_sync_meta_wait (sync_meta, GST_GL_BASE_MIXER (mix)->context);
121
122 if (!gst_video_frame_map (prepared_frame, &gl_info, buffer,
123 GST_MAP_READ | GST_MAP_GL)) {
124 GST_ERROR_OBJECT (pad, "Failed to map input frame");
125 return FALSE;
126 }
127
128 pad->current_texture = *(guint *) prepared_frame->data[0];
129
130 return TRUE;
131 }
132
133 static void
gst_gl_mixer_pad_clean_frame(GstVideoAggregatorPad * vpad,GstVideoAggregator * vagg,GstVideoFrame * prepared_frame)134 gst_gl_mixer_pad_clean_frame (GstVideoAggregatorPad * vpad,
135 GstVideoAggregator * vagg, GstVideoFrame * prepared_frame)
136 {
137 GstGLMixerPad *pad = GST_GL_MIXER_PAD (vpad);
138
139 pad->current_texture = 0;
140 if (prepared_frame->buffer) {
141 gst_video_frame_unmap (prepared_frame);
142 memset (prepared_frame, 0, sizeof (GstVideoFrame));
143 }
144 }
145
146 static gboolean
_negotiated_caps(GstAggregator * agg,GstCaps * caps)147 _negotiated_caps (GstAggregator * agg, GstCaps * caps)
148 {
149 GstGLMixer *mix = GST_GL_MIXER (agg);
150 gboolean ret;
151
152 mix->priv->negotiated = TRUE;
153
154 gst_caps_replace (&mix->out_caps, caps);
155
156 ret = GST_AGGREGATOR_CLASS (parent_class)->negotiated_src_caps (agg, caps);
157
158 return ret;
159 }
160
161 static void
_find_best_format(GstVideoAggregator * vagg,GstCaps * downstream_caps,GstVideoInfo * best_info,gboolean * at_least_one_alpha)162 _find_best_format (GstVideoAggregator * vagg, GstCaps * downstream_caps,
163 GstVideoInfo * best_info, gboolean * at_least_one_alpha)
164 {
165 GstVideoInfo tmp_info;
166
167 GST_VIDEO_AGGREGATOR_CLASS (parent_class)->find_best_format (vagg,
168 downstream_caps, best_info, at_least_one_alpha);
169
170 gst_video_info_set_format (&tmp_info, GST_VIDEO_FORMAT_RGBA,
171 best_info->width, best_info->height);
172 tmp_info.par_n = best_info->par_n;
173 tmp_info.par_d = best_info->par_d;
174 tmp_info.fps_n = best_info->fps_n;
175 tmp_info.fps_d = best_info->fps_d;
176 tmp_info.flags = best_info->flags;
177 tmp_info.interlace_mode = best_info->interlace_mode;
178 *best_info = tmp_info;
179 }
180
181 static gboolean
gst_gl_mixer_propose_allocation(GstAggregator * agg,GstAggregatorPad * agg_pad,GstQuery * decide_query,GstQuery * query)182 gst_gl_mixer_propose_allocation (GstAggregator * agg,
183 GstAggregatorPad * agg_pad, GstQuery * decide_query, GstQuery * query)
184 {
185 GstGLMixer *mix = GST_GL_MIXER (agg);
186 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
187 GstGLContext *context;
188 GstBufferPool *pool = NULL;
189 GstStructure *config;
190 GstCaps *caps;
191 GstVideoInfo info;
192 guint size = 0;
193 gboolean need_pool;
194
195 if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->propose_allocation
196 (agg, agg_pad, decide_query, query))
197 return FALSE;
198
199 context = base_mix->context;
200
201 gst_query_parse_allocation (query, &caps, &need_pool);
202
203 if (caps == NULL)
204 goto no_caps;
205
206 if (!gst_video_info_from_caps (&info, caps))
207 goto invalid_caps;
208
209 /* the normal size of a frame */
210 size = info.size;
211
212 if (need_pool) {
213 GST_DEBUG_OBJECT (mix, "create new pool");
214 pool = gst_gl_buffer_pool_new (context);
215
216 config = gst_buffer_pool_get_config (pool);
217 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
218
219 if (!gst_buffer_pool_set_config (pool, config)) {
220 g_object_unref (pool);
221 goto config_failed;
222 }
223 }
224
225 gst_query_add_allocation_pool (query, pool, size, 1, 0);
226 if (pool)
227 g_object_unref (pool);
228
229 /* we also support various metadata */
230 if (context->gl_vtable->FenceSync)
231 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
232
233 return TRUE;
234
235 /* ERRORS */
236 no_caps:
237 {
238 GST_DEBUG_OBJECT (mix, "no caps specified");
239 return FALSE;
240 }
241 invalid_caps:
242 {
243 GST_DEBUG_OBJECT (mix, "invalid caps specified");
244 return FALSE;
245 }
246 config_failed:
247 {
248 GST_DEBUG_OBJECT (mix, "failed setting config");
249 return FALSE;
250 }
251 }
252
253 static gboolean
gst_gl_mixer_pad_sink_acceptcaps(GstPad * pad,GstGLMixer * mix,GstCaps * caps)254 gst_gl_mixer_pad_sink_acceptcaps (GstPad * pad, GstGLMixer * mix,
255 GstCaps * caps)
256 {
257 gboolean ret;
258 GstCaps *template_caps;
259
260 GST_DEBUG_OBJECT (pad, "try accept caps of %" GST_PTR_FORMAT, caps);
261
262 template_caps = gst_pad_get_pad_template_caps (pad);
263 template_caps = gst_caps_make_writable (template_caps);
264
265 ret = gst_caps_can_intersect (caps, template_caps);
266 GST_DEBUG_OBJECT (pad, "%saccepted caps %" GST_PTR_FORMAT,
267 (ret ? "" : "not "), caps);
268 gst_caps_unref (template_caps);
269
270 return ret;
271 }
272
273 static GstCaps *
gst_gl_mixer_pad_sink_getcaps(GstPad * pad,GstGLMixer * mix,GstCaps * filter)274 gst_gl_mixer_pad_sink_getcaps (GstPad * pad, GstGLMixer * mix, GstCaps * filter)
275 {
276 GstCaps *sinkcaps;
277 GstCaps *template_caps;
278 GstCaps *filtered_caps;
279 GstCaps *returned_caps;
280
281 template_caps = gst_pad_get_pad_template_caps (pad);
282
283 sinkcaps = gst_pad_get_current_caps (pad);
284 if (sinkcaps == NULL) {
285 sinkcaps = gst_caps_ref (template_caps);
286 } else {
287 sinkcaps = gst_caps_merge (sinkcaps, gst_caps_ref (template_caps));
288 }
289
290 if (filter) {
291 filtered_caps = gst_caps_intersect (sinkcaps, filter);
292 gst_caps_unref (sinkcaps);
293 } else {
294 filtered_caps = sinkcaps; /* pass ownership */
295 }
296
297 returned_caps = gst_caps_intersect (filtered_caps, template_caps);
298
299 gst_caps_unref (template_caps);
300 gst_caps_unref (filtered_caps);
301
302 GST_DEBUG_OBJECT (pad, "returning %" GST_PTR_FORMAT, returned_caps);
303
304 return returned_caps;
305 }
306
307 static gboolean
gst_gl_mixer_sink_query(GstAggregator * agg,GstAggregatorPad * bpad,GstQuery * query)308 gst_gl_mixer_sink_query (GstAggregator * agg, GstAggregatorPad * bpad,
309 GstQuery * query)
310 {
311 gboolean ret = FALSE;
312 GstGLMixer *mix = GST_GL_MIXER (agg);
313
314 GST_TRACE ("QUERY %" GST_PTR_FORMAT, query);
315
316 switch (GST_QUERY_TYPE (query)) {
317 case GST_QUERY_CAPS:
318 {
319 GstCaps *filter, *caps;
320
321 gst_query_parse_caps (query, &filter);
322 caps = gst_gl_mixer_pad_sink_getcaps (GST_PAD (bpad), mix, filter);
323 gst_query_set_caps_result (query, caps);
324 gst_caps_unref (caps);
325 ret = TRUE;
326 break;
327 }
328 case GST_QUERY_ACCEPT_CAPS:
329 {
330 GstCaps *caps;
331
332 gst_query_parse_accept_caps (query, &caps);
333 ret = gst_gl_mixer_pad_sink_acceptcaps (GST_PAD (bpad), mix, caps);
334 gst_query_set_accept_caps_result (query, ret);
335 ret = TRUE;
336 break;
337 }
338 default:
339 ret = GST_AGGREGATOR_CLASS (parent_class)->sink_query (agg, bpad, query);
340 break;
341 }
342
343 return ret;
344 }
345
346 static void
gst_gl_mixer_pad_init(GstGLMixerPad * mixerpad)347 gst_gl_mixer_pad_init (GstGLMixerPad * mixerpad)
348 {
349 }
350
351 /* GLMixer signals and args */
352 enum
353 {
354 /* FILL ME */
355 LAST_SIGNAL
356 };
357
358 enum
359 {
360 PROP_0,
361 };
362
363 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
364 GST_PAD_SRC,
365 GST_PAD_ALWAYS,
366 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
367 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
368 "RGBA"))
369 );
370
371 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%u",
372 GST_PAD_SINK,
373 GST_PAD_REQUEST,
374 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
375 (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
376 "RGBA"))
377 );
378
379 static gboolean gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query);
380 static gboolean gst_gl_mixer_stop (GstAggregator * agg);
381 static gboolean gst_gl_mixer_start (GstAggregator * agg);
382
383 static GstFlowReturn
384 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg,
385 GstBuffer * outbuffer);
386
387 static void gst_gl_mixer_set_property (GObject * object, guint prop_id,
388 const GValue * value, GParamSpec * pspec);
389 static void gst_gl_mixer_get_property (GObject * object, guint prop_id,
390 GValue * value, GParamSpec * pspec);
391
392 static gboolean gst_gl_mixer_decide_allocation (GstAggregator * agg,
393 GstQuery * query);
394
395 static gboolean gst_gl_mixer_gl_start (GstGLBaseMixer * mix);
396 static void gst_gl_mixer_gl_stop (GstGLBaseMixer * mix);
397
398 static void gst_gl_mixer_finalize (GObject * object);
399
400 static void
gst_gl_mixer_class_init(GstGLMixerClass * klass)401 gst_gl_mixer_class_init (GstGLMixerClass * klass)
402 {
403 GObjectClass *gobject_class = (GObjectClass *) klass;
404 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
405 GstVideoAggregatorClass *videoaggregator_class =
406 (GstVideoAggregatorClass *) klass;
407 GstAggregatorClass *agg_class = (GstAggregatorClass *) klass;
408 GstGLBaseMixerClass *base_class = (GstGLBaseMixerClass *) klass;
409
410 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glmixer", 0, "OpenGL mixer");
411
412 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_gl_mixer_finalize);
413
414 gobject_class->get_property = gst_gl_mixer_get_property;
415 gobject_class->set_property = gst_gl_mixer_set_property;
416
417 gst_element_class_add_static_pad_template_with_gtype (element_class,
418 &src_factory, GST_TYPE_AGGREGATOR_PAD);
419 gst_element_class_add_static_pad_template_with_gtype (element_class,
420 &sink_factory, GST_TYPE_GL_MIXER_PAD);
421
422 agg_class->sink_query = gst_gl_mixer_sink_query;
423 agg_class->src_query = gst_gl_mixer_src_query;
424 agg_class->stop = gst_gl_mixer_stop;
425 agg_class->start = gst_gl_mixer_start;
426 agg_class->negotiated_src_caps = _negotiated_caps;
427 agg_class->decide_allocation = gst_gl_mixer_decide_allocation;
428 agg_class->propose_allocation = gst_gl_mixer_propose_allocation;
429
430 videoaggregator_class->aggregate_frames = gst_gl_mixer_aggregate_frames;
431 videoaggregator_class->find_best_format = _find_best_format;
432
433 base_class->gl_start = gst_gl_mixer_gl_start;
434 base_class->gl_stop = gst_gl_mixer_gl_stop;
435
436 /* Register the pad class */
437 g_type_class_ref (GST_TYPE_GL_MIXER_PAD);
438
439 klass->set_caps = NULL;
440
441 gst_type_mark_as_plugin_api (GST_TYPE_GL_MIXER_PAD, 0);
442 gst_type_mark_as_plugin_api (GST_TYPE_GL_MIXER, 0);
443 }
444
445 static void
gst_gl_mixer_reset(GstGLMixer * mix)446 gst_gl_mixer_reset (GstGLMixer * mix)
447 {
448 mix->priv->negotiated = FALSE;
449 }
450
451 static void
gst_gl_mixer_init(GstGLMixer * mix)452 gst_gl_mixer_init (GstGLMixer * mix)
453 {
454 mix->priv = gst_gl_mixer_get_instance_private (mix);
455
456 mix->priv->gl_resource_ready = FALSE;
457 g_mutex_init (&mix->priv->gl_resource_lock);
458 g_cond_init (&mix->priv->gl_resource_cond);
459 /* initialize variables */
460 gst_gl_mixer_reset (mix);
461 }
462
463 static void
gst_gl_mixer_finalize(GObject * object)464 gst_gl_mixer_finalize (GObject * object)
465 {
466 GstGLMixer *mix = GST_GL_MIXER (object);
467 GstGLMixerPrivate *priv = mix->priv;
468
469 if (mix->out_caps)
470 gst_caps_unref (mix->out_caps);
471
472 g_mutex_clear (&priv->gl_resource_lock);
473 g_cond_clear (&priv->gl_resource_cond);
474 G_OBJECT_CLASS (parent_class)->finalize (object);
475 }
476
477 static gboolean
gst_gl_mixer_query_caps(GstPad * pad,GstAggregator * agg,GstQuery * query)478 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
479 {
480 GstCaps *filter, *current_caps, *retcaps, *template_caps;
481
482 gst_query_parse_caps (query, &filter);
483
484 template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
485
486 current_caps = gst_pad_get_current_caps (pad);
487 if (current_caps == NULL)
488 retcaps = gst_caps_ref (template_caps);
489 else {
490 retcaps = gst_caps_merge (current_caps, template_caps);
491 template_caps = NULL;
492 }
493
494 if (filter) {
495 current_caps =
496 gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
497 gst_caps_unref (retcaps);
498 retcaps = current_caps;
499 }
500
501 gst_query_set_caps_result (query, retcaps);
502 gst_caps_unref (retcaps);
503
504 if (template_caps)
505 gst_caps_unref (template_caps);
506
507 return TRUE;
508 }
509
510 static gboolean
gst_gl_mixer_src_query(GstAggregator * agg,GstQuery * query)511 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
512 {
513 gboolean res = FALSE;
514
515 switch (GST_QUERY_TYPE (query)) {
516 case GST_QUERY_CAPS:
517 res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
518 break;
519 default:
520 res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
521 break;
522 }
523
524 return res;
525 }
526
527 static void
_mixer_create_fbo(GstGLContext * context,GstGLMixer * mix)528 _mixer_create_fbo (GstGLContext * context, GstGLMixer * mix)
529 {
530 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
531 guint out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
532 guint out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
533
534 mix->fbo =
535 gst_gl_framebuffer_new_with_default_depth (context, out_width,
536 out_height);
537 }
538
539 static gboolean
gst_gl_mixer_gl_start(GstGLBaseMixer * base_mix)540 gst_gl_mixer_gl_start (GstGLBaseMixer * base_mix)
541 {
542 return GST_GL_BASE_MIXER_CLASS (parent_class)->gl_start (base_mix);
543 }
544
545 static void
gst_gl_mixer_gl_stop(GstGLBaseMixer * base_mix)546 gst_gl_mixer_gl_stop (GstGLBaseMixer * base_mix)
547 {
548 GstGLMixer *mix = GST_GL_MIXER (base_mix);
549 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
550
551 if (mixer_class->reset)
552 mixer_class->reset (mix);
553
554 g_mutex_lock (&mix->priv->gl_resource_lock);
555 gst_clear_object (&mix->fbo);
556 g_mutex_unlock (&mix->priv->gl_resource_lock);
557
558 GST_GL_BASE_MIXER_CLASS (parent_class)->gl_stop (base_mix);
559 }
560
561 static gboolean
gst_gl_mixer_decide_allocation(GstAggregator * agg,GstQuery * query)562 gst_gl_mixer_decide_allocation (GstAggregator * agg, GstQuery * query)
563 {
564 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
565 GstGLMixer *mix = GST_GL_MIXER (base_mix);
566 GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
567 GstGLContext *context;
568 GstBufferPool *pool = NULL;
569 GstStructure *config;
570 GstCaps *caps;
571 guint min, max, size;
572 gboolean update_pool;
573
574 if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->decide_allocation (agg,
575 query))
576 return FALSE;
577
578 context = gst_gl_base_mixer_get_gl_context (base_mix);
579 if (!context) {
580 GST_WARNING_OBJECT (agg, "No OpenGL context");
581 return FALSE;
582 }
583
584 g_mutex_lock (&mix->priv->gl_resource_lock);
585 mix->priv->gl_resource_ready = FALSE;
586 if (mix->fbo)
587 gst_object_unref (mix->fbo);
588
589 gst_gl_context_thread_add (context,
590 (GstGLContextThreadFunc) _mixer_create_fbo, mix);
591 if (!mix->fbo) {
592 g_cond_signal (&mix->priv->gl_resource_cond);
593 g_mutex_unlock (&mix->priv->gl_resource_lock);
594 goto context_error;
595 }
596
597 if (mixer_class->set_caps)
598 mixer_class->set_caps (mix, mix->out_caps);
599
600 mix->priv->gl_resource_ready = TRUE;
601 g_cond_signal (&mix->priv->gl_resource_cond);
602 g_mutex_unlock (&mix->priv->gl_resource_lock);
603
604 gst_query_parse_allocation (query, &caps, NULL);
605
606 if (gst_query_get_n_allocation_pools (query) > 0) {
607 gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
608
609 update_pool = TRUE;
610 } else {
611 GstVideoInfo vinfo;
612
613 gst_video_info_init (&vinfo);
614 gst_video_info_from_caps (&vinfo, caps);
615 size = vinfo.size;
616 min = max = 0;
617 update_pool = FALSE;
618 }
619
620 if (!pool)
621 pool = gst_gl_buffer_pool_new (context);
622 config = gst_buffer_pool_get_config (pool);
623
624 gst_buffer_pool_config_set_params (config, caps, size, min, max);
625 gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
626
627 gst_buffer_pool_set_config (pool, config);
628
629 if (update_pool)
630 gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
631 else
632 gst_query_add_allocation_pool (query, pool, size, min, max);
633
634 gst_object_unref (pool);
635
636 gst_clear_object (&context);
637
638 return TRUE;
639
640 context_error:
641 {
642 GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("Context error"), (NULL));
643 return FALSE;
644 }
645 }
646
647 gboolean
gst_gl_mixer_process_textures(GstGLMixer * mix,GstBuffer * outbuf)648 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
649 {
650 GstGLMemory *out_tex;
651 gboolean res = TRUE;
652 GstVideoFrame out_frame;
653 GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
654 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
655
656 GST_TRACE ("Processing buffers");
657
658 if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
659 GST_MAP_WRITE | GST_MAP_GL)) {
660 return FALSE;
661 }
662
663 out_tex = (GstGLMemory *) out_frame.map[0].memory;
664
665 g_mutex_lock (&mix->priv->gl_resource_lock);
666 if (!mix->priv->gl_resource_ready)
667 g_cond_wait (&mix->priv->gl_resource_cond, &mix->priv->gl_resource_lock);
668
669 if (!mix->priv->gl_resource_ready) {
670 g_mutex_unlock (&mix->priv->gl_resource_lock);
671 GST_ERROR_OBJECT (mix,
672 "fbo used to render can't be created, do not run process_textures");
673 res = FALSE;
674 goto out;
675 }
676
677 mix_class->process_textures (mix, out_tex);
678
679 g_mutex_unlock (&mix->priv->gl_resource_lock);
680
681 out:
682 gst_video_frame_unmap (&out_frame);
683
684 return res;
685 }
686
687 static gboolean
gst_gl_mixer_process_buffers(GstGLMixer * mix,GstBuffer * outbuf)688 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
689 {
690 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
691
692 return mix_class->process_buffers (mix, outbuf);
693 }
694
695 static GstFlowReturn
gst_gl_mixer_aggregate_frames(GstVideoAggregator * vagg,GstBuffer * outbuf)696 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
697 {
698 GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (vagg);
699 gboolean res = FALSE;
700 GstGLMixer *mix = GST_GL_MIXER (vagg);
701 GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
702 GstGLContext *context = gst_gl_base_mixer_get_gl_context (base_mix);
703 GstGLSyncMeta *sync_meta;
704
705 if (!context) {
706 GST_DEBUG_OBJECT (vagg, "No OpenGL context, try again later");
707 return GST_AGGREGATOR_FLOW_NEED_DATA;
708 }
709
710 if (mix_class->process_buffers)
711 res = gst_gl_mixer_process_buffers (mix, outbuf);
712 else if (mix_class->process_textures)
713 res = gst_gl_mixer_process_textures (mix, outbuf);
714
715 sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
716 if (sync_meta)
717 gst_gl_sync_meta_set_sync_point (sync_meta, context);
718
719 gst_clear_object (&context);
720
721 return res ? GST_FLOW_OK : GST_FLOW_ERROR;
722 }
723
724 static void
gst_gl_mixer_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)725 gst_gl_mixer_get_property (GObject * object,
726 guint prop_id, GValue * value, GParamSpec * pspec)
727 {
728 switch (prop_id) {
729 default:
730 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
731 break;
732 }
733 }
734
735 static void
gst_gl_mixer_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)736 gst_gl_mixer_set_property (GObject * object,
737 guint prop_id, const GValue * value, GParamSpec * pspec)
738 {
739 switch (prop_id) {
740 default:
741 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
742 break;
743 }
744 }
745
746 static gboolean
gst_gl_mixer_start(GstAggregator * agg)747 gst_gl_mixer_start (GstAggregator * agg)
748 {
749 return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
750 }
751
752 static gboolean
gst_gl_mixer_stop(GstAggregator * agg)753 gst_gl_mixer_stop (GstAggregator * agg)
754 {
755 GstGLMixer *mix = GST_GL_MIXER (agg);
756
757 gst_gl_mixer_reset (mix);
758
759 return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);
760 }
761