gst-libs: Remove interfaces libs and mixer/tuner interfaces
[platform/upstream/gstreamer.git] / gst / playback / gstsubtitleoverlay.c
1 /*
2  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-subtitleoverlay
22  *
23  * #GstBin that auto-magically overlays a video stream with subtitles by
24  * autoplugging the required elements.
25  *
26  * It supports raw, timestamped text, different textual subtitle formats and
27  * DVD subpicture subtitles.
28  *
29  * <refsect2>
30  * <title>Examples</title>
31  * |[
32  * gst-launch -v filesrc location=test.mkv ! matroskademux name=demux ! "video/x-h264" ! queue2 ! decodebin ! subtitleoverlay name=overlay ! videoconvert ! autovideosink  demux. ! "video/x-dvd-subpicture" ! queue2 ! overlay.
33  * ]| This will play back the given Matroska file with h264 video and subpicture subtitles.
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "gstsubtitleoverlay.h"
42
43 #include <gst/pbutils/missing-plugins.h>
44 #include <gst/video/video.h>
45 #include <string.h>
46
47 #include "gst/glib-compat-private.h"
48
49 GST_DEBUG_CATEGORY_STATIC (subtitle_overlay_debug);
50 #define GST_CAT_DEFAULT subtitle_overlay_debug
51
52 #define IS_SUBTITLE_CHAIN_IGNORE_ERROR(flow) \
53   G_UNLIKELY (flow == GST_FLOW_ERROR || flow == GST_FLOW_NOT_NEGOTIATED)
54
55 #define IS_VIDEO_CHAIN_IGNORE_ERROR(flow) \
56   G_UNLIKELY (flow == GST_FLOW_ERROR)
57
58 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS_ANY);
62
63 static GstStaticPadTemplate video_sinktemplate =
64 GST_STATIC_PAD_TEMPLATE ("video_sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68
69 static GstStaticPadTemplate subtitle_sinktemplate =
70 GST_STATIC_PAD_TEMPLATE ("subtitle_sink",
71     GST_PAD_SINK,
72     GST_PAD_ALWAYS,
73     GST_STATIC_CAPS_ANY);
74
75 enum
76 {
77   PROP_0,
78   PROP_SILENT,
79   PROP_FONT_DESC,
80   PROP_SUBTITLE_ENCODING
81 };
82
83 #define gst_subtitle_overlay_parent_class parent_class
84 G_DEFINE_TYPE (GstSubtitleOverlay, gst_subtitle_overlay, GST_TYPE_BIN);
85
86 static GQuark _subtitle_overlay_event_marker_id = 0;
87
88 static void
89 do_async_start (GstSubtitleOverlay * self)
90 {
91   if (!self->do_async) {
92     GstMessage *msg = gst_message_new_async_start (GST_OBJECT_CAST (self));
93
94     GST_DEBUG_OBJECT (self, "Posting async-start");
95     GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
96     self->do_async = TRUE;
97   }
98 }
99
100 static void
101 do_async_done (GstSubtitleOverlay * self)
102 {
103   if (self->do_async) {
104     GstMessage *msg =
105         gst_message_new_async_done (GST_OBJECT_CAST (self), FALSE);
106
107     GST_DEBUG_OBJECT (self, "Posting async-done");
108     GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
109     self->do_async = FALSE;
110   }
111 }
112
113 static GstPadProbeReturn
114 _pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
115
116 static void
117 block_video (GstSubtitleOverlay * self)
118 {
119   if (self->video_block_id != 0)
120     return;
121
122   if (self->video_block_pad) {
123     self->video_block_id =
124         gst_pad_add_probe (self->video_block_pad,
125         GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, _pad_blocked_cb, self, NULL);
126   }
127 }
128
129 static void
130 unblock_video (GstSubtitleOverlay * self)
131 {
132   if (self->video_block_id) {
133     gst_pad_remove_probe (self->video_block_pad, self->video_block_id);
134     self->video_sink_blocked = FALSE;
135     self->video_block_id = 0;
136   }
137 }
138
139 static void
140 block_subtitle (GstSubtitleOverlay * self)
141 {
142   if (self->subtitle_block_id != 0)
143     return;
144
145   if (self->subtitle_block_pad) {
146     self->subtitle_block_id =
147         gst_pad_add_probe (self->subtitle_block_pad,
148         GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, _pad_blocked_cb, self, NULL);
149   }
150 }
151
152 static void
153 unblock_subtitle (GstSubtitleOverlay * self)
154 {
155   if (self->subtitle_block_id) {
156     gst_pad_remove_probe (self->subtitle_block_pad, self->subtitle_block_id);
157     self->subtitle_sink_blocked = FALSE;
158     self->subtitle_block_id = 0;
159   }
160 }
161
162 static void
163 gst_subtitle_overlay_finalize (GObject * object)
164 {
165   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (object);
166
167   if (self->lock) {
168     g_mutex_free (self->lock);
169     self->lock = NULL;
170   }
171
172   if (self->factories_lock) {
173     g_mutex_free (self->factories_lock);
174     self->factories_lock = NULL;
175   }
176
177   if (self->factories)
178     gst_plugin_feature_list_free (self->factories);
179   self->factories = NULL;
180   gst_caps_replace (&self->factory_caps, NULL);
181
182   if (self->font_desc) {
183     g_free (self->font_desc);
184     self->font_desc = NULL;
185   }
186
187   if (self->encoding) {
188     g_free (self->encoding);
189     self->encoding = NULL;
190   }
191
192   G_OBJECT_CLASS (parent_class)->finalize (object);
193 }
194
195 static gboolean
196 _is_renderer (GstElementFactory * factory)
197 {
198   const gchar *klass, *name;
199
200   klass = gst_element_factory_get_klass (factory);
201   name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
202
203   if (strstr (klass, "Overlay/Subtitle") != NULL ||
204       strstr (klass, "Overlay/SubPicture") != NULL)
205     return TRUE;
206   if (strcmp (name, "textoverlay") == 0)
207     return TRUE;
208   return FALSE;
209 }
210
211 static gboolean
212 _is_parser (GstElementFactory * factory)
213 {
214   const gchar *klass;
215
216   klass = gst_element_factory_get_klass (factory);
217
218   if (strstr (klass, "Parser/Subtitle") != NULL)
219     return TRUE;
220   return FALSE;
221 }
222
223 static const gchar *const _sub_pad_names[] = { "subpicture", "subpicture_sink",
224   "text", "text_sink",
225   "subtitle_sink", "subtitle"
226 };
227
228 static inline gboolean
229 _is_raw_video (GstStructure * s)
230 {
231   const gchar *name;
232
233   name = gst_structure_get_name (s);
234
235   if (g_str_equal (name, "video/x-raw"))
236     return TRUE;
237   return FALSE;
238 }
239
240 static gboolean
241 _is_video_pad (GstPad * pad, gboolean * hw_accelerated)
242 {
243   GstPad *peer = gst_pad_get_peer (pad);
244   GstCaps *caps;
245   gboolean ret;
246   const gchar *name;
247
248   if (peer) {
249     caps = gst_pad_get_current_caps (peer);
250     if (!caps) {
251       caps = gst_pad_query_caps (peer, NULL);
252     }
253     gst_object_unref (peer);
254   } else {
255     caps = gst_pad_query_caps (pad, NULL);
256   }
257
258   name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
259   if (g_str_equal (name, "video/x-raw")) {
260     ret = TRUE;
261     if (hw_accelerated)
262       *hw_accelerated = FALSE;
263
264   } else if (g_str_has_prefix (name, "video/x-surface")) {
265     ret = TRUE;
266     if (hw_accelerated)
267       *hw_accelerated = TRUE;
268   } else {
269
270     ret = FALSE;
271     if (hw_accelerated)
272       *hw_accelerated = FALSE;
273   }
274
275   gst_caps_unref (caps);
276
277   return ret;
278 }
279
280 static GstCaps *
281 _get_sub_caps (GstElementFactory * factory)
282 {
283   const GList *templates;
284   GList *walk;
285   gboolean is_parser = _is_parser (factory);
286
287   templates = gst_element_factory_get_static_pad_templates (factory);
288   for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
289     GstStaticPadTemplate *templ = walk->data;
290
291     if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
292       gboolean found = FALSE;
293
294       if (is_parser) {
295         found = TRUE;
296       } else {
297         guint i;
298
299         for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
300           if (strcmp (templ->name_template, _sub_pad_names[i]) == 0) {
301             found = TRUE;
302             break;
303           }
304         }
305       }
306       if (found)
307         return gst_static_caps_get (&templ->static_caps);
308     }
309   }
310   return NULL;
311 }
312
313 static gboolean
314 _factory_filter (GstPluginFeature * feature, GstCaps ** subcaps)
315 {
316   GstElementFactory *factory;
317   guint rank;
318   const gchar *name;
319   const GList *templates;
320   GList *walk;
321   gboolean is_renderer;
322   GstCaps *templ_caps = NULL;
323   gboolean have_video_sink = FALSE;
324
325   /* we only care about element factories */
326   if (!GST_IS_ELEMENT_FACTORY (feature))
327     return FALSE;
328
329   factory = GST_ELEMENT_FACTORY_CAST (feature);
330
331   /* only select elements with autoplugging rank or textoverlay */
332   name = gst_plugin_feature_get_name (feature);
333   rank = gst_plugin_feature_get_rank (feature);
334   if (strcmp ("textoverlay", name) != 0 && rank < GST_RANK_MARGINAL)
335     return FALSE;
336
337   /* Check if it's a renderer or a parser */
338   if (_is_renderer (factory)) {
339     is_renderer = TRUE;
340   } else if (_is_parser (factory)) {
341     is_renderer = FALSE;
342   } else {
343     return FALSE;
344   }
345
346   /* Check if there's a video sink in case of a renderer */
347   if (is_renderer) {
348     templates = gst_element_factory_get_static_pad_templates (factory);
349     for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
350       GstStaticPadTemplate *templ = walk->data;
351
352       /* we only care about the always-sink templates */
353       if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
354         if (strcmp (templ->name_template, "video") == 0 ||
355             strcmp (templ->name_template, "video_sink") == 0) {
356           have_video_sink = TRUE;
357         }
358       }
359     }
360   }
361   templ_caps = _get_sub_caps (factory);
362
363   if (is_renderer && have_video_sink && templ_caps) {
364     GST_DEBUG ("Found renderer element %s (%s) with caps %" GST_PTR_FORMAT,
365         gst_element_factory_get_longname (factory),
366         gst_plugin_feature_get_name (feature), templ_caps);
367     *subcaps = gst_caps_merge (*subcaps, templ_caps);
368     return TRUE;
369   } else if (!is_renderer && !have_video_sink && templ_caps) {
370     GST_DEBUG ("Found parser element %s (%s) with caps %" GST_PTR_FORMAT,
371         gst_element_factory_get_longname (factory),
372         gst_plugin_feature_get_name (feature), templ_caps);
373     *subcaps = gst_caps_merge (*subcaps, templ_caps);
374     return TRUE;
375   } else {
376     if (templ_caps)
377       gst_caps_unref (templ_caps);
378     return FALSE;
379   }
380 }
381
382 /* Call with factories_lock! */
383 static gboolean
384 gst_subtitle_overlay_update_factory_list (GstSubtitleOverlay * self)
385 {
386   GstRegistry *registry;
387   guint cookie;
388
389   registry = gst_registry_get ();
390   cookie = gst_registry_get_feature_list_cookie (registry);
391   if (!self->factories || self->factories_cookie != cookie) {
392     GstCaps *subcaps;
393     GList *factories;
394
395     subcaps = gst_caps_new_empty ();
396
397     factories = gst_registry_feature_filter (registry,
398         (GstPluginFeatureFilter) _factory_filter, FALSE, &subcaps);
399     GST_DEBUG_OBJECT (self, "Created factory caps: %" GST_PTR_FORMAT, subcaps);
400     gst_caps_replace (&self->factory_caps, subcaps);
401     gst_caps_unref (subcaps);
402     if (self->factories)
403       gst_plugin_feature_list_free (self->factories);
404     self->factories = factories;
405     self->factories_cookie = cookie;
406   }
407
408   return (self->factories != NULL);
409 }
410
411 G_LOCK_DEFINE_STATIC (_factory_caps);
412 static GstCaps *_factory_caps = NULL;
413 static guint32 _factory_caps_cookie = 0;
414
415 GstCaps *
416 gst_subtitle_overlay_create_factory_caps (void)
417 {
418   GstRegistry *registry;
419   GList *factories;
420   GstCaps *subcaps = NULL;
421   guint cookie;
422
423   registry = gst_registry_get ();
424   cookie = gst_registry_get_feature_list_cookie (registry);
425   G_LOCK (_factory_caps);
426   if (!_factory_caps || _factory_caps_cookie != cookie) {
427     if (_factory_caps)
428       gst_caps_unref (_factory_caps);
429     _factory_caps = gst_caps_new_empty ();
430
431     factories = gst_registry_feature_filter (registry,
432         (GstPluginFeatureFilter) _factory_filter, FALSE, &_factory_caps);
433     GST_DEBUG ("Created factory caps: %" GST_PTR_FORMAT, _factory_caps);
434     gst_plugin_feature_list_free (factories);
435     _factory_caps_cookie = cookie;
436   }
437   subcaps = gst_caps_ref (_factory_caps);
438   G_UNLOCK (_factory_caps);
439
440   return subcaps;
441 }
442
443 static gboolean
444 check_factory_for_caps (GstElementFactory * factory, const GstCaps * caps)
445 {
446   GstCaps *fcaps = _get_sub_caps (factory);
447   gboolean ret = (fcaps) ? gst_caps_can_intersect (fcaps, caps) : FALSE;
448
449   if (fcaps)
450     gst_caps_unref (fcaps);
451
452   if (ret)
453     gst_object_ref (factory);
454   return ret;
455 }
456
457 static GList *
458 gst_subtitle_overlay_get_factories_for_caps (const GList * list,
459     const GstCaps * caps)
460 {
461   const GList *walk = list;
462   GList *result = NULL;
463
464   while (walk) {
465     GstElementFactory *factory = walk->data;
466
467     walk = g_list_next (walk);
468
469     if (check_factory_for_caps (factory, caps)) {
470       result = g_list_prepend (result, factory);
471     }
472   }
473
474   return result;
475 }
476
477 static gint
478 _sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
479 {
480   gint diff;
481   const gchar *rname1, *rname2;
482
483   diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
484   if (diff != 0)
485     return diff;
486
487   /* If the ranks are the same sort by name to get deterministic results */
488   rname1 = gst_plugin_feature_get_name (f1);
489   rname2 = gst_plugin_feature_get_name (f2);
490
491   diff = strcmp (rname1, rname2);
492
493   return diff;
494 }
495
496 static GstPad *
497 _get_sub_pad (GstElement * element)
498 {
499   GstPad *pad;
500   guint i;
501
502   for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
503     pad = gst_element_get_static_pad (element, _sub_pad_names[i]);
504     if (pad)
505       return pad;
506   }
507   return NULL;
508 }
509
510 static GstPad *
511 _get_video_pad (GstElement * element)
512 {
513   static const gchar *const pad_names[] = { "video", "video_sink" };
514   GstPad *pad;
515   guint i;
516
517   for (i = 0; i < G_N_ELEMENTS (pad_names); i++) {
518     pad = gst_element_get_static_pad (element, pad_names[i]);
519     if (pad)
520       return pad;
521   }
522   return NULL;
523 }
524
525 static gboolean
526 _create_element (GstSubtitleOverlay * self, GstElement ** element,
527     const gchar * factory_name, GstElementFactory * factory,
528     const gchar * element_name, gboolean mandatory)
529 {
530   GstElement *elt;
531
532   g_assert (!factory || !factory_name);
533
534   if (factory_name) {
535     elt = gst_element_factory_make (factory_name, element_name);
536   } else {
537     factory_name =
538         gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
539     elt = gst_element_factory_create (factory, element_name);
540   }
541
542   if (G_UNLIKELY (!elt)) {
543     if (!factory) {
544       GstMessage *msg;
545
546       msg =
547           gst_missing_element_message_new (GST_ELEMENT_CAST (self),
548           factory_name);
549       gst_element_post_message (GST_ELEMENT_CAST (self), msg);
550
551       if (mandatory)
552         GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
553             ("no '%s' plugin found", factory_name));
554       else
555         GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
556             ("no '%s' plugin found", factory_name));
557     } else {
558       if (mandatory) {
559         GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
560             ("can't instantiate '%s'", factory_name));
561       } else {
562         GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
563             ("can't instantiate '%s'", factory_name));
564       }
565     }
566
567     return FALSE;
568   }
569
570   if (G_UNLIKELY (gst_element_set_state (elt,
571               GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS)) {
572     gst_object_unref (elt);
573     if (mandatory) {
574       GST_ELEMENT_ERROR (self, CORE, STATE_CHANGE, (NULL),
575           ("failed to set '%s' to READY", factory_name));
576     } else {
577       GST_WARNING_OBJECT (self, "Failed to set '%s' to READY", factory_name);
578     }
579     return FALSE;
580   }
581
582   if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (self), gst_object_ref (elt)))) {
583     gst_element_set_state (elt, GST_STATE_NULL);
584     gst_object_unref (elt);
585     if (mandatory) {
586       GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
587           ("failed to add '%s' to subtitleoverlay", factory_name));
588     } else {
589       GST_WARNING_OBJECT (self, "Failed to add '%s' to subtitleoverlay",
590           factory_name);
591     }
592     return FALSE;
593   }
594
595   gst_element_sync_state_with_parent (elt);
596   *element = elt;
597   return TRUE;
598 }
599
600 static void
601 _remove_element (GstSubtitleOverlay * self, GstElement ** element)
602 {
603   if (*element) {
604     gst_bin_remove (GST_BIN_CAST (self), *element);
605     gst_element_set_state (*element, GST_STATE_NULL);
606     gst_object_unref (*element);
607     *element = NULL;
608   }
609 }
610
611 static void
612 _generate_update_segment_event (GstSegment * segment, GstEvent ** event1)
613 {
614   GstEvent *event;
615   GstStructure *structure;
616
617   event = gst_event_new_segment (segment);
618   structure = gst_event_writable_structure (event);
619   gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
620       G_TYPE_BOOLEAN, TRUE, NULL);
621   *event1 = event;
622 }
623
624 static gboolean
625 _setup_passthrough (GstSubtitleOverlay * self)
626 {
627   GstPad *src, *sink;
628   GstElement *identity;
629
630   GST_DEBUG_OBJECT (self, "Doing video passthrough");
631
632   if (self->passthrough_identity) {
633     GST_DEBUG_OBJECT (self, "Already in passthrough mode");
634     goto out;
635   }
636
637   /* Unlink & destroy everything */
638   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
639   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
640   gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad), NULL);
641   self->silent_property = NULL;
642   _remove_element (self, &self->post_colorspace);
643   _remove_element (self, &self->overlay);
644   _remove_element (self, &self->parser);
645   _remove_element (self, &self->renderer);
646   _remove_element (self, &self->pre_colorspace);
647   _remove_element (self, &self->passthrough_identity);
648
649   if (G_UNLIKELY (!_create_element (self, &self->passthrough_identity,
650               "identity", NULL, "passthrough-identity", TRUE))) {
651     return FALSE;
652   }
653
654   identity = self->passthrough_identity;
655   g_object_set (G_OBJECT (identity), "silent", TRUE, "signal-handoffs", FALSE,
656       NULL);
657
658   /* Set src ghostpad target */
659   src = gst_element_get_static_pad (self->passthrough_identity, "src");
660   if (G_UNLIKELY (!src)) {
661     GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
662         ("Failed to get srcpad from identity"));
663     return FALSE;
664   }
665
666   if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
667               src))) {
668     GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
669         ("Failed to set srcpad target"));
670     gst_object_unref (src);
671     return FALSE;
672   }
673   gst_object_unref (src);
674
675   sink = gst_element_get_static_pad (self->passthrough_identity, "sink");
676   if (G_UNLIKELY (!sink)) {
677     GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
678         ("Failed to get sinkpad from identity"));
679     return FALSE;
680   }
681
682   /* Send segment to the identity. This is dropped because identity
683    * is not linked downstream yet */
684   if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
685     GstEvent *event1;
686
687     _generate_update_segment_event (&self->video_segment, &event1);
688     GST_DEBUG_OBJECT (self,
689         "Pushing video segment event: %" GST_PTR_FORMAT, event1);
690     gst_pad_send_event (sink, event1);
691   }
692
693   /* Link sink ghostpads to identity */
694   if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
695               (self->video_sinkpad), sink))) {
696     GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
697         ("Failed to set video sinkpad target"));
698     gst_object_unref (sink);
699     return FALSE;
700   }
701   gst_object_unref (sink);
702
703   GST_DEBUG_OBJECT (self, "Video passthrough setup successfully");
704
705 out:
706   /* Unblock pads */
707   unblock_video (self);
708   unblock_subtitle (self);
709
710   return TRUE;
711 }
712
713 /* Must be called with subtitleoverlay lock! */
714 static gboolean
715 _has_property_with_type (GObject * object, const gchar * property, GType type)
716 {
717   GObjectClass *gobject_class;
718   GParamSpec *pspec;
719
720   gobject_class = G_OBJECT_GET_CLASS (object);
721   pspec = g_object_class_find_property (gobject_class, property);
722   return (pspec && pspec->value_type == type);
723 }
724
725 static void
726 gst_subtitle_overlay_set_fps (GstSubtitleOverlay * self)
727 {
728   if (!self->parser || self->fps_d == 0)
729     return;
730
731   if (!_has_property_with_type (G_OBJECT (self->parser), "video-fps",
732           GST_TYPE_FRACTION))
733     return;
734
735   GST_DEBUG_OBJECT (self, "Updating video-fps property in parser");
736   g_object_set (self->parser, "video-fps", self->fps_n, self->fps_d, NULL);
737 }
738
739 static const gchar *
740 _get_silent_property (GstElement * element, gboolean * invert)
741 {
742   static const struct
743   {
744     const gchar *name;
745     gboolean invert;
746   } properties[] = { {
747   "silent", FALSE}, {
748   "enable", TRUE}};
749   guint i;
750
751   for (i = 0; i < G_N_ELEMENTS (properties); i++) {
752     if (_has_property_with_type (G_OBJECT (element), properties[i].name,
753             G_TYPE_BOOLEAN)) {
754       *invert = properties[i].invert;
755       return properties[i].name;
756     }
757   }
758   return NULL;
759 }
760
761 static gboolean
762 _setup_parser (GstSubtitleOverlay * self)
763 {
764   GstPad *video_peer;
765
766   /* Try to get the latest video framerate */
767   video_peer = gst_pad_get_peer (self->video_sinkpad);
768   if (video_peer) {
769     GstCaps *video_caps;
770     gint fps_n, fps_d;
771
772     video_caps = gst_pad_get_current_caps (video_peer);
773     if (!video_caps) {
774       video_caps = gst_pad_query_caps (video_peer, NULL);
775       if (!gst_caps_is_fixed (video_caps)) {
776         gst_caps_unref (video_caps);
777         video_caps = NULL;
778       }
779     }
780
781     if (video_caps) {
782       GstStructure *st = gst_caps_get_structure (video_caps, 0);
783       if (gst_structure_get_fraction (st, "framerate", &fps_n, &fps_d)) {
784         GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
785         self->fps_n = fps_n;
786         self->fps_d = fps_d;
787       }
788     }
789
790     if (video_caps)
791       gst_caps_unref (video_caps);
792     gst_object_unref (video_peer);
793   }
794
795   if (_has_property_with_type (G_OBJECT (self->parser), "subtitle-encoding",
796           G_TYPE_STRING))
797     g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
798
799   /* Try to set video fps on the parser */
800   gst_subtitle_overlay_set_fps (self);
801
802
803   return TRUE;
804 }
805
806 static gboolean
807 _setup_renderer (GstSubtitleOverlay * self, GstElement * renderer)
808 {
809   GstElementFactory *factory = gst_element_get_factory (renderer);
810   const gchar *name =
811       gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
812
813   if (strcmp (name, "textoverlay") == 0) {
814     /* Set some textoverlay specific properties */
815     g_object_set (G_OBJECT (renderer),
816         "halign", "center", "valign", "bottom", "wait-text", FALSE, NULL);
817     if (self->font_desc)
818       g_object_set (G_OBJECT (renderer), "font-desc", self->font_desc, NULL);
819     self->silent_property = "silent";
820     self->silent_property_invert = FALSE;
821   } else {
822     self->silent_property =
823         _get_silent_property (renderer, &self->silent_property_invert);
824     if (_has_property_with_type (G_OBJECT (renderer), "subtitle-encoding",
825             G_TYPE_STRING))
826       g_object_set (renderer, "subtitle-encoding", self->encoding, NULL);
827     if (_has_property_with_type (G_OBJECT (renderer), "font-desc",
828             G_TYPE_STRING))
829       g_object_set (renderer, "font-desc", self->font_desc, NULL);
830   }
831
832   return TRUE;
833 }
834
835 /* subtitle_src==NULL means: use subtitle_sink ghostpad */
836 static gboolean
837 _link_renderer (GstSubtitleOverlay * self, GstElement * renderer,
838     GstPad * subtitle_src)
839 {
840   GstPad *sink, *src;
841   gboolean is_video, is_hw;
842
843   is_video = _is_video_pad (self->video_sinkpad, &is_hw);
844
845   if (is_video) {
846     gboolean render_is_hw;
847
848     /* First check that renderer also supports the video format */
849     sink = _get_video_pad (renderer);
850     if (G_UNLIKELY (!sink)) {
851       GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
852       return FALSE;
853     }
854
855     if (is_video != _is_video_pad (sink, &render_is_hw) ||
856         is_hw != render_is_hw) {
857       GST_DEBUG_OBJECT (self, "Renderer doesn't support %s video",
858           is_hw ? "surface" : "raw");
859       gst_object_unref (sink);
860       return FALSE;
861     }
862     gst_object_unref (sink);
863
864     if (!is_hw) {
865       /* First link everything internally */
866       if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
867                   COLORSPACE, NULL, "post-colorspace", FALSE))) {
868         return FALSE;
869       }
870       src = gst_element_get_static_pad (renderer, "src");
871       if (G_UNLIKELY (!src)) {
872         GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
873         return FALSE;
874       }
875
876       sink = gst_element_get_static_pad (self->post_colorspace, "sink");
877       if (G_UNLIKELY (!sink)) {
878         GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
879         gst_object_unref (src);
880         return FALSE;
881       }
882
883       if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
884         GST_WARNING_OBJECT (self, "Can't link renderer with " COLORSPACE);
885         gst_object_unref (src);
886         gst_object_unref (sink);
887         return FALSE;
888       }
889       gst_object_unref (src);
890       gst_object_unref (sink);
891
892       if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
893                   COLORSPACE, NULL, "pre-colorspace", FALSE))) {
894         return FALSE;
895       }
896
897       sink = _get_video_pad (renderer);
898       if (G_UNLIKELY (!sink)) {
899         GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
900         return FALSE;
901       }
902
903       src = gst_element_get_static_pad (self->pre_colorspace, "src");
904       if (G_UNLIKELY (!src)) {
905         GST_WARNING_OBJECT (self, "Can't get srcpad from " COLORSPACE);
906         gst_object_unref (sink);
907         return FALSE;
908       }
909
910       if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
911         GST_WARNING_OBJECT (self, "Can't link " COLORSPACE " to renderer");
912         gst_object_unref (src);
913         gst_object_unref (sink);
914         return FALSE;
915       }
916       gst_object_unref (src);
917       gst_object_unref (sink);
918
919       /* Set src ghostpad target */
920       src = gst_element_get_static_pad (self->post_colorspace, "src");
921       if (G_UNLIKELY (!src)) {
922         GST_WARNING_OBJECT (self, "Can't get src pad from " COLORSPACE);
923         return FALSE;
924       }
925     } else {
926       /* Set src ghostpad target in the harware accelerated case */
927
928       src = gst_element_get_static_pad (renderer, "src");
929       if (G_UNLIKELY (!src)) {
930         GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
931         return FALSE;
932       }
933     }
934   } else {                      /* No video pad */
935     GstCaps *allowed_caps, *video_caps = NULL;
936     GstPad *video_peer;
937     gboolean can_intersect = FALSE;
938
939     video_peer = gst_pad_get_peer (self->video_sinkpad);
940     if (video_peer) {
941       video_caps = gst_pad_get_current_caps (video_peer);
942       if (!video_caps) {
943         video_caps = gst_pad_query_caps (video_peer, NULL);
944       }
945       gst_object_unref (video_peer);
946     }
947
948     sink = _get_video_pad (renderer);
949     if (G_UNLIKELY (!sink)) {
950       GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
951       return FALSE;
952     }
953     allowed_caps = gst_pad_query_caps (sink, NULL);
954     gst_object_unref (sink);
955
956     if (allowed_caps && video_caps)
957       can_intersect = gst_caps_can_intersect (allowed_caps, video_caps);
958
959     if (allowed_caps)
960       gst_caps_unref (allowed_caps);
961
962     if (video_caps)
963       gst_caps_unref (video_caps);
964
965     if (G_UNLIKELY (!can_intersect)) {
966       GST_WARNING_OBJECT (self, "Renderer with custom caps is not "
967           "compatible with video stream");
968       return FALSE;
969     }
970
971     src = gst_element_get_static_pad (renderer, "src");
972     if (G_UNLIKELY (!src)) {
973       GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
974       return FALSE;
975     }
976   }
977
978   if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
979               (self->srcpad), src))) {
980     GST_WARNING_OBJECT (self, "Can't set srcpad target");
981     gst_object_unref (src);
982     return FALSE;
983   }
984   gst_object_unref (src);
985
986   /* Set the sink ghostpad targets */
987   if (self->pre_colorspace) {
988     sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
989     if (G_UNLIKELY (!sink)) {
990       GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
991       return FALSE;
992     }
993   } else {
994     sink = _get_video_pad (renderer);
995     if (G_UNLIKELY (!sink)) {
996       GST_WARNING_OBJECT (self, "Can't get sink pad from %" GST_PTR_FORMAT,
997           renderer);
998       return FALSE;
999     }
1000   }
1001
1002   if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1003               (self->video_sinkpad), sink))) {
1004     GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
1005     gst_object_unref (sink);
1006     return FALSE;
1007   }
1008   gst_object_unref (sink);
1009
1010   sink = _get_sub_pad (renderer);
1011   if (G_UNLIKELY (!sink)) {
1012     GST_WARNING_OBJECT (self, "Failed to get subpad");
1013     return FALSE;
1014   }
1015
1016   if (subtitle_src) {
1017     if (G_UNLIKELY (gst_pad_link (subtitle_src, sink) != GST_PAD_LINK_OK)) {
1018       GST_WARNING_OBJECT (self, "Failed to link subtitle srcpad with renderer");
1019       gst_object_unref (sink);
1020       return FALSE;
1021     }
1022   } else {
1023     if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1024                 (self->subtitle_sinkpad), sink))) {
1025       GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
1026       gst_object_unref (sink);
1027       return FALSE;
1028     }
1029   }
1030   gst_object_unref (sink);
1031
1032   return TRUE;
1033 }
1034
1035 static GstPadProbeReturn
1036 _pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
1037 {
1038   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (user_data);
1039   GstCaps *subcaps;
1040   GList *l, *factories = NULL;
1041
1042   GST_DEBUG_OBJECT (pad, "Pad blocked");
1043
1044   GST_SUBTITLE_OVERLAY_LOCK (self);
1045   if (pad == self->video_block_pad)
1046     self->video_sink_blocked = TRUE;
1047   else if (pad == self->subtitle_block_pad)
1048     self->subtitle_sink_blocked = TRUE;
1049
1050   /* Now either both or the video sink are blocked */
1051
1052   /* Get current subtitle caps */
1053   subcaps = self->subcaps;
1054   if (!subcaps) {
1055     GstPad *peer;
1056
1057     peer = gst_pad_get_peer (self->subtitle_sinkpad);
1058     if (peer) {
1059       subcaps = gst_pad_get_current_caps (peer);
1060       if (!subcaps) {
1061         subcaps = gst_pad_query_caps (peer, NULL);
1062         if (!gst_caps_is_fixed (subcaps)) {
1063           gst_caps_unref (subcaps);
1064           subcaps = NULL;
1065         }
1066       }
1067       gst_object_unref (peer);
1068     }
1069     gst_caps_replace (&self->subcaps, subcaps);
1070     if (subcaps)
1071       gst_caps_unref (subcaps);
1072   }
1073   GST_DEBUG_OBJECT (self, "Current subtitle caps: %" GST_PTR_FORMAT, subcaps);
1074
1075   /* If there are no subcaps but the subtitle sink is blocked upstream
1076    * must behave wrong as there are no fixed caps set for the first
1077    * buffer or in-order event */
1078   if (G_UNLIKELY (!subcaps && self->subtitle_sink_blocked)) {
1079     GST_ELEMENT_WARNING (self, CORE, NEGOTIATION, (NULL),
1080         ("Subtitle sink is blocked but we have no subtitle caps"));
1081     subcaps = NULL;
1082   }
1083
1084   if (self->subtitle_error || (self->silent && !self->silent_property)) {
1085     _setup_passthrough (self);
1086     do_async_done (self);
1087     goto out;
1088   }
1089
1090   /* Now do something with the caps */
1091   if (subcaps && !self->subtitle_flush) {
1092     GstPad *target =
1093         gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1094
1095     if (target && gst_pad_query_accept_caps (target, subcaps)) {
1096       GST_DEBUG_OBJECT (pad, "Target accepts caps");
1097
1098       gst_object_unref (target);
1099
1100       /* Unblock pads */
1101       unblock_video (self);
1102       unblock_subtitle (self);
1103       goto out;
1104     } else if (target) {
1105       gst_object_unref (target);
1106     }
1107   }
1108
1109   if (self->subtitle_sink_blocked && !self->video_sink_blocked) {
1110     GST_DEBUG_OBJECT (self, "Subtitle sink blocked but video not blocked");
1111     block_video (self);
1112     goto out;
1113   }
1114
1115   self->subtitle_flush = FALSE;
1116
1117   /* Find our factories */
1118   g_mutex_lock (self->factories_lock);
1119   gst_subtitle_overlay_update_factory_list (self);
1120   if (subcaps) {
1121     factories =
1122         gst_subtitle_overlay_get_factories_for_caps (self->factories, subcaps);
1123     if (!factories) {
1124       GstMessage *msg;
1125
1126       msg = gst_missing_decoder_message_new (GST_ELEMENT_CAST (self), subcaps);
1127       gst_element_post_message (GST_ELEMENT_CAST (self), msg);
1128       GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
1129           ("no suitable subtitle plugin found"));
1130       subcaps = NULL;
1131       self->subtitle_error = TRUE;
1132     }
1133   }
1134   g_mutex_unlock (self->factories_lock);
1135
1136   if (!subcaps) {
1137     _setup_passthrough (self);
1138     do_async_done (self);
1139     goto out;
1140   }
1141
1142   /* Now the interesting parts are done: subtitle overlaying! */
1143
1144   /* Sort the factories by rank */
1145   factories = g_list_sort (factories, (GCompareFunc) _sort_by_ranks);
1146
1147   for (l = factories; l; l = l->next) {
1148     GstElementFactory *factory = l->data;
1149     gboolean is_renderer = _is_renderer (factory);
1150     GstPad *sink, *src;
1151
1152     /* Unlink & destroy everything */
1153
1154     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1155     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1156     gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1157         NULL);
1158     self->silent_property = NULL;
1159     _remove_element (self, &self->post_colorspace);
1160     _remove_element (self, &self->overlay);
1161     _remove_element (self, &self->parser);
1162     _remove_element (self, &self->renderer);
1163     _remove_element (self, &self->pre_colorspace);
1164     _remove_element (self, &self->passthrough_identity);
1165
1166     GST_DEBUG_OBJECT (self, "Trying factory '%s'",
1167         GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1168                 (factory))));
1169
1170     if (G_UNLIKELY ((is_renderer
1171                 && !_create_element (self, &self->renderer, NULL, factory,
1172                     "renderer", FALSE)) || (!is_renderer
1173                 && !_create_element (self, &self->parser, NULL, factory,
1174                     "parser", FALSE))))
1175       continue;
1176
1177     if (!is_renderer) {
1178       GstCaps *parser_caps;
1179       GList *overlay_factories, *k;
1180
1181       if (!_setup_parser (self))
1182         continue;
1183
1184       /* Find our factories */
1185       src = gst_element_get_static_pad (self->parser, "src");
1186       parser_caps = gst_pad_query_caps (src, NULL);
1187       gst_object_unref (src);
1188
1189       g_assert (parser_caps != NULL);
1190
1191       g_mutex_lock (self->factories_lock);
1192       gst_subtitle_overlay_update_factory_list (self);
1193       GST_DEBUG_OBJECT (self,
1194           "Searching overlay factories for caps %" GST_PTR_FORMAT, parser_caps);
1195       overlay_factories =
1196           gst_subtitle_overlay_get_factories_for_caps (self->factories,
1197           parser_caps);
1198       g_mutex_unlock (self->factories_lock);
1199
1200       if (!overlay_factories) {
1201         GST_WARNING_OBJECT (self,
1202             "Found no suitable overlay factories for caps %" GST_PTR_FORMAT,
1203             parser_caps);
1204         gst_caps_unref (parser_caps);
1205         continue;
1206       }
1207       gst_caps_unref (parser_caps);
1208
1209       /* Sort the factories by rank */
1210       overlay_factories =
1211           g_list_sort (overlay_factories, (GCompareFunc) _sort_by_ranks);
1212
1213       for (k = overlay_factories; k; k = k->next) {
1214         GstElementFactory *overlay_factory = k->data;
1215
1216         GST_DEBUG_OBJECT (self, "Trying overlay factory '%s'",
1217             GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1218                     (overlay_factory))));
1219
1220         /* Try this factory and link it, otherwise unlink everything
1221          * again and remove the overlay. Up to this point only the
1222          * parser was instantiated and setup, nothing was linked
1223          */
1224
1225         gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1226         gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad),
1227             NULL);
1228         gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1229             NULL);
1230         self->silent_property = NULL;
1231         _remove_element (self, &self->post_colorspace);
1232         _remove_element (self, &self->overlay);
1233         _remove_element (self, &self->pre_colorspace);
1234
1235         if (!_create_element (self, &self->overlay, NULL, overlay_factory,
1236                 "overlay", FALSE)) {
1237           GST_DEBUG_OBJECT (self, "Could not create element");
1238           continue;
1239         }
1240
1241         if (!_setup_renderer (self, self->overlay)) {
1242           GST_DEBUG_OBJECT (self, "Could not setup element");
1243           continue;
1244         }
1245
1246         src = gst_element_get_static_pad (self->parser, "src");
1247         if (!_link_renderer (self, self->overlay, src)) {
1248           GST_DEBUG_OBJECT (self, "Could not link element");
1249           gst_object_unref (src);
1250           continue;
1251         }
1252         gst_object_unref (src);
1253
1254         /* Everything done here, go out of loop */
1255         GST_DEBUG_OBJECT (self, "%s is a suitable element",
1256             GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1257                     (overlay_factory))));
1258         break;
1259       }
1260
1261       if (overlay_factories)
1262         gst_plugin_feature_list_free (overlay_factories);
1263
1264       if (G_UNLIKELY (k == NULL)) {
1265         GST_WARNING_OBJECT (self, "Failed to find usable overlay factory");
1266         continue;
1267       }
1268
1269       /* Now link subtitle sinkpad of the bin and the parser */
1270       sink = gst_element_get_static_pad (self->parser, "sink");
1271       if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1272               (self->subtitle_sinkpad), sink)) {
1273         gst_object_unref (sink);
1274         continue;
1275       }
1276       gst_object_unref (sink);
1277
1278       /* Everything done here, go out of loop */
1279       break;
1280     } else {
1281       /* Is renderer factory */
1282
1283       if (!_setup_renderer (self, self->renderer))
1284         continue;
1285
1286       /* subtitle_src==NULL means: use subtitle_sink ghostpad */
1287       if (!_link_renderer (self, self->renderer, NULL))
1288         continue;
1289
1290       /* Everything done here, go out of loop */
1291       break;
1292     }
1293   }
1294
1295   if (G_UNLIKELY (l == NULL)) {
1296     GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
1297         ("Failed to find any usable factories"));
1298     self->subtitle_error = TRUE;
1299     _setup_passthrough (self);
1300     do_async_done (self);
1301     goto out;
1302   }
1303
1304   /* Send segments to the renderer if necessary. These are not sent
1305    * outside this element because of the proxy pad event handler */
1306   if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
1307     GstEvent *event1;
1308     GstPad *sink;
1309
1310     if (self->pre_colorspace) {
1311       sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1312     } else {
1313       sink = _get_video_pad ((self->renderer) ? self->renderer : self->overlay);
1314     }
1315
1316     _generate_update_segment_event (&self->video_segment, &event1);
1317     GST_DEBUG_OBJECT (self,
1318         "Pushing video update segment event: %" GST_PTR_FORMAT,
1319         gst_event_get_structure (event1));
1320     gst_pad_send_event (sink, event1);
1321     gst_object_unref (sink);
1322   }
1323
1324   if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
1325     GstEvent *event1;
1326     GstPad *sink;
1327
1328     if (self->renderer)
1329       sink = _get_sub_pad (self->renderer);
1330     else
1331       sink = gst_element_get_static_pad (self->parser, "sink");
1332
1333     _generate_update_segment_event (&self->subtitle_segment, &event1);
1334     GST_DEBUG_OBJECT (self,
1335         "Pushing subtitle update segment event: %" GST_PTR_FORMAT,
1336         gst_event_get_structure (event1));
1337     gst_pad_send_event (sink, event1);
1338     gst_object_unref (sink);
1339   }
1340
1341   GST_DEBUG_OBJECT (self, "Everything worked, unblocking pads");
1342   unblock_video (self);
1343   unblock_subtitle (self);
1344   do_async_done (self);
1345
1346 out:
1347   if (factories)
1348     gst_plugin_feature_list_free (factories);
1349   GST_SUBTITLE_OVERLAY_UNLOCK (self);
1350
1351   return GST_PAD_PROBE_OK;
1352 }
1353
1354 static GstStateChangeReturn
1355 gst_subtitle_overlay_change_state (GstElement * element,
1356     GstStateChange transition)
1357 {
1358   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (element);
1359   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1360
1361   switch (transition) {
1362     case GST_STATE_CHANGE_NULL_TO_READY:
1363       GST_DEBUG_OBJECT (self, "State change NULL->READY");
1364       g_mutex_lock (self->factories_lock);
1365       if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self))) {
1366         g_mutex_unlock (self->factories_lock);
1367         return GST_STATE_CHANGE_FAILURE;
1368       }
1369       g_mutex_unlock (self->factories_lock);
1370
1371       GST_SUBTITLE_OVERLAY_LOCK (self);
1372       /* Set the internal pads to blocking */
1373       block_video (self);
1374       block_subtitle (self);
1375       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1376       break;
1377     case GST_STATE_CHANGE_READY_TO_PAUSED:
1378       GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
1379       gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1380       gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
1381
1382       self->fps_n = self->fps_d = 0;
1383
1384       self->subtitle_flush = FALSE;
1385       self->subtitle_error = FALSE;
1386
1387       self->downstream_chain_error = FALSE;
1388
1389       do_async_start (self);
1390       ret = GST_STATE_CHANGE_ASYNC;
1391
1392       break;
1393     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1394       GST_DEBUG_OBJECT (self, "State change PAUSED->PLAYING");
1395     default:
1396       break;
1397   }
1398
1399   {
1400     GstStateChangeReturn bret;
1401
1402     bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1403     GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", bret);
1404     if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
1405       return ret;
1406     else if (bret == GST_STATE_CHANGE_ASYNC)
1407       ret = bret;
1408     else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
1409       do_async_done (self);
1410       ret = bret;
1411     }
1412   }
1413
1414   switch (transition) {
1415     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1416       GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
1417       break;
1418     case GST_STATE_CHANGE_PAUSED_TO_READY:
1419       GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
1420
1421       /* Set the pads back to blocking state */
1422       GST_SUBTITLE_OVERLAY_LOCK (self);
1423       block_video (self);
1424       block_subtitle (self);
1425       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1426
1427       do_async_done (self);
1428
1429       break;
1430     case GST_STATE_CHANGE_READY_TO_NULL:
1431       GST_DEBUG_OBJECT (self, "State change READY->NULL");
1432
1433       GST_SUBTITLE_OVERLAY_LOCK (self);
1434       gst_caps_replace (&self->subcaps, NULL);
1435
1436       /* Unlink ghost pads */
1437       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1438       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1439       gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1440           NULL);
1441
1442       /* Unblock pads */
1443       unblock_video (self);
1444       unblock_subtitle (self);
1445
1446       /* Remove elements */
1447       self->silent_property = NULL;
1448       _remove_element (self, &self->post_colorspace);
1449       _remove_element (self, &self->overlay);
1450       _remove_element (self, &self->parser);
1451       _remove_element (self, &self->renderer);
1452       _remove_element (self, &self->pre_colorspace);
1453       _remove_element (self, &self->passthrough_identity);
1454       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1455
1456       break;
1457     default:
1458       break;
1459   }
1460
1461   return ret;
1462 }
1463
1464 static void
1465 gst_subtitle_overlay_handle_message (GstBin * bin, GstMessage * message)
1466 {
1467   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (bin);
1468
1469   if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1470     GstObject *src = GST_MESSAGE_SRC (message);
1471
1472     /* Convert error messages from the subtitle pipeline to
1473      * warnings and switch to passthrough mode */
1474     if (src && (
1475             (self->overlay
1476                 && gst_object_has_ancestor (src,
1477                     GST_OBJECT_CAST (self->overlay))) || (self->parser
1478                 && gst_object_has_ancestor (src,
1479                     GST_OBJECT_CAST (self->parser))) || (self->renderer
1480                 && gst_object_has_ancestor (src,
1481                     GST_OBJECT_CAST (self->renderer))))) {
1482       GError *err = NULL;
1483       gchar *debug = NULL;
1484       GstMessage *wmsg;
1485
1486       gst_message_parse_error (message, &err, &debug);
1487       GST_DEBUG_OBJECT (self,
1488           "Got error message from subtitle element %s: %s (%s)",
1489           GST_MESSAGE_SRC_NAME (message), GST_STR_NULL (err->message),
1490           GST_STR_NULL (debug));
1491
1492       wmsg = gst_message_new_warning (src, err, debug);
1493       gst_message_unref (message);
1494       g_error_free (err);
1495       g_free (debug);
1496       message = wmsg;
1497
1498       GST_SUBTITLE_OVERLAY_LOCK (self);
1499       self->subtitle_error = TRUE;
1500
1501       block_subtitle (self);
1502       block_video (self);
1503       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1504     }
1505   }
1506
1507   GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1508 }
1509
1510 static void
1511 gst_subtitle_overlay_get_property (GObject * object, guint prop_id,
1512     GValue * value, GParamSpec * pspec)
1513 {
1514   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1515
1516   switch (prop_id) {
1517     case PROP_SILENT:
1518       g_value_set_boolean (value, self->silent);
1519       break;
1520     case PROP_FONT_DESC:
1521       GST_SUBTITLE_OVERLAY_LOCK (self);
1522       g_value_set_string (value, self->font_desc);
1523       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1524       break;
1525     case PROP_SUBTITLE_ENCODING:
1526       GST_SUBTITLE_OVERLAY_LOCK (self);
1527       g_value_set_string (value, self->encoding);
1528       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1529       break;
1530     default:
1531       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1532       break;
1533   }
1534 }
1535
1536 static void
1537 gst_subtitle_overlay_set_property (GObject * object, guint prop_id,
1538     const GValue * value, GParamSpec * pspec)
1539 {
1540   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1541
1542   switch (prop_id) {
1543     case PROP_SILENT:
1544       GST_SUBTITLE_OVERLAY_LOCK (self);
1545       self->silent = g_value_get_boolean (value);
1546       if (self->silent_property) {
1547         gboolean silent = self->silent;
1548
1549         if (self->silent_property_invert)
1550           silent = !silent;
1551
1552         if (self->overlay)
1553           g_object_set (self->overlay, self->silent_property, silent, NULL);
1554         else if (self->renderer)
1555           g_object_set (self->renderer, self->silent_property, silent, NULL);
1556       } else {
1557         block_subtitle (self);
1558         block_video (self);
1559       }
1560       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1561       break;
1562     case PROP_FONT_DESC:
1563       GST_SUBTITLE_OVERLAY_LOCK (self);
1564       g_free (self->font_desc);
1565       self->font_desc = g_value_dup_string (value);
1566       if (self->overlay
1567           && _has_property_with_type (G_OBJECT (self->overlay), "font-desc",
1568               G_TYPE_STRING))
1569         g_object_set (self->overlay, "font-desc", self->font_desc, NULL);
1570       else if (self->renderer
1571           && _has_property_with_type (G_OBJECT (self->renderer), "font-desc",
1572               G_TYPE_STRING))
1573         g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1574       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1575       break;
1576     case PROP_SUBTITLE_ENCODING:
1577       GST_SUBTITLE_OVERLAY_LOCK (self);
1578       g_free (self->encoding);
1579       self->encoding = g_value_dup_string (value);
1580       if (self->renderer
1581           && _has_property_with_type (G_OBJECT (self->renderer),
1582               "subtitle-encoding", G_TYPE_STRING))
1583         g_object_set (self->renderer, "subtitle-encoding", self->encoding,
1584             NULL);
1585       if (self->parser
1586           && _has_property_with_type (G_OBJECT (self->parser),
1587               "subtitle-encoding", G_TYPE_STRING))
1588         g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
1589       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1590       break;
1591     default:
1592       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1593       break;
1594   }
1595 }
1596
1597 static void
1598 gst_subtitle_overlay_class_init (GstSubtitleOverlayClass * klass)
1599 {
1600   GObjectClass *gobject_class = (GObjectClass *) klass;
1601   GstElementClass *element_class = (GstElementClass *) klass;
1602   GstBinClass *bin_class = (GstBinClass *) klass;
1603
1604   gobject_class->finalize = gst_subtitle_overlay_finalize;
1605   gobject_class->set_property = gst_subtitle_overlay_set_property;
1606   gobject_class->get_property = gst_subtitle_overlay_get_property;
1607
1608   g_object_class_install_property (gobject_class, PROP_SILENT,
1609       g_param_spec_boolean ("silent",
1610           "Silent",
1611           "Whether to show subtitles", FALSE,
1612           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1613
1614   g_object_class_install_property (gobject_class, PROP_FONT_DESC,
1615       g_param_spec_string ("font-desc",
1616           "Subtitle font description",
1617           "Pango font description of font "
1618           "to be used for subtitle rendering", NULL,
1619           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1620
1621   g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
1622       g_param_spec_string ("subtitle-encoding", "subtitle encoding",
1623           "Encoding to assume if input subtitles are not in UTF-8 encoding. "
1624           "If not set, the GST_SUBTITLE_ENCODING environment variable will "
1625           "be checked for an encoding to use. If that is not set either, "
1626           "ISO-8859-15 will be assumed.", NULL,
1627           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1628
1629   gst_element_class_add_pad_template (element_class,
1630       gst_static_pad_template_get (&srctemplate));
1631
1632   gst_element_class_add_pad_template (element_class,
1633       gst_static_pad_template_get (&video_sinktemplate));
1634   gst_element_class_add_pad_template (element_class,
1635       gst_static_pad_template_get (&subtitle_sinktemplate));
1636
1637   gst_element_class_set_static_metadata (element_class, "Subtitle Overlay",
1638       "Video/Overlay/Subtitle",
1639       "Overlays a video stream with subtitles",
1640       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1641
1642   element_class->change_state =
1643       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_change_state);
1644
1645   bin_class->handle_message =
1646       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_handle_message);
1647 }
1648
1649 static GstFlowReturn
1650 gst_subtitle_overlay_src_proxy_chain (GstPad * proxypad, GstObject * parent,
1651     GstBuffer * buffer)
1652 {
1653   GstPad *ghostpad;
1654   GstSubtitleOverlay *self;
1655   GstFlowReturn ret;
1656
1657   ghostpad = GST_PAD_CAST (parent);
1658   if (G_UNLIKELY (!ghostpad)) {
1659     gst_buffer_unref (buffer);
1660     return GST_FLOW_ERROR;
1661   }
1662   self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1663   if (G_UNLIKELY (!self || self->srcpad != ghostpad)) {
1664     gst_buffer_unref (buffer);
1665     gst_object_unref (ghostpad);
1666     return GST_FLOW_ERROR;
1667   }
1668
1669   ret = gst_proxy_pad_chain_default (proxypad, parent, buffer);
1670
1671   if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1672     GST_ERROR_OBJECT (self, "Downstream chain error: %s",
1673         gst_flow_get_name (ret));
1674     self->downstream_chain_error = TRUE;
1675   }
1676
1677   gst_object_unref (self);
1678
1679   return ret;
1680 }
1681
1682 static gboolean
1683 gst_subtitle_overlay_src_proxy_event (GstPad * proxypad, GstObject * parent,
1684     GstEvent * event)
1685 {
1686   GstPad *ghostpad = NULL;
1687   GstSubtitleOverlay *self = NULL;
1688   gboolean ret = FALSE;
1689   const GstStructure *s;
1690
1691   ghostpad = GST_PAD_CAST (parent);
1692   if (G_UNLIKELY (!ghostpad))
1693     goto out;
1694   self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1695   if (G_UNLIKELY (!self || self->srcpad != ghostpad))
1696     goto out;
1697
1698   s = gst_event_get_structure (event);
1699   if (s && gst_structure_id_has_field (s, _subtitle_overlay_event_marker_id)) {
1700     GST_DEBUG_OBJECT (ghostpad,
1701         "Dropping event with marker: %" GST_PTR_FORMAT,
1702         gst_event_get_structure (event));
1703     gst_event_unref (event);
1704     event = NULL;
1705     ret = TRUE;
1706   } else {
1707     ret = gst_proxy_pad_event_default (proxypad, parent, event);
1708     event = NULL;
1709   }
1710
1711 out:
1712   if (event)
1713     gst_event_unref (event);
1714   if (self)
1715     gst_object_unref (self);
1716
1717   return ret;
1718 }
1719
1720 static gboolean
1721 gst_subtitle_overlay_video_sink_setcaps (GstSubtitleOverlay * self,
1722     GstCaps * caps)
1723 {
1724   GstPad *target;
1725   gboolean ret = TRUE;
1726   GstVideoInfo info;
1727
1728   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1729
1730   if (!gst_video_info_from_caps (&info, caps)) {
1731     GST_ERROR_OBJECT (self, "Failed to parse caps");
1732     ret = FALSE;
1733     GST_SUBTITLE_OVERLAY_UNLOCK (self);
1734     goto out;
1735   }
1736
1737   target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->video_sinkpad));
1738
1739   GST_SUBTITLE_OVERLAY_LOCK (self);
1740
1741   if (!target || !gst_pad_query_accept_caps (target, caps)) {
1742     GST_DEBUG_OBJECT (target, "Target did not accept caps -- reconfiguring");
1743
1744     block_subtitle (self);
1745     block_video (self);
1746   }
1747
1748   if (self->fps_n != info.fps_n || self->fps_d != info.fps_d) {
1749     GST_DEBUG_OBJECT (self, "New video fps: %d/%d", info.fps_n, info.fps_d);
1750     self->fps_n = info.fps_n;
1751     self->fps_d = info.fps_d;
1752     gst_subtitle_overlay_set_fps (self);
1753   }
1754   GST_SUBTITLE_OVERLAY_UNLOCK (self);
1755
1756   if (target)
1757     gst_object_unref (target);
1758
1759 out:
1760
1761   return ret;
1762 }
1763
1764 static gboolean
1765 gst_subtitle_overlay_video_sink_event (GstPad * pad, GstObject * parent,
1766     GstEvent * event)
1767 {
1768   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1769   gboolean ret;
1770
1771   switch (GST_EVENT_TYPE (event)) {
1772     case GST_EVENT_FLUSH_STOP:
1773     {
1774       GST_DEBUG_OBJECT (pad,
1775           "Resetting video segment because of flush-stop event");
1776       gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1777       self->fps_n = self->fps_d = 0;
1778       break;
1779     }
1780     case GST_EVENT_CAPS:
1781     {
1782       GstCaps *caps;
1783
1784       gst_event_parse_caps (event, &caps);
1785       ret = gst_subtitle_overlay_video_sink_setcaps (self, caps);
1786       if (!ret)
1787         goto done;
1788       break;
1789     }
1790     default:
1791       break;
1792   }
1793
1794   ret = gst_proxy_pad_event_default (pad, parent, gst_event_ref (event));
1795
1796   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1797     GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
1798     gst_event_copy_segment (event, &self->video_segment);
1799
1800     if (self->video_segment.format != GST_FORMAT_TIME)
1801       goto invalid_format;
1802   }
1803
1804 done:
1805   gst_event_unref (event);
1806
1807   return ret;
1808
1809   /* ERRORS */
1810 invalid_format:
1811   {
1812     GST_ERROR_OBJECT (pad, "Segment event in non-time format: %s",
1813         gst_format_get_name (self->video_segment.format));
1814     ret = FALSE;
1815     goto done;
1816   }
1817 }
1818
1819 static GstFlowReturn
1820 gst_subtitle_overlay_video_sink_chain (GstPad * pad, GstObject * parent,
1821     GstBuffer * buffer)
1822 {
1823   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1824   GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1825
1826   if (G_UNLIKELY (self->downstream_chain_error) || self->passthrough_identity) {
1827     return ret;
1828   } else if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1829     GST_DEBUG_OBJECT (self, "Subtitle renderer produced chain error: %s",
1830         gst_flow_get_name (ret));
1831     GST_SUBTITLE_OVERLAY_LOCK (self);
1832     self->subtitle_error = TRUE;
1833     block_subtitle (self);
1834     block_video (self);
1835     GST_SUBTITLE_OVERLAY_UNLOCK (self);
1836
1837     return GST_FLOW_OK;
1838   }
1839
1840   return ret;
1841 }
1842
1843 static GstFlowReturn
1844 gst_subtitle_overlay_subtitle_sink_chain (GstPad * pad, GstObject * parent,
1845     GstBuffer * buffer)
1846 {
1847   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1848
1849   if (self->subtitle_error) {
1850     gst_buffer_unref (buffer);
1851     return GST_FLOW_OK;
1852   } else {
1853     GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1854
1855     if (IS_SUBTITLE_CHAIN_IGNORE_ERROR (ret)) {
1856       GST_DEBUG_OBJECT (self, "Subtitle chain error: %s",
1857           gst_flow_get_name (ret));
1858       GST_SUBTITLE_OVERLAY_LOCK (self);
1859       self->subtitle_error = TRUE;
1860       block_subtitle (self);
1861       block_video (self);
1862       GST_SUBTITLE_OVERLAY_UNLOCK (self);
1863
1864       return GST_FLOW_OK;
1865     }
1866
1867     return ret;
1868   }
1869 }
1870
1871 static GstCaps *
1872 gst_subtitle_overlay_subtitle_sink_getcaps (GstPad * pad, GstCaps * filter)
1873 {
1874   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1875   GstCaps *ret;
1876
1877   g_mutex_lock (self->factories_lock);
1878   if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self)))
1879     ret = gst_caps_new_empty ();
1880   else if (filter)
1881     ret =
1882         gst_caps_intersect_full (filter, self->factory_caps,
1883         GST_CAPS_INTERSECT_FIRST);
1884   else
1885     ret = gst_caps_ref (self->factory_caps);
1886   g_mutex_unlock (self->factories_lock);
1887
1888   GST_DEBUG_OBJECT (pad, "Returning subtitle caps %" GST_PTR_FORMAT, ret);
1889
1890   gst_object_unref (self);
1891
1892   return ret;
1893 }
1894
1895 static gboolean
1896 gst_subtitle_overlay_subtitle_sink_setcaps (GstSubtitleOverlay * self,
1897     GstCaps * caps)
1898 {
1899   gboolean ret = TRUE;
1900   GstPad *target = NULL;;
1901
1902   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1903
1904   target =
1905       gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1906
1907   GST_SUBTITLE_OVERLAY_LOCK (self);
1908   gst_caps_replace (&self->subcaps, caps);
1909
1910   if (target && gst_pad_query_accept_caps (target, caps)) {
1911     GST_DEBUG_OBJECT (self, "Target accepts caps");
1912     GST_SUBTITLE_OVERLAY_UNLOCK (self);
1913     goto out;
1914   }
1915
1916   GST_DEBUG_OBJECT (self, "Target did not accept caps");
1917
1918   self->subtitle_error = FALSE;
1919   block_subtitle (self);
1920   block_video (self);
1921   GST_SUBTITLE_OVERLAY_UNLOCK (self);
1922
1923 out:
1924   if (target)
1925     gst_object_unref (target);
1926
1927   return ret;
1928 }
1929
1930 static GstPadLinkReturn
1931 gst_subtitle_overlay_subtitle_sink_link (GstPad * pad, GstPad * peer)
1932 {
1933   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1934   GstPadLinkReturn ret;
1935   GstCaps *caps;
1936
1937   GST_DEBUG_OBJECT (pad, "Linking pad to peer %" GST_PTR_FORMAT, peer);
1938
1939   caps = gst_pad_get_current_caps (peer);
1940   if (!caps) {
1941     caps = gst_pad_query_caps (peer, NULL);
1942     if (!gst_caps_is_fixed (caps)) {
1943       gst_caps_unref (caps);
1944       caps = NULL;
1945     }
1946   }
1947
1948   if (caps) {
1949     GST_SUBTITLE_OVERLAY_LOCK (self);
1950     GST_DEBUG_OBJECT (pad, "Have fixed peer caps: %" GST_PTR_FORMAT, caps);
1951     gst_caps_replace (&self->subcaps, caps);
1952
1953     self->subtitle_error = FALSE;
1954
1955     block_subtitle (self);
1956     block_video (self);
1957     GST_SUBTITLE_OVERLAY_UNLOCK (self);
1958     gst_caps_unref (caps);
1959   }
1960
1961   ret = gst_ghost_pad_link_default (pad, peer);
1962
1963   gst_object_unref (self);
1964   return ret;
1965 }
1966
1967 static void
1968 gst_subtitle_overlay_subtitle_sink_unlink (GstPad * pad)
1969 {
1970   GstSubtitleOverlay *self =
1971       GST_SUBTITLE_OVERLAY (gst_object_ref (GST_PAD_PARENT (pad)));
1972
1973   /* FIXME: Can't use gst_pad_get_parent() here because this is called with
1974    * the object lock from state changes
1975    */
1976
1977   GST_DEBUG_OBJECT (pad, "Pad unlinking");
1978   gst_caps_replace (&self->subcaps, NULL);
1979
1980   gst_ghost_pad_unlink_default (pad);
1981
1982   GST_SUBTITLE_OVERLAY_LOCK (self);
1983   self->subtitle_error = FALSE;
1984
1985   block_subtitle (self);
1986   block_video (self);
1987   GST_SUBTITLE_OVERLAY_UNLOCK (self);
1988
1989   gst_object_unref (self);
1990 }
1991
1992 static gboolean
1993 gst_subtitle_overlay_subtitle_sink_event (GstPad * pad, GstObject * parent,
1994     GstEvent * event)
1995 {
1996   GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1997   gboolean ret;
1998
1999   if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB &&
2000       gst_event_has_name (event, "subtitleoverlay-flush-subtitle")) {
2001     GST_DEBUG_OBJECT (pad, "Custom subtitle flush event");
2002     GST_SUBTITLE_OVERLAY_LOCK (self);
2003     self->subtitle_flush = TRUE;
2004     self->subtitle_error = FALSE;
2005     block_subtitle (self);
2006     block_video (self);
2007     GST_SUBTITLE_OVERLAY_UNLOCK (self);
2008
2009     gst_event_unref (event);
2010     event = NULL;
2011     ret = TRUE;
2012     goto out;
2013   }
2014
2015   switch (GST_EVENT_TYPE (event)) {
2016     case GST_EVENT_CAPS:
2017     {
2018       GstCaps *caps;
2019
2020       gst_event_parse_caps (event, &caps);
2021       ret = gst_subtitle_overlay_subtitle_sink_setcaps (self, caps);
2022       if (!ret)
2023         goto out;
2024       break;
2025     }
2026     case GST_EVENT_FLUSH_STOP:
2027       GST_DEBUG_OBJECT (pad,
2028           "Resetting subtitle segment because of flush-stop");
2029       gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
2030       /* fall through */
2031     case GST_EVENT_FLUSH_START:
2032     case GST_EVENT_SEGMENT:
2033     case GST_EVENT_EOS:
2034     {
2035       GstStructure *structure;
2036
2037       /* Add our event marker to make sure no events from here go ever outside
2038        * the element, they're only interesting for our internal elements */
2039       event = GST_EVENT_CAST (gst_event_make_writable (event));
2040       structure = gst_event_writable_structure (event);
2041
2042       gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
2043           G_TYPE_BOOLEAN, TRUE, NULL);
2044       break;
2045     }
2046     default:
2047       break;
2048   }
2049
2050   ret = gst_proxy_pad_event_default (pad, parent, gst_event_ref (event));
2051
2052   if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2053     GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
2054     gst_event_copy_segment (event, &self->subtitle_segment);
2055     GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
2056         &self->subtitle_segment);
2057   }
2058   gst_event_unref (event);
2059
2060 out:
2061   return ret;
2062 }
2063
2064 static gboolean
2065 gst_subtitle_overlay_subtitle_sink_query (GstPad * pad, GstObject * parent,
2066     GstQuery * query)
2067 {
2068   gboolean ret;
2069
2070   switch (GST_QUERY_TYPE (query)) {
2071     case GST_QUERY_ACCEPT_CAPS:
2072     {
2073       GstCaps *caps, *othercaps;
2074
2075       gst_query_parse_accept_caps (query, &caps);
2076       othercaps = gst_subtitle_overlay_subtitle_sink_getcaps (pad, NULL);
2077       ret = gst_caps_is_subset (caps, othercaps);
2078       gst_caps_unref (othercaps);
2079       gst_query_set_accept_caps_result (query, ret);
2080       ret = TRUE;
2081       break;
2082     }
2083     case GST_QUERY_CAPS:
2084     {
2085       GstCaps *filter, *caps;
2086
2087       gst_query_parse_caps (query, &filter);
2088       caps = gst_subtitle_overlay_subtitle_sink_getcaps (pad, filter);
2089       gst_query_set_caps_result (query, caps);
2090       gst_caps_unref (caps);
2091       ret = TRUE;
2092       break;
2093     }
2094     default:
2095       ret = gst_pad_query_default (pad, parent, query);
2096       break;
2097   }
2098
2099   return ret;
2100 }
2101
2102 static void
2103 gst_subtitle_overlay_init (GstSubtitleOverlay * self)
2104 {
2105   GstPadTemplate *templ;
2106   GstPad *proxypad = NULL;
2107
2108   self->lock = g_mutex_new ();
2109   self->factories_lock = g_mutex_new ();
2110
2111   templ = gst_static_pad_template_get (&srctemplate);
2112   self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
2113   gst_object_unref (templ);
2114
2115   proxypad =
2116       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->srcpad)));
2117   gst_pad_set_event_function (proxypad,
2118       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_event));
2119   gst_pad_set_chain_function (proxypad,
2120       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_chain));
2121   gst_object_unref (proxypad);
2122
2123   gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
2124
2125   templ = gst_static_pad_template_get (&video_sinktemplate);
2126   self->video_sinkpad =
2127       gst_ghost_pad_new_no_target_from_template ("video_sink", templ);
2128   gst_object_unref (templ);
2129   gst_pad_set_event_function (self->video_sinkpad,
2130       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_event));
2131   gst_pad_set_chain_function (self->video_sinkpad,
2132       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_chain));
2133
2134   proxypad =
2135       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2136           (self->video_sinkpad)));
2137   self->video_block_pad = proxypad;
2138   gst_object_unref (proxypad);
2139   gst_element_add_pad (GST_ELEMENT_CAST (self), self->video_sinkpad);
2140
2141   templ = gst_static_pad_template_get (&subtitle_sinktemplate);
2142   self->subtitle_sinkpad =
2143       gst_ghost_pad_new_no_target_from_template ("subtitle_sink", templ);
2144   gst_object_unref (templ);
2145   gst_pad_set_link_function (self->subtitle_sinkpad,
2146       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_link));
2147   gst_pad_set_unlink_function (self->subtitle_sinkpad,
2148       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_unlink));
2149   gst_pad_set_event_function (self->subtitle_sinkpad,
2150       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_event));
2151   gst_pad_set_query_function (self->subtitle_sinkpad,
2152       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_query));
2153   gst_pad_set_chain_function (self->subtitle_sinkpad,
2154       GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_chain));
2155
2156   proxypad =
2157       GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2158           (self->subtitle_sinkpad)));
2159   self->subtitle_block_pad = proxypad;
2160   gst_object_unref (proxypad);
2161
2162   gst_element_add_pad (GST_ELEMENT_CAST (self), self->subtitle_sinkpad);
2163
2164   self->fps_n = 0;
2165   self->fps_d = 0;
2166 }
2167
2168 gboolean
2169 gst_subtitle_overlay_plugin_init (GstPlugin * plugin)
2170 {
2171   GST_DEBUG_CATEGORY_INIT (subtitle_overlay_debug, "subtitleoverlay", 0,
2172       "Subtitle Overlay");
2173
2174   _subtitle_overlay_event_marker_id =
2175       g_quark_from_static_string ("gst-subtitle-overlay-event-marker");
2176
2177   return gst_element_register (plugin, "subtitleoverlay", GST_RANK_NONE,
2178       GST_TYPE_SUBTITLE_OVERLAY);
2179 }