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