2 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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.
21 * SECTION:element-subtitleoverlay
23 * #GstBin that auto-magically overlays a video stream with subtitles by
24 * autoplugging the required elements.
26 * It supports raw, timestamped text, different textual subtitle formats and
27 * DVD subpicture subtitles.
30 * <title>Examples</title>
32 * gst-launch -v filesrc location=test.mkv ! matroskademux name=demux ! "video/x-h264" ! queue2 ! decodebin ! 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.
41 #include "gstsubtitleoverlay.h"
43 #include <gst/gstfilter.h>
44 #include <gst/pbutils/missing-plugins.h>
45 #include <gst/video/video.h>
48 GST_DEBUG_CATEGORY_STATIC (subtitle_overlay_debug);
49 #define GST_CAT_DEFAULT subtitle_overlay_debug
51 #define IS_SUBTITLE_CHAIN_IGNORE_ERROR(flow) \
52 G_UNLIKELY (flow == GST_FLOW_ERROR || flow == GST_FLOW_NOT_NEGOTIATED)
54 #define IS_VIDEO_CHAIN_IGNORE_ERROR(flow) \
55 G_UNLIKELY (flow == GST_FLOW_ERROR)
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 static GstStaticPadTemplate video_sinktemplate =
63 GST_STATIC_PAD_TEMPLATE ("video_sink",
68 static GstStaticPadTemplate subtitle_sinktemplate =
69 GST_STATIC_PAD_TEMPLATE ("subtitle_sink",
79 PROP_SUBTITLE_ENCODING
82 #define gst_subtitle_overlay_parent_class parent_class
83 G_DEFINE_TYPE (GstSubtitleOverlay, gst_subtitle_overlay, GST_TYPE_BIN);
85 static GQuark _subtitle_overlay_event_marker_id = 0;
88 do_async_start (GstSubtitleOverlay * self)
90 if (!self->do_async) {
91 GstMessage *msg = gst_message_new_async_start (GST_OBJECT_CAST (self));
93 GST_DEBUG_OBJECT (self, "Posting async-start");
94 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
95 self->do_async = TRUE;
100 do_async_done (GstSubtitleOverlay * self)
102 if (self->do_async) {
104 gst_message_new_async_done (GST_OBJECT_CAST (self), FALSE);
106 GST_DEBUG_OBJECT (self, "Posting async-done");
107 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
108 self->do_async = FALSE;
112 static GstProbeReturn
113 _pad_blocked_cb (GstPad * pad, GstProbeType type, gpointer type_data,
117 block_video (GstSubtitleOverlay * self)
119 if (self->video_block_id != 0)
122 if (self->video_block_pad) {
123 self->video_block_id =
124 gst_pad_add_probe (self->video_block_pad, GST_PROBE_TYPE_BLOCK,
125 _pad_blocked_cb, gst_object_ref (self),
126 (GDestroyNotify) gst_object_unref);
131 unblock_video (GstSubtitleOverlay * self)
133 if (self->video_block_id) {
134 gst_pad_remove_probe (self->video_block_pad, self->video_block_id);
135 self->video_sink_blocked = FALSE;
136 self->video_block_id = 0;
141 block_subtitle (GstSubtitleOverlay * self)
143 if (self->subtitle_block_id != 0)
146 if (self->subtitle_block_pad) {
147 self->subtitle_block_id =
148 gst_pad_add_probe (self->subtitle_block_pad, GST_PROBE_TYPE_BLOCK,
149 _pad_blocked_cb, gst_object_ref (self),
150 (GDestroyNotify) gst_object_unref);
155 unblock_subtitle (GstSubtitleOverlay * self)
157 if (self->subtitle_block_id) {
158 gst_pad_remove_probe (self->subtitle_block_pad, self->subtitle_block_id);
159 self->subtitle_sink_blocked = FALSE;
160 self->subtitle_block_id = 0;
165 gst_subtitle_overlay_finalize (GObject * object)
167 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (object);
170 g_mutex_free (self->lock);
174 if (self->factories_lock) {
175 g_mutex_free (self->factories_lock);
176 self->factories_lock = NULL;
180 gst_plugin_feature_list_free (self->factories);
181 self->factories = NULL;
182 gst_caps_replace (&self->factory_caps, NULL);
184 if (self->font_desc) {
185 g_free (self->font_desc);
186 self->font_desc = NULL;
189 if (self->encoding) {
190 g_free (self->encoding);
191 self->encoding = NULL;
194 G_OBJECT_CLASS (parent_class)->finalize (object);
198 _is_renderer (GstElementFactory * factory)
200 const gchar *klass, *name;
202 klass = gst_element_factory_get_klass (factory);
203 name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
205 if (strstr (klass, "Overlay/Subtitle") != NULL ||
206 strstr (klass, "Overlay/SubPicture") != NULL)
208 if (strcmp (name, "textoverlay") == 0)
214 _is_parser (GstElementFactory * factory)
218 klass = gst_element_factory_get_klass (factory);
220 if (strstr (klass, "Parser/Subtitle") != NULL)
225 static const gchar *_sub_pad_names[] = { "subpicture", "subpicture_sink",
227 "subtitle_sink", "subtitle"
231 _get_sub_caps (GstElementFactory * factory)
233 const GList *templates;
235 gboolean is_parser = _is_parser (factory);
237 templates = gst_element_factory_get_static_pad_templates (factory);
238 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
239 GstStaticPadTemplate *templ = walk->data;
241 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
242 gboolean found = FALSE;
249 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
250 if (strcmp (templ->name_template, _sub_pad_names[i]) == 0) {
257 return gst_static_caps_get (&templ->static_caps);
264 _factory_filter (GstPluginFeature * feature, GstCaps ** subcaps)
266 GstElementFactory *factory;
269 const GList *templates;
271 gboolean is_renderer;
272 GstCaps *templ_caps = NULL;
273 gboolean have_video_sink = FALSE;
275 /* we only care about element factories */
276 if (!GST_IS_ELEMENT_FACTORY (feature))
279 factory = GST_ELEMENT_FACTORY_CAST (feature);
281 /* only select elements with autoplugging rank or textoverlay */
282 name = gst_plugin_feature_get_name (feature);
283 rank = gst_plugin_feature_get_rank (feature);
284 if (strcmp ("textoverlay", name) != 0 && rank < GST_RANK_MARGINAL)
287 /* Check if it's a renderer or a parser */
288 if (_is_renderer (factory)) {
290 } else if (_is_parser (factory)) {
296 /* Check if there's a video sink in case of a renderer */
298 templates = gst_element_factory_get_static_pad_templates (factory);
299 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
300 GstStaticPadTemplate *templ = walk->data;
302 /* we only care about the always-sink templates */
303 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
304 if (strcmp (templ->name_template, "video") == 0 ||
305 strcmp (templ->name_template, "video_sink") == 0) {
306 have_video_sink = TRUE;
311 templ_caps = _get_sub_caps (factory);
313 if (is_renderer && have_video_sink && templ_caps) {
316 GST_DEBUG ("Found renderer element %s (%s) with caps %" GST_PTR_FORMAT,
317 gst_element_factory_get_longname (factory),
318 gst_plugin_feature_get_name (feature), templ_caps);
319 tmp = gst_caps_union (*subcaps, templ_caps);
320 gst_caps_unref (templ_caps);
321 gst_caps_replace (subcaps, tmp);
322 gst_caps_unref (tmp);
324 } else if (!is_renderer && !have_video_sink && templ_caps) {
327 GST_DEBUG ("Found parser element %s (%s) with caps %" GST_PTR_FORMAT,
328 gst_element_factory_get_longname (factory),
329 gst_plugin_feature_get_name (feature), templ_caps);
330 tmp = gst_caps_union (*subcaps, templ_caps);
331 gst_caps_unref (templ_caps);
332 gst_caps_replace (subcaps, tmp);
333 gst_caps_unref (tmp);
337 gst_caps_unref (templ_caps);
342 /* Call with factories_lock! */
344 gst_subtitle_overlay_update_factory_list (GstSubtitleOverlay * self)
347 || self->factories_cookie !=
348 gst_default_registry_get_feature_list_cookie ()) {
352 subcaps = gst_caps_new_empty ();
354 factories = gst_default_registry_feature_filter (
355 (GstPluginFeatureFilter) _factory_filter, FALSE, &subcaps);
356 GST_DEBUG_OBJECT (self, "Created factory caps: %" GST_PTR_FORMAT, subcaps);
357 gst_caps_replace (&self->factory_caps, subcaps);
358 gst_caps_unref (subcaps);
360 gst_plugin_feature_list_free (self->factories);
361 self->factories = factories;
362 self->factories_cookie = gst_default_registry_get_feature_list_cookie ();
365 return (self->factories != NULL);
368 G_LOCK_DEFINE_STATIC (_factory_caps);
369 static GstCaps *_factory_caps = NULL;
370 static guint32 _factory_caps_cookie = 0;
373 gst_subtitle_overlay_create_factory_caps (void)
376 GstCaps *subcaps = NULL;
378 G_LOCK (_factory_caps);
380 || _factory_caps_cookie !=
381 gst_default_registry_get_feature_list_cookie ()) {
383 gst_caps_unref (_factory_caps);
384 _factory_caps = gst_caps_new_empty ();
386 factories = gst_default_registry_feature_filter (
387 (GstPluginFeatureFilter) _factory_filter, FALSE, &_factory_caps);
388 GST_DEBUG ("Created factory caps: %" GST_PTR_FORMAT, _factory_caps);
389 gst_plugin_feature_list_free (factories);
390 _factory_caps_cookie = gst_default_registry_get_feature_list_cookie ();
392 subcaps = gst_caps_ref (_factory_caps);
393 G_UNLOCK (_factory_caps);
399 _filter_factories_for_caps (GstElementFactory * factory, const GstCaps * caps)
401 GstCaps *fcaps = _get_sub_caps (factory);
402 gboolean ret = (fcaps) ? gst_caps_can_intersect (fcaps, caps) : FALSE;
405 gst_caps_unref (fcaps);
408 gst_object_ref (factory);
413 _sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
416 const gchar *rname1, *rname2;
418 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
422 /* If the ranks are the same sort by name to get deterministic results */
423 rname1 = gst_plugin_feature_get_name (f1);
424 rname2 = gst_plugin_feature_get_name (f2);
426 diff = strcmp (rname1, rname2);
432 _get_sub_pad (GstElement * element)
437 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
438 pad = gst_element_get_static_pad (element, _sub_pad_names[i]);
446 _get_video_pad (GstElement * element)
448 static const gchar *pad_names[] = { "video", "video_sink" };
452 for (i = 0; i < G_N_ELEMENTS (pad_names); i++) {
453 pad = gst_element_get_static_pad (element, pad_names[i]);
461 _create_element (GstSubtitleOverlay * self, GstElement ** element,
462 const gchar * factory_name, GstElementFactory * factory,
463 const gchar * element_name, gboolean mandatory)
467 g_assert (!factory || !factory_name);
470 elt = gst_element_factory_make (factory_name, element_name);
473 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
474 elt = gst_element_factory_create (factory, element_name);
477 if (G_UNLIKELY (!elt)) {
482 gst_missing_element_message_new (GST_ELEMENT_CAST (self),
484 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
487 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
488 ("no '%s' plugin found", factory_name));
490 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
491 ("no '%s' plugin found", factory_name));
494 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
495 ("can't instantiate '%s'", factory_name));
497 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
498 ("can't instantiate '%s'", factory_name));
505 if (G_UNLIKELY (gst_element_set_state (elt,
506 GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS)) {
507 gst_object_unref (elt);
509 GST_ELEMENT_ERROR (self, CORE, STATE_CHANGE, (NULL),
510 ("failed to set '%s' to READY", factory_name));
512 GST_WARNING_OBJECT (self, "Failed to set '%s' to READY", factory_name);
517 if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (self), gst_object_ref (elt)))) {
518 gst_element_set_state (elt, GST_STATE_NULL);
519 gst_object_unref (elt);
521 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
522 ("failed to add '%s' to subtitleoverlay", factory_name));
524 GST_WARNING_OBJECT (self, "Failed to add '%s' to subtitleoverlay",
530 gst_element_sync_state_with_parent (elt);
536 _remove_element (GstSubtitleOverlay * self, GstElement ** element)
539 gst_bin_remove (GST_BIN_CAST (self), *element);
540 gst_element_set_state (*element, GST_STATE_NULL);
541 gst_object_unref (*element);
547 _generate_update_segment_event (GstSegment * segment, GstEvent ** event1)
550 GstStructure *structure;
552 event = gst_event_new_segment (segment);
553 structure = gst_event_writable_structure (event);
554 gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
555 G_TYPE_BOOLEAN, TRUE, NULL);
560 _setup_passthrough (GstSubtitleOverlay * self)
563 GstElement *identity;
565 GST_DEBUG_OBJECT (self, "Doing video passthrough");
567 if (self->passthrough_identity) {
568 GST_DEBUG_OBJECT (self, "Already in passthrough mode");
572 /* Unlink & destroy everything */
573 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
574 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
575 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad), NULL);
576 self->silent_property = NULL;
577 _remove_element (self, &self->post_colorspace);
578 _remove_element (self, &self->overlay);
579 _remove_element (self, &self->parser);
580 _remove_element (self, &self->renderer);
581 _remove_element (self, &self->pre_colorspace);
582 _remove_element (self, &self->passthrough_identity);
584 if (G_UNLIKELY (!_create_element (self, &self->passthrough_identity,
585 "identity", NULL, "passthrough-identity", TRUE))) {
589 identity = self->passthrough_identity;
590 g_object_set (G_OBJECT (identity), "silent", TRUE, "signal-handoffs", FALSE,
593 /* Set src ghostpad target */
594 src = gst_element_get_static_pad (self->passthrough_identity, "src");
595 if (G_UNLIKELY (!src)) {
596 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
597 ("Failed to get srcpad from identity"));
601 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
603 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
604 ("Failed to set srcpad target"));
605 gst_object_unref (src);
608 gst_object_unref (src);
610 sink = gst_element_get_static_pad (self->passthrough_identity, "sink");
611 if (G_UNLIKELY (!sink)) {
612 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
613 ("Failed to get sinkpad from identity"));
617 /* Send segment to the identity. This is dropped because identity
618 * is not linked downstream yet */
619 if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
622 _generate_update_segment_event (&self->video_segment, &event1);
623 GST_DEBUG_OBJECT (self,
624 "Pushing video segment event: %" GST_PTR_FORMAT, event1);
625 gst_pad_send_event (sink, event1);
628 /* Link sink ghostpads to identity */
629 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
630 (self->video_sinkpad), sink))) {
631 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
632 ("Failed to set video sinkpad target"));
633 gst_object_unref (sink);
636 gst_object_unref (sink);
638 GST_DEBUG_OBJECT (self, "Video passthrough setup successfully");
642 unblock_video (self);
643 unblock_subtitle (self);
648 /* Must be called with subtitleoverlay lock! */
650 gst_subtitle_overlay_set_fps (GstSubtitleOverlay * self)
652 GObjectClass *gobject_class;
655 if (!self->parser || self->fps_d == 0)
658 gobject_class = G_OBJECT_GET_CLASS (self->parser);
659 pspec = g_object_class_find_property (gobject_class, "video-fps");
660 if (!pspec || pspec->value_type != GST_TYPE_FRACTION)
663 GST_DEBUG_OBJECT (self, "Updating video-fps property in parser");
664 g_object_set (self->parser, "video-fps", self->fps_n, self->fps_d, NULL);
668 _get_silent_property (GstElement * element, gboolean * invert)
677 GObjectClass *gobject_class;
681 gobject_class = G_OBJECT_GET_CLASS (element);
683 for (i = 0; i < G_N_ELEMENTS (properties); i++) {
684 pspec = g_object_class_find_property (gobject_class, properties[i].name);
685 if (pspec && pspec->value_type == G_TYPE_BOOLEAN) {
686 *invert = properties[i].invert;
687 return properties[i].name;
694 _has_subtitle_encoding_property (GstElement * element)
699 g_object_class_find_property (G_OBJECT_GET_CLASS (element),
700 "subtitle-encoding");
701 return (pspec && pspec->value_type == G_TYPE_STRING);
705 _has_font_desc_property (GstElement * element)
710 g_object_class_find_property (G_OBJECT_GET_CLASS (element), "font-desc");
711 return (pspec && pspec->value_type == G_TYPE_STRING);
714 static GstProbeReturn
715 _pad_blocked_cb (GstPad * pad, GstProbeType type, gpointer type_data,
718 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (user_data);
720 GList *l, *factories = NULL;
722 GST_DEBUG_OBJECT (pad, "Pad blocked");
724 GST_SUBTITLE_OVERLAY_LOCK (self);
725 if (pad == self->video_block_pad)
726 self->video_sink_blocked = TRUE;
727 else if (pad == self->subtitle_block_pad)
728 self->subtitle_sink_blocked = TRUE;
730 /* Now either both or the video sink are blocked */
732 /* Get current subtitle caps */
733 subcaps = self->subcaps;
737 peer = gst_pad_get_peer (self->subtitle_sinkpad);
739 subcaps = gst_pad_get_negotiated_caps (peer);
741 subcaps = gst_pad_get_caps (peer, NULL);
742 if (!gst_caps_is_fixed (subcaps)) {
743 gst_caps_unref (subcaps);
747 gst_object_unref (peer);
749 gst_caps_replace (&self->subcaps, subcaps);
751 gst_caps_unref (subcaps);
753 GST_DEBUG_OBJECT (self, "Current subtitle caps: %" GST_PTR_FORMAT, subcaps);
755 /* If there are no subcaps but the subtitle sink is blocked upstream
756 * must behave wrong as there are no fixed caps set for the first
757 * buffer or in-order event */
758 if (G_UNLIKELY (!subcaps && self->subtitle_sink_blocked)) {
759 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION, (NULL),
760 ("Subtitle sink is blocked but we have no subtitle caps"));
764 if (self->subtitle_error || (self->silent && !self->silent_property)) {
765 _setup_passthrough (self);
766 do_async_done (self);
770 /* Now do something with the caps */
771 if (subcaps && !self->subtitle_flush) {
773 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
775 if (target && gst_pad_accept_caps (target, subcaps)) {
776 GST_DEBUG_OBJECT (pad, "Target accepts caps");
778 gst_object_unref (target);
781 unblock_video (self);
782 unblock_subtitle (self);
786 gst_object_unref (target);
790 if (self->subtitle_sink_blocked && !self->video_sink_blocked) {
791 GST_DEBUG_OBJECT (self, "Subtitle sink blocked but video not blocked");
796 self->subtitle_flush = FALSE;
798 /* Find our factories */
799 g_mutex_lock (self->factories_lock);
800 gst_subtitle_overlay_update_factory_list (self);
802 factories = gst_filter_run (self->factories,
803 (GstFilterFunc) _filter_factories_for_caps, FALSE, subcaps);
807 msg = gst_missing_decoder_message_new (GST_ELEMENT_CAST (self), subcaps);
808 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
809 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
810 ("no suitable subtitle plugin found"));
812 self->subtitle_error = TRUE;
815 g_mutex_unlock (self->factories_lock);
818 _setup_passthrough (self);
819 do_async_done (self);
823 /* Now the interesting parts are done: subtitle overlaying! */
825 /* Sort the factories by rank */
826 factories = g_list_sort (factories, (GCompareFunc) _sort_by_ranks);
828 for (l = factories; l; l = l->next) {
829 GstElementFactory *factory = l->data;
830 gboolean is_renderer = _is_renderer (factory);
834 /* Unlink & destroy everything */
836 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
837 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
838 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
840 self->silent_property = NULL;
841 _remove_element (self, &self->post_colorspace);
842 _remove_element (self, &self->overlay);
843 _remove_element (self, &self->parser);
844 _remove_element (self, &self->renderer);
845 _remove_element (self, &self->pre_colorspace);
846 _remove_element (self, &self->passthrough_identity);
848 GST_DEBUG_OBJECT (self, "Trying factory '%s'",
849 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
852 if (G_UNLIKELY ((is_renderer
853 && !_create_element (self, &self->renderer, NULL, factory,
854 "renderer", FALSE)) || (!is_renderer
855 && !_create_element (self, &self->parser, NULL, factory,
859 element = is_renderer ? self->renderer : self->parser;
861 /* If this is a parser, create textoverlay and link video and the parser to it
862 * Else link the renderer to the output colorspace */
867 /* Try to get the latest video framerate */
868 video_peer = gst_pad_get_peer (self->video_sinkpad);
873 video_caps = gst_pad_get_negotiated_caps (video_peer);
875 video_caps = gst_pad_get_caps (video_peer, NULL);
876 if (!gst_caps_is_fixed (video_caps)) {
877 gst_caps_unref (video_caps);
883 && gst_video_parse_caps_framerate (video_caps, &fps_n, &fps_d)) {
884 if (self->fps_n != fps_n || self->fps_d != fps_d) {
885 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
892 gst_caps_unref (video_caps);
893 gst_object_unref (video_peer);
896 if (_has_subtitle_encoding_property (self->parser))
897 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
899 /* Try to set video fps on the parser */
900 gst_subtitle_overlay_set_fps (self);
902 /* First link everything internally */
903 if (G_UNLIKELY (!_create_element (self, &self->overlay, "textoverlay",
904 NULL, "overlay", FALSE))) {
907 overlay = self->overlay;
908 self->silent_property = "silent";
909 self->silent_property_invert = FALSE;
911 /* Set some properties */
912 g_object_set (G_OBJECT (overlay),
913 "halign", "center", "valign", "bottom", "wait-text", FALSE, NULL);
915 g_object_set (G_OBJECT (overlay), "font-desc", self->font_desc, NULL);
917 src = gst_element_get_static_pad (element, "src");
918 if (G_UNLIKELY (!src)) {
922 sink = gst_element_get_static_pad (overlay, "text_sink");
923 if (G_UNLIKELY (!sink)) {
924 GST_WARNING_OBJECT (self, "Can't get text sink from textoverlay");
925 gst_object_unref (src);
929 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
930 GST_WARNING_OBJECT (self, "Can't link parser to textoverlay");
931 gst_object_unref (sink);
932 gst_object_unref (src);
935 gst_object_unref (sink);
936 gst_object_unref (src);
938 if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
939 "ffmpegcolorspace", NULL, "post-colorspace", FALSE))) {
943 src = gst_element_get_static_pad (overlay, "src");
944 if (G_UNLIKELY (!src)) {
945 GST_WARNING_OBJECT (self, "Can't get src pad from overlay");
949 sink = gst_element_get_static_pad (self->post_colorspace, "sink");
950 if (G_UNLIKELY (!sink)) {
951 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
952 gst_object_unref (src);
956 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
957 GST_WARNING_OBJECT (self, "Can't link overlay with ffmpegcolorspace");
958 gst_object_unref (src);
959 gst_object_unref (sink);
962 gst_object_unref (src);
963 gst_object_unref (sink);
965 if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
966 "ffmpegcolorspace", NULL, "pre-colorspace", FALSE))) {
970 sink = gst_element_get_static_pad (overlay, "video_sink");
971 if (G_UNLIKELY (!sink)) {
972 GST_WARNING_OBJECT (self, "Can't get video sink from textoverlay");
976 src = gst_element_get_static_pad (self->pre_colorspace, "src");
977 if (G_UNLIKELY (!src)) {
978 GST_WARNING_OBJECT (self, "Can't get srcpad from ffmpegcolorspace");
979 gst_object_unref (sink);
983 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
984 GST_WARNING_OBJECT (self, "Can't link ffmpegcolorspace to textoverlay");
985 gst_object_unref (src);
986 gst_object_unref (sink);
989 gst_object_unref (src);
990 gst_object_unref (sink);
992 /* Set src ghostpad target */
993 src = gst_element_get_static_pad (self->post_colorspace, "src");
994 if (G_UNLIKELY (!src)) {
995 GST_WARNING_OBJECT (self, "Can't get src pad from ffmpegcolorspace");
999 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1000 (self->srcpad), src))) {
1001 GST_WARNING_OBJECT (self, "Can't set srcpad target");
1002 gst_object_unref (src);
1005 gst_object_unref (src);
1007 /* Send segments to the parser/overlay if necessary. These are not sent
1008 * outside this element because of the proxy pad event function */
1009 if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
1012 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1013 if (G_UNLIKELY (!sink)) {
1014 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
1018 _generate_update_segment_event (&self->video_segment, &event1);
1019 GST_DEBUG_OBJECT (self,
1020 "Pushing video segment event: %" GST_PTR_FORMAT, event1);
1021 gst_pad_send_event (sink, event1);
1023 gst_object_unref (sink);
1026 if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
1029 sink = gst_element_get_static_pad (element, "sink");
1030 if (G_UNLIKELY (!sink)) {
1031 GST_WARNING_OBJECT (self, "Failed to get subpad");
1035 _generate_update_segment_event (&self->subtitle_segment, &event1);
1036 GST_DEBUG_OBJECT (self,
1037 "Pushing subtitle segment event: %" GST_PTR_FORMAT, event1);
1038 gst_pad_send_event (sink, event1);
1040 gst_object_unref (sink);
1043 /* Set the sink ghostpad targets */
1044 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1045 if (G_UNLIKELY (!sink)) {
1046 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
1050 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1051 (self->video_sinkpad), sink))) {
1052 GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
1053 gst_object_unref (sink);
1056 gst_object_unref (sink);
1058 /* Link subtitle identity to subtitle pad of our element */
1059 sink = gst_element_get_static_pad (element, "sink");
1060 if (G_UNLIKELY (!sink)) {
1061 GST_WARNING_OBJECT (self, "Failed to get subpad");
1065 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1066 (self->subtitle_sinkpad), sink))) {
1067 GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
1068 gst_object_unref (sink);
1071 gst_object_unref (sink);
1074 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
1076 if (strcmp (name, "textoverlay") == 0) {
1077 /* Set some textoverlay specific properties */
1078 g_object_set (G_OBJECT (element),
1079 "halign", "center", "valign", "bottom", "wait-text", FALSE, NULL);
1080 if (self->font_desc)
1081 g_object_set (G_OBJECT (element), "font-desc", self->font_desc, NULL);
1082 self->silent_property = "silent";
1083 self->silent_property_invert = FALSE;
1085 self->silent_property =
1086 _get_silent_property (element, &self->silent_property_invert);
1087 if (_has_subtitle_encoding_property (self->renderer))
1088 g_object_set (self->renderer, "subtitle-encoding", self->encoding,
1090 if (_has_font_desc_property (self->renderer))
1091 g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1094 /* First link everything internally */
1095 if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
1096 "ffmpegcolorspace", NULL, "post-colorspace", FALSE))) {
1100 src = gst_element_get_static_pad (element, "src");
1101 if (G_UNLIKELY (!src)) {
1102 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
1106 sink = gst_element_get_static_pad (self->post_colorspace, "sink");
1107 if (G_UNLIKELY (!sink)) {
1108 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
1109 gst_object_unref (src);
1113 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
1114 GST_WARNING_OBJECT (self, "Can't link renderer with ffmpegcolorspace");
1115 gst_object_unref (src);
1116 gst_object_unref (sink);
1119 gst_object_unref (src);
1120 gst_object_unref (sink);
1122 if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
1123 "ffmpegcolorspace", NULL, "pre-colorspace", FALSE))) {
1127 sink = _get_video_pad (element);
1128 if (G_UNLIKELY (!sink)) {
1129 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
1133 src = gst_element_get_static_pad (self->pre_colorspace, "src");
1134 if (G_UNLIKELY (!src)) {
1135 GST_WARNING_OBJECT (self, "Can't get srcpad from ffmpegcolorspace");
1136 gst_object_unref (sink);
1140 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
1141 GST_WARNING_OBJECT (self, "Can't link ffmpegcolorspace to renderer");
1142 gst_object_unref (src);
1143 gst_object_unref (sink);
1146 gst_object_unref (src);
1147 gst_object_unref (sink);
1149 /* Set src ghostpad target */
1150 src = gst_element_get_static_pad (self->post_colorspace, "src");
1151 if (G_UNLIKELY (!src)) {
1152 GST_WARNING_OBJECT (self, "Can't get src pad from ffmpegcolorspace");
1156 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1157 (self->srcpad), src))) {
1158 GST_WARNING_OBJECT (self, "Can't set srcpad target");
1159 gst_object_unref (src);
1162 gst_object_unref (src);
1164 /* Send segments to the renderer if necessary. These are not sent
1165 * outside this element because of the proxy pad event handler */
1166 if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
1169 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1170 if (G_UNLIKELY (!sink)) {
1171 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
1175 _generate_update_segment_event (&self->video_segment, &event1);
1176 GST_DEBUG_OBJECT (self,
1177 "Pushing video segment event: %" GST_PTR_FORMAT, event1);
1178 gst_pad_send_event (sink, event1);
1179 gst_object_unref (sink);
1182 if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
1185 sink = _get_sub_pad (element);
1186 if (G_UNLIKELY (!sink)) {
1187 GST_WARNING_OBJECT (self, "Failed to get subpad");
1191 _generate_update_segment_event (&self->subtitle_segment, &event1);
1192 GST_DEBUG_OBJECT (self,
1193 "Pushing subtitle segment event: %" GST_PTR_FORMAT, event1);
1194 gst_pad_send_event (sink, event1);
1195 gst_object_unref (sink);
1198 /* Set the sink ghostpad targets */
1199 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1200 if (G_UNLIKELY (!sink)) {
1201 GST_WARNING_OBJECT (self, "Can't get sink pad from ffmpegcolorspace");
1205 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1206 (self->video_sinkpad), sink))) {
1207 GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
1208 gst_object_unref (sink);
1211 gst_object_unref (sink);
1213 sink = _get_sub_pad (element);
1214 if (G_UNLIKELY (!sink)) {
1215 GST_WARNING_OBJECT (self, "Failed to get subpad");
1219 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1220 (self->subtitle_sinkpad), sink))) {
1221 GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
1222 gst_object_unref (sink);
1225 gst_object_unref (sink);
1231 if (G_UNLIKELY (l == NULL)) {
1232 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
1233 ("Failed to find any usable factories"));
1234 self->subtitle_error = TRUE;
1235 _setup_passthrough (self);
1236 do_async_done (self);
1238 GST_DEBUG_OBJECT (self, "Everything worked, unblocking pads");
1239 unblock_video (self);
1240 unblock_subtitle (self);
1241 do_async_done (self);
1246 gst_plugin_feature_list_free (factories);
1247 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1249 return GST_PROBE_OK;
1252 static GstStateChangeReturn
1253 gst_subtitle_overlay_change_state (GstElement * element,
1254 GstStateChange transition)
1256 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (element);
1257 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1259 switch (transition) {
1260 case GST_STATE_CHANGE_NULL_TO_READY:
1261 GST_DEBUG_OBJECT (self, "State change NULL->READY");
1262 g_mutex_lock (self->factories_lock);
1263 if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self))) {
1264 g_mutex_unlock (self->factories_lock);
1265 return GST_STATE_CHANGE_FAILURE;
1267 g_mutex_unlock (self->factories_lock);
1269 GST_SUBTITLE_OVERLAY_LOCK (self);
1270 /* Set the internal pads to blocking */
1272 block_subtitle (self);
1273 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1275 case GST_STATE_CHANGE_READY_TO_PAUSED:
1276 GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
1277 gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1278 gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
1280 self->fps_n = self->fps_d = 0;
1282 self->subtitle_flush = FALSE;
1283 self->subtitle_error = FALSE;
1285 self->downstream_chain_error = FALSE;
1287 do_async_start (self);
1288 ret = GST_STATE_CHANGE_ASYNC;
1291 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1292 GST_DEBUG_OBJECT (self, "State change PAUSED->PLAYING");
1298 GstStateChangeReturn bret;
1300 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1301 GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", bret);
1302 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
1304 else if (bret == GST_STATE_CHANGE_ASYNC)
1306 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
1307 do_async_done (self);
1312 switch (transition) {
1313 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1314 GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
1316 case GST_STATE_CHANGE_PAUSED_TO_READY:
1317 GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
1318 do_async_done (self);
1321 case GST_STATE_CHANGE_READY_TO_NULL:
1322 GST_DEBUG_OBJECT (self, "State change READY->NULL");
1324 GST_SUBTITLE_OVERLAY_LOCK (self);
1325 gst_caps_replace (&self->subcaps, NULL);
1327 /* Unlink ghost pads */
1328 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1329 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1330 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1334 unblock_video (self);
1335 unblock_subtitle (self);
1337 /* Remove elements */
1338 self->silent_property = NULL;
1339 _remove_element (self, &self->post_colorspace);
1340 _remove_element (self, &self->overlay);
1341 _remove_element (self, &self->parser);
1342 _remove_element (self, &self->renderer);
1343 _remove_element (self, &self->pre_colorspace);
1344 _remove_element (self, &self->passthrough_identity);
1345 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1356 gst_subtitle_overlay_handle_message (GstBin * bin, GstMessage * message)
1358 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (bin);
1360 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1361 GstObject *src = GST_MESSAGE_SRC (message);
1363 /* Convert error messages from the subtitle pipeline to
1364 * warnings and switch to passthrough mode */
1367 && gst_object_has_ancestor (src,
1368 GST_OBJECT_CAST (self->overlay))) || (self->parser
1369 && gst_object_has_ancestor (src,
1370 GST_OBJECT_CAST (self->parser))) || (self->renderer
1371 && gst_object_has_ancestor (src,
1372 GST_OBJECT_CAST (self->renderer))))) {
1374 gchar *debug = NULL;
1377 gst_message_parse_error (message, &err, &debug);
1378 GST_DEBUG_OBJECT (self,
1379 "Got error message from subtitle element %s: %s (%s)",
1380 GST_MESSAGE_SRC_NAME (message), GST_STR_NULL (err->message),
1381 GST_STR_NULL (debug));
1383 wmsg = gst_message_new_warning (src, err, debug);
1384 gst_message_unref (message);
1389 GST_SUBTITLE_OVERLAY_LOCK (self);
1390 self->subtitle_error = TRUE;
1392 block_subtitle (self);
1394 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1398 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1402 gst_subtitle_overlay_get_property (GObject * object, guint prop_id,
1403 GValue * value, GParamSpec * pspec)
1405 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1409 g_value_set_boolean (value, self->silent);
1411 case PROP_FONT_DESC:
1412 GST_SUBTITLE_OVERLAY_LOCK (self);
1413 g_value_set_string (value, self->font_desc);
1414 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1416 case PROP_SUBTITLE_ENCODING:
1417 GST_SUBTITLE_OVERLAY_LOCK (self);
1418 g_value_set_string (value, self->encoding);
1419 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1428 gst_subtitle_overlay_set_property (GObject * object, guint prop_id,
1429 const GValue * value, GParamSpec * pspec)
1431 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1435 GST_SUBTITLE_OVERLAY_LOCK (self);
1436 self->silent = g_value_get_boolean (value);
1437 if (self->silent_property) {
1438 gboolean silent = self->silent;
1440 if (self->silent_property_invert)
1444 g_object_set (self->overlay, self->silent_property, silent, NULL);
1445 else if (self->renderer)
1446 g_object_set (self->renderer, self->silent_property, silent, NULL);
1448 block_subtitle (self);
1451 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1453 case PROP_FONT_DESC:
1454 GST_SUBTITLE_OVERLAY_LOCK (self);
1455 g_free (self->font_desc);
1456 self->font_desc = g_value_dup_string (value);
1458 g_object_set (self->overlay, "font-desc", self->font_desc, NULL);
1459 else if (self->renderer && _has_font_desc_property (self->renderer))
1460 g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1461 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1463 case PROP_SUBTITLE_ENCODING:
1464 GST_SUBTITLE_OVERLAY_LOCK (self);
1465 g_free (self->encoding);
1466 self->encoding = g_value_dup_string (value);
1467 if (self->renderer && _has_subtitle_encoding_property (self->renderer))
1468 g_object_set (self->renderer, "subtitle-encoding", self->encoding,
1470 if (self->parser && _has_subtitle_encoding_property (self->parser))
1471 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
1472 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1475 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1481 gst_subtitle_overlay_class_init (GstSubtitleOverlayClass * klass)
1483 GObjectClass *gobject_class = (GObjectClass *) klass;
1484 GstElementClass *element_class = (GstElementClass *) klass;
1485 GstBinClass *bin_class = (GstBinClass *) klass;
1487 gobject_class->finalize = gst_subtitle_overlay_finalize;
1488 gobject_class->set_property = gst_subtitle_overlay_set_property;
1489 gobject_class->get_property = gst_subtitle_overlay_get_property;
1491 g_object_class_install_property (gobject_class, PROP_SILENT,
1492 g_param_spec_boolean ("silent",
1494 "Whether to show subtitles", FALSE,
1495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1497 g_object_class_install_property (gobject_class, PROP_FONT_DESC,
1498 g_param_spec_string ("font-desc",
1499 "Subtitle font description",
1500 "Pango font description of font "
1501 "to be used for subtitle rendering", NULL,
1502 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1504 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
1505 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
1506 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
1507 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
1508 "be checked for an encoding to use. If that is not set either, "
1509 "ISO-8859-15 will be assumed.", NULL,
1510 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1512 gst_element_class_add_pad_template (element_class,
1513 gst_static_pad_template_get (&srctemplate));
1515 gst_element_class_add_pad_template (element_class,
1516 gst_static_pad_template_get (&video_sinktemplate));
1517 gst_element_class_add_pad_template (element_class,
1518 gst_static_pad_template_get (&subtitle_sinktemplate));
1520 gst_element_class_set_details_simple (element_class, "Subtitle Overlay",
1521 "Video/Overlay/Subtitle",
1522 "Overlays a video stream with subtitles",
1523 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1525 element_class->change_state =
1526 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_change_state);
1528 bin_class->handle_message =
1529 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_handle_message);
1532 static GstFlowReturn
1533 gst_subtitle_overlay_src_proxy_chain (GstPad * proxypad, GstBuffer * buffer)
1536 GstSubtitleOverlay *self;
1539 ghostpad = GST_PAD_CAST (gst_pad_get_parent (proxypad));
1540 if (G_UNLIKELY (!ghostpad)) {
1541 gst_buffer_unref (buffer);
1542 return GST_FLOW_ERROR;
1544 self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1545 if (G_UNLIKELY (!self || self->srcpad != ghostpad)) {
1546 gst_buffer_unref (buffer);
1547 gst_object_unref (ghostpad);
1548 return GST_FLOW_ERROR;
1551 ret = gst_proxy_pad_chain_default (proxypad, buffer);
1553 if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1554 GST_ERROR_OBJECT (self, "Downstream chain error: %s",
1555 gst_flow_get_name (ret));
1556 self->downstream_chain_error = TRUE;
1559 gst_object_unref (self);
1560 gst_object_unref (ghostpad);
1566 gst_subtitle_overlay_src_proxy_event (GstPad * proxypad, GstEvent * event)
1568 GstPad *ghostpad = NULL;
1569 GstSubtitleOverlay *self = NULL;
1570 gboolean ret = FALSE;
1571 const GstStructure *s;
1573 ghostpad = GST_PAD_CAST (gst_pad_get_parent (proxypad));
1574 if (G_UNLIKELY (!ghostpad))
1576 self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1577 if (G_UNLIKELY (!self || self->srcpad != ghostpad))
1580 s = gst_event_get_structure (event);
1581 if (s && gst_structure_id_has_field (s, _subtitle_overlay_event_marker_id)) {
1582 GST_DEBUG_OBJECT (ghostpad, "Dropping event with marker: %" GST_PTR_FORMAT,
1584 gst_event_unref (event);
1588 ret = gst_proxy_pad_event_default (proxypad, event);
1594 gst_event_unref (event);
1596 gst_object_unref (self);
1598 gst_object_unref (ghostpad);
1603 gst_subtitle_overlay_video_sink_setcaps (GstSubtitleOverlay * self,
1606 gboolean ret = TRUE;
1609 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1611 if (!gst_video_parse_caps_framerate (caps, &fps_n, &fps_d)) {
1612 GST_ERROR_OBJECT (self, "Failed to parse framerate from caps");
1617 GST_SUBTITLE_OVERLAY_LOCK (self);
1618 if (self->fps_n != fps_n || self->fps_d != fps_d) {
1619 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
1620 self->fps_n = fps_n;
1621 self->fps_d = fps_d;
1622 gst_subtitle_overlay_set_fps (self);
1624 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1631 gst_subtitle_overlay_video_sink_event (GstPad * pad, GstEvent * event)
1633 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1636 switch (GST_EVENT_TYPE (event)) {
1637 case GST_EVENT_FLUSH_STOP:
1639 GST_DEBUG_OBJECT (pad,
1640 "Resetting video segment because of flush-stop event");
1641 gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1642 self->fps_n = self->fps_d = 0;
1645 case GST_EVENT_CAPS:
1649 gst_event_parse_caps (event, &caps);
1650 ret = gst_subtitle_overlay_video_sink_setcaps (self, caps);
1659 ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
1661 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1662 GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
1663 gst_event_copy_segment (event, &self->video_segment);
1665 if (self->video_segment.format != GST_FORMAT_TIME)
1666 goto invalid_format;
1670 gst_event_unref (event);
1671 gst_object_unref (self);
1678 GST_ERROR_OBJECT (pad, "Newsegment event in non-time format: %s",
1679 gst_format_get_name (self->video_segment.format));
1685 static GstFlowReturn
1686 gst_subtitle_overlay_video_sink_chain (GstPad * pad, GstBuffer * buffer)
1688 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (GST_PAD_PARENT (pad));
1689 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, buffer);
1691 if (G_UNLIKELY (self->downstream_chain_error) || self->passthrough_identity) {
1693 } else if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1694 GST_DEBUG_OBJECT (self, "Subtitle renderer produced chain error: %s",
1695 gst_flow_get_name (ret));
1696 GST_SUBTITLE_OVERLAY_LOCK (self);
1697 self->subtitle_error = TRUE;
1698 block_subtitle (self);
1700 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1708 static GstFlowReturn
1709 gst_subtitle_overlay_subtitle_sink_chain (GstPad * pad, GstBuffer * buffer)
1711 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (GST_PAD_PARENT (pad));
1713 if (self->subtitle_error) {
1714 gst_buffer_unref (buffer);
1717 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, buffer);
1719 if (IS_SUBTITLE_CHAIN_IGNORE_ERROR (ret)) {
1720 GST_DEBUG_OBJECT (self, "Subtitle chain error: %s",
1721 gst_flow_get_name (ret));
1722 GST_SUBTITLE_OVERLAY_LOCK (self);
1723 self->subtitle_error = TRUE;
1724 block_subtitle (self);
1726 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1736 gst_subtitle_overlay_subtitle_sink_getcaps (GstPad * pad, GstCaps * filter)
1738 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1741 g_mutex_lock (self->factories_lock);
1742 if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self)))
1743 ret = GST_CAPS_NONE;
1746 gst_caps_intersect_full (filter, self->factory_caps,
1747 GST_CAPS_INTERSECT_FIRST);
1749 ret = gst_caps_ref (self->factory_caps);
1750 g_mutex_unlock (self->factories_lock);
1752 GST_DEBUG_OBJECT (pad, "Returning subtitle caps %" GST_PTR_FORMAT, ret);
1754 gst_object_unref (self);
1760 gst_subtitle_overlay_subtitle_sink_acceptcaps (GstPad * pad, GstCaps * caps)
1762 GstCaps *othercaps = gst_subtitle_overlay_subtitle_sink_getcaps (pad, NULL);
1763 gboolean ret = gst_caps_can_intersect (caps, othercaps);
1765 gst_caps_unref (othercaps);
1771 gst_subtitle_overlay_subtitle_sink_setcaps (GstSubtitleOverlay * self,
1774 gboolean ret = TRUE;
1775 GstPad *target = NULL;;
1777 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1780 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1782 GST_SUBTITLE_OVERLAY_LOCK (self);
1783 gst_caps_replace (&self->subcaps, caps);
1785 if (target && gst_pad_accept_caps (target, caps)) {
1786 GST_DEBUG_OBJECT (self, "Target accepts caps");
1787 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1791 GST_DEBUG_OBJECT (self, "Target did not accept caps");
1793 self->subtitle_error = FALSE;
1794 block_subtitle (self);
1796 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1800 gst_object_unref (target);
1805 static GstPadLinkReturn
1806 gst_subtitle_overlay_subtitle_sink_link (GstPad * pad, GstPad * peer)
1808 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1809 GstPadLinkReturn ret;
1812 GST_DEBUG_OBJECT (pad, "Linking pad to peer %" GST_PTR_FORMAT, peer);
1814 caps = gst_pad_get_negotiated_caps (peer);
1816 caps = gst_pad_get_caps (peer, NULL);
1817 if (!gst_caps_is_fixed (caps)) {
1818 gst_caps_unref (caps);
1824 GST_SUBTITLE_OVERLAY_LOCK (self);
1825 GST_DEBUG_OBJECT (pad, "Have fixed peer caps: %" GST_PTR_FORMAT, caps);
1826 gst_caps_replace (&self->subcaps, caps);
1828 self->subtitle_error = FALSE;
1830 block_subtitle (self);
1832 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1833 gst_caps_unref (caps);
1836 ret = gst_ghost_pad_link_default (pad, peer);
1838 gst_object_unref (self);
1843 gst_subtitle_overlay_subtitle_sink_unlink (GstPad * pad)
1845 GstSubtitleOverlay *self =
1846 GST_SUBTITLE_OVERLAY (gst_object_ref (GST_PAD_PARENT (pad)));
1848 /* FIXME: Can't use gst_pad_get_parent() here because this is called with
1849 * the object lock from state changes
1852 GST_DEBUG_OBJECT (pad, "Pad unlinking");
1853 gst_caps_replace (&self->subcaps, NULL);
1855 gst_ghost_pad_unlink_default (pad);
1857 GST_SUBTITLE_OVERLAY_LOCK (self);
1858 self->subtitle_error = FALSE;
1860 block_subtitle (self);
1862 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1864 gst_object_unref (self);
1868 gst_subtitle_overlay_subtitle_sink_event (GstPad * pad, GstEvent * event)
1870 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (gst_pad_get_parent (pad));
1873 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB &&
1874 gst_event_has_name (event, "subtitleoverlay-flush-subtitle")) {
1875 GST_DEBUG_OBJECT (pad, "Custom subtitle flush event");
1876 GST_SUBTITLE_OVERLAY_LOCK (self);
1877 self->subtitle_flush = TRUE;
1878 self->subtitle_error = FALSE;
1879 block_subtitle (self);
1881 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1883 gst_event_unref (event);
1889 switch (GST_EVENT_TYPE (event)) {
1890 case GST_EVENT_CAPS:
1894 gst_event_parse_caps (event, &caps);
1895 ret = gst_subtitle_overlay_subtitle_sink_setcaps (self, caps);
1900 case GST_EVENT_FLUSH_STOP:
1901 GST_DEBUG_OBJECT (pad,
1902 "Resetting subtitle segment because of flush-stop");
1903 gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
1905 case GST_EVENT_FLUSH_START:
1906 case GST_EVENT_SEGMENT:
1909 GstStructure *structure;
1911 /* Add our event marker to make sure no events from here go ever outside
1912 * the element, they're only interesting for our internal elements */
1913 event = GST_EVENT_CAST (gst_event_make_writable (event));
1914 structure = gst_event_writable_structure (event);
1916 gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
1917 G_TYPE_BOOLEAN, TRUE, NULL);
1924 ret = gst_proxy_pad_event_default (pad, gst_event_ref (event));
1926 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1927 GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
1928 gst_event_copy_segment (event, &self->subtitle_segment);
1929 GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
1930 &self->subtitle_segment);
1932 gst_event_unref (event);
1935 gst_object_unref (self);
1940 gst_subtitle_overlay_init (GstSubtitleOverlay * self)
1942 GstPadTemplate *templ;
1943 GstPad *proxypad = NULL;
1945 self->lock = g_mutex_new ();
1946 self->factories_lock = g_mutex_new ();
1948 templ = gst_static_pad_template_get (&srctemplate);
1949 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
1952 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->srcpad)));
1953 gst_pad_set_event_function (proxypad,
1954 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_event));
1955 gst_pad_set_chain_function (proxypad,
1956 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_chain));
1957 gst_object_unref (proxypad);
1959 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
1961 templ = gst_static_pad_template_get (&video_sinktemplate);
1962 self->video_sinkpad =
1963 gst_ghost_pad_new_no_target_from_template ("video_sink", templ);
1964 gst_pad_set_event_function (self->video_sinkpad,
1965 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_event));
1966 gst_pad_set_chain_function (self->video_sinkpad,
1967 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_chain));
1970 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
1971 (self->video_sinkpad)));
1972 self->video_block_pad = proxypad;
1974 gst_element_add_pad (GST_ELEMENT_CAST (self), self->video_sinkpad);
1976 templ = gst_static_pad_template_get (&subtitle_sinktemplate);
1977 self->subtitle_sinkpad =
1978 gst_ghost_pad_new_no_target_from_template ("subtitle_sink", templ);
1979 gst_pad_set_link_function (self->subtitle_sinkpad,
1980 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_link));
1981 gst_pad_set_unlink_function (self->subtitle_sinkpad,
1982 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_unlink));
1983 gst_pad_set_event_function (self->subtitle_sinkpad,
1984 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_event));
1985 gst_pad_set_chain_function (self->subtitle_sinkpad,
1986 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_chain));
1987 gst_pad_set_getcaps_function (self->subtitle_sinkpad,
1988 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_getcaps));
1989 gst_pad_set_acceptcaps_function (self->subtitle_sinkpad,
1990 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_acceptcaps));
1993 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
1994 (self->subtitle_sinkpad)));
1995 self->subtitle_block_pad = proxypad;
1997 gst_element_add_pad (GST_ELEMENT_CAST (self), self->subtitle_sinkpad);
2004 gst_subtitle_overlay_plugin_init (GstPlugin * plugin)
2006 GST_DEBUG_CATEGORY_INIT (subtitle_overlay_debug, "subtitleoverlay", 0,
2007 "Subtitle Overlay");
2009 _subtitle_overlay_event_marker_id =
2010 g_quark_from_static_string ("gst-subtitle-overlay-event-marker");
2012 return gst_element_register (plugin, "subtitleoverlay", GST_RANK_NONE,
2013 GST_TYPE_SUBTITLE_OVERLAY);