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