plugins: uddate gst_type_mark_as_plugin_api() calls
[platform/upstream/gstreamer.git] / ext / gl / gstglmixer.c
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
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
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
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
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
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
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
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
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
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 *
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
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
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
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 }
443
444 static void
445 gst_gl_mixer_reset (GstGLMixer * mix)
446 {
447   mix->priv->negotiated = FALSE;
448 }
449
450 static void
451 gst_gl_mixer_init (GstGLMixer * mix)
452 {
453   mix->priv = gst_gl_mixer_get_instance_private (mix);
454
455   mix->priv->gl_resource_ready = FALSE;
456   g_mutex_init (&mix->priv->gl_resource_lock);
457   g_cond_init (&mix->priv->gl_resource_cond);
458   /* initialize variables */
459   gst_gl_mixer_reset (mix);
460 }
461
462 static void
463 gst_gl_mixer_finalize (GObject * object)
464 {
465   GstGLMixer *mix = GST_GL_MIXER (object);
466   GstGLMixerPrivate *priv = mix->priv;
467
468   if (mix->out_caps)
469     gst_caps_unref (mix->out_caps);
470
471   g_mutex_clear (&priv->gl_resource_lock);
472   g_cond_clear (&priv->gl_resource_cond);
473   G_OBJECT_CLASS (parent_class)->finalize (object);
474 }
475
476 static gboolean
477 gst_gl_mixer_query_caps (GstPad * pad, GstAggregator * agg, GstQuery * query)
478 {
479   GstCaps *filter, *current_caps, *retcaps, *template_caps;
480
481   gst_query_parse_caps (query, &filter);
482
483   template_caps = gst_pad_get_pad_template_caps (agg->srcpad);
484
485   current_caps = gst_pad_get_current_caps (pad);
486   if (current_caps == NULL)
487     retcaps = gst_caps_ref (template_caps);
488   else {
489     retcaps = gst_caps_merge (current_caps, template_caps);
490     template_caps = NULL;
491   }
492
493   if (filter) {
494     current_caps =
495         gst_caps_intersect_full (filter, retcaps, GST_CAPS_INTERSECT_FIRST);
496     gst_caps_unref (retcaps);
497     retcaps = current_caps;
498   }
499
500   gst_query_set_caps_result (query, retcaps);
501   gst_caps_unref (retcaps);
502
503   if (template_caps)
504     gst_caps_unref (template_caps);
505
506   return TRUE;
507 }
508
509 static gboolean
510 gst_gl_mixer_src_query (GstAggregator * agg, GstQuery * query)
511 {
512   gboolean res = FALSE;
513
514   switch (GST_QUERY_TYPE (query)) {
515     case GST_QUERY_CAPS:
516       res = gst_gl_mixer_query_caps (agg->srcpad, agg, query);
517       break;
518     default:
519       res = GST_AGGREGATOR_CLASS (parent_class)->src_query (agg, query);
520       break;
521   }
522
523   return res;
524 }
525
526 static void
527 _mixer_create_fbo (GstGLContext * context, GstGLMixer * mix)
528 {
529   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
530   guint out_width = GST_VIDEO_INFO_WIDTH (&vagg->info);
531   guint out_height = GST_VIDEO_INFO_HEIGHT (&vagg->info);
532
533   mix->fbo =
534       gst_gl_framebuffer_new_with_default_depth (context, out_width,
535       out_height);
536 }
537
538 static gboolean
539 gst_gl_mixer_gl_start (GstGLBaseMixer * base_mix)
540 {
541   return GST_GL_BASE_MIXER_CLASS (parent_class)->gl_start (base_mix);
542 }
543
544 static void
545 gst_gl_mixer_gl_stop (GstGLBaseMixer * base_mix)
546 {
547   GstGLMixer *mix = GST_GL_MIXER (base_mix);
548   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
549
550   if (mixer_class->reset)
551     mixer_class->reset (mix);
552
553   g_mutex_lock (&mix->priv->gl_resource_lock);
554   gst_clear_object (&mix->fbo);
555   g_mutex_unlock (&mix->priv->gl_resource_lock);
556
557   GST_GL_BASE_MIXER_CLASS (parent_class)->gl_stop (base_mix);
558 }
559
560 static gboolean
561 gst_gl_mixer_decide_allocation (GstAggregator * agg, GstQuery * query)
562 {
563   GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (agg);
564   GstGLMixer *mix = GST_GL_MIXER (base_mix);
565   GstGLMixerClass *mixer_class = GST_GL_MIXER_GET_CLASS (mix);
566   GstGLContext *context;
567   GstBufferPool *pool = NULL;
568   GstStructure *config;
569   GstCaps *caps;
570   guint min, max, size;
571   gboolean update_pool;
572
573   if (!GST_AGGREGATOR_CLASS (gst_gl_mixer_parent_class)->decide_allocation (agg,
574           query))
575     return FALSE;
576
577   context = gst_gl_base_mixer_get_gl_context (base_mix);
578   if (!context) {
579     GST_WARNING_OBJECT (agg, "No OpenGL context");
580     return FALSE;
581   }
582
583   g_mutex_lock (&mix->priv->gl_resource_lock);
584   mix->priv->gl_resource_ready = FALSE;
585   if (mix->fbo)
586     gst_object_unref (mix->fbo);
587
588   gst_gl_context_thread_add (context,
589       (GstGLContextThreadFunc) _mixer_create_fbo, mix);
590   if (!mix->fbo) {
591     g_cond_signal (&mix->priv->gl_resource_cond);
592     g_mutex_unlock (&mix->priv->gl_resource_lock);
593     goto context_error;
594   }
595
596   if (mixer_class->set_caps)
597     mixer_class->set_caps (mix, mix->out_caps);
598
599   mix->priv->gl_resource_ready = TRUE;
600   g_cond_signal (&mix->priv->gl_resource_cond);
601   g_mutex_unlock (&mix->priv->gl_resource_lock);
602
603   gst_query_parse_allocation (query, &caps, NULL);
604
605   if (gst_query_get_n_allocation_pools (query) > 0) {
606     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
607
608     update_pool = TRUE;
609   } else {
610     GstVideoInfo vinfo;
611
612     gst_video_info_init (&vinfo);
613     gst_video_info_from_caps (&vinfo, caps);
614     size = vinfo.size;
615     min = max = 0;
616     update_pool = FALSE;
617   }
618
619   if (!pool)
620     pool = gst_gl_buffer_pool_new (context);
621   config = gst_buffer_pool_get_config (pool);
622
623   gst_buffer_pool_config_set_params (config, caps, size, min, max);
624   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
625
626   gst_buffer_pool_set_config (pool, config);
627
628   if (update_pool)
629     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
630   else
631     gst_query_add_allocation_pool (query, pool, size, min, max);
632
633   gst_object_unref (pool);
634
635   gst_clear_object (&context);
636
637   return TRUE;
638
639 context_error:
640   {
641     GST_ELEMENT_ERROR (mix, RESOURCE, NOT_FOUND, ("Context error"), (NULL));
642     return FALSE;
643   }
644 }
645
646 gboolean
647 gst_gl_mixer_process_textures (GstGLMixer * mix, GstBuffer * outbuf)
648 {
649   GstGLMemory *out_tex;
650   gboolean res = TRUE;
651   GstVideoFrame out_frame;
652   GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR (mix);
653   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
654
655   GST_TRACE ("Processing buffers");
656
657   if (!gst_video_frame_map (&out_frame, &vagg->info, outbuf,
658           GST_MAP_WRITE | GST_MAP_GL)) {
659     return FALSE;
660   }
661
662   out_tex = (GstGLMemory *) out_frame.map[0].memory;
663
664   g_mutex_lock (&mix->priv->gl_resource_lock);
665   if (!mix->priv->gl_resource_ready)
666     g_cond_wait (&mix->priv->gl_resource_cond, &mix->priv->gl_resource_lock);
667
668   if (!mix->priv->gl_resource_ready) {
669     g_mutex_unlock (&mix->priv->gl_resource_lock);
670     GST_ERROR_OBJECT (mix,
671         "fbo used to render can't be created, do not run process_textures");
672     res = FALSE;
673     goto out;
674   }
675
676   mix_class->process_textures (mix, out_tex);
677
678   g_mutex_unlock (&mix->priv->gl_resource_lock);
679
680 out:
681   gst_video_frame_unmap (&out_frame);
682
683   return res;
684 }
685
686 static gboolean
687 gst_gl_mixer_process_buffers (GstGLMixer * mix, GstBuffer * outbuf)
688 {
689   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (mix);
690
691   return mix_class->process_buffers (mix, outbuf);
692 }
693
694 static GstFlowReturn
695 gst_gl_mixer_aggregate_frames (GstVideoAggregator * vagg, GstBuffer * outbuf)
696 {
697   GstGLBaseMixer *base_mix = GST_GL_BASE_MIXER (vagg);
698   gboolean res = FALSE;
699   GstGLMixer *mix = GST_GL_MIXER (vagg);
700   GstGLMixerClass *mix_class = GST_GL_MIXER_GET_CLASS (vagg);
701   GstGLContext *context = gst_gl_base_mixer_get_gl_context (base_mix);
702   GstGLSyncMeta *sync_meta;
703
704   if (!context) {
705     GST_DEBUG_OBJECT (vagg, "No OpenGL context, try again later");
706     return GST_AGGREGATOR_FLOW_NEED_DATA;
707   }
708
709   if (mix_class->process_buffers)
710     res = gst_gl_mixer_process_buffers (mix, outbuf);
711   else if (mix_class->process_textures)
712     res = gst_gl_mixer_process_textures (mix, outbuf);
713
714   sync_meta = gst_buffer_get_gl_sync_meta (outbuf);
715   if (sync_meta)
716     gst_gl_sync_meta_set_sync_point (sync_meta, context);
717
718   gst_clear_object (&context);
719
720   return res ? GST_FLOW_OK : GST_FLOW_ERROR;
721 }
722
723 static void
724 gst_gl_mixer_get_property (GObject * object,
725     guint prop_id, GValue * value, GParamSpec * pspec)
726 {
727   switch (prop_id) {
728     default:
729       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
730       break;
731   }
732 }
733
734 static void
735 gst_gl_mixer_set_property (GObject * object,
736     guint prop_id, const GValue * value, GParamSpec * pspec)
737 {
738   switch (prop_id) {
739     default:
740       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
741       break;
742   }
743 }
744
745 static gboolean
746 gst_gl_mixer_start (GstAggregator * agg)
747 {
748   return GST_AGGREGATOR_CLASS (parent_class)->start (agg);
749 }
750
751 static gboolean
752 gst_gl_mixer_stop (GstAggregator * agg)
753 {
754   GstGLMixer *mix = GST_GL_MIXER (agg);
755
756   gst_gl_mixer_reset (mix);
757
758   return GST_AGGREGATOR_CLASS (parent_class)->stop (agg);
759 }