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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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 ! videoconvert ! autovideosink demux. ! "subpicture/x-dvd" ! queue2 ! overlay.
33 * ]| This will play back the given Matroska file with h264 video and subpicture subtitles.
41 #include "gstsubtitleoverlay.h"
43 #include <gst/pbutils/missing-plugins.h>
44 #include <gst/video/video.h>
47 GST_DEBUG_CATEGORY_STATIC (subtitle_overlay_debug);
48 #define GST_CAT_DEFAULT subtitle_overlay_debug
50 #define IS_SUBTITLE_CHAIN_IGNORE_ERROR(flow) \
51 G_UNLIKELY (flow == GST_FLOW_ERROR || flow == GST_FLOW_NOT_NEGOTIATED)
53 #define IS_VIDEO_CHAIN_IGNORE_ERROR(flow) \
54 G_UNLIKELY (flow == GST_FLOW_ERROR)
56 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
61 static GstStaticPadTemplate video_sinktemplate =
62 GST_STATIC_PAD_TEMPLATE ("video_sink",
67 static GstStaticPadTemplate subtitle_sinktemplate =
68 GST_STATIC_PAD_TEMPLATE ("subtitle_sink",
78 PROP_SUBTITLE_ENCODING
81 #define gst_subtitle_overlay_parent_class parent_class
82 G_DEFINE_TYPE (GstSubtitleOverlay, gst_subtitle_overlay, GST_TYPE_BIN);
84 static GQuark _subtitle_overlay_event_marker_id = 0;
87 do_async_start (GstSubtitleOverlay * self)
89 if (!self->do_async) {
90 GstMessage *msg = gst_message_new_async_start (GST_OBJECT_CAST (self));
92 GST_DEBUG_OBJECT (self, "Posting async-start");
93 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
94 self->do_async = TRUE;
99 do_async_done (GstSubtitleOverlay * self)
101 if (self->do_async) {
102 GstMessage *msg = gst_message_new_async_done (GST_OBJECT_CAST (self),
103 GST_CLOCK_TIME_NONE);
105 GST_DEBUG_OBJECT (self, "Posting async-done");
106 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
107 self->do_async = FALSE;
111 static GstPadProbeReturn
112 _pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
115 block_video (GstSubtitleOverlay * self)
117 if (self->video_block_id != 0)
120 if (self->video_block_pad) {
121 self->video_block_id =
122 gst_pad_add_probe (self->video_block_pad,
123 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, _pad_blocked_cb, self, NULL);
128 unblock_video (GstSubtitleOverlay * self)
130 if (self->video_block_id) {
131 gst_pad_remove_probe (self->video_block_pad, self->video_block_id);
132 self->video_sink_blocked = FALSE;
133 self->video_block_id = 0;
138 block_subtitle (GstSubtitleOverlay * self)
140 if (self->subtitle_block_id != 0)
143 if (self->subtitle_block_pad) {
144 self->subtitle_block_id =
145 gst_pad_add_probe (self->subtitle_block_pad,
146 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, _pad_blocked_cb, self, NULL);
151 unblock_subtitle (GstSubtitleOverlay * self)
153 if (self->subtitle_block_id) {
154 gst_pad_remove_probe (self->subtitle_block_pad, self->subtitle_block_id);
155 self->subtitle_sink_blocked = FALSE;
156 self->subtitle_block_id = 0;
161 gst_subtitle_overlay_finalize (GObject * object)
163 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (object);
165 g_mutex_clear (&self->lock);
166 g_mutex_clear (&self->factories_lock);
169 gst_plugin_feature_list_free (self->factories);
170 self->factories = NULL;
171 gst_caps_replace (&self->factory_caps, NULL);
173 if (self->font_desc) {
174 g_free (self->font_desc);
175 self->font_desc = NULL;
178 if (self->encoding) {
179 g_free (self->encoding);
180 self->encoding = NULL;
183 G_OBJECT_CLASS (parent_class)->finalize (object);
187 _is_renderer (GstElementFactory * factory)
189 const gchar *klass, *name;
192 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
193 name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
196 if (strstr (klass, "Overlay/Subtitle") != NULL ||
197 strstr (klass, "Overlay/SubPicture") != NULL)
199 if (strcmp (name, "textoverlay") == 0)
206 _is_parser (GstElementFactory * factory)
211 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
213 if (klass != NULL && strstr (klass, "Parser/Subtitle") != NULL)
218 static const gchar *const _sub_pad_names[] = { "subpicture", "subpicture_sink",
220 "subtitle_sink", "subtitle"
224 _is_video_pad (GstPad * pad, gboolean * hw_accelerated)
226 GstPad *peer = gst_pad_get_peer (pad);
232 caps = gst_pad_get_current_caps (peer);
234 caps = gst_pad_query_caps (peer, NULL);
236 gst_object_unref (peer);
238 caps = gst_pad_query_caps (pad, NULL);
241 name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
242 if (g_str_equal (name, "video/x-raw")) {
245 *hw_accelerated = FALSE;
247 } else if (g_str_has_prefix (name, "video/x-surface")) {
250 *hw_accelerated = TRUE;
255 *hw_accelerated = FALSE;
258 gst_caps_unref (caps);
264 _get_sub_caps (GstElementFactory * factory)
266 const GList *templates;
268 gboolean is_parser = _is_parser (factory);
270 templates = gst_element_factory_get_static_pad_templates (factory);
271 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
272 GstStaticPadTemplate *templ = walk->data;
274 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
275 gboolean found = FALSE;
282 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
283 if (strcmp (templ->name_template, _sub_pad_names[i]) == 0) {
290 return gst_static_caps_get (&templ->static_caps);
297 _factory_filter (GstPluginFeature * feature, GstCaps ** subcaps)
299 GstElementFactory *factory;
302 const GList *templates;
304 gboolean is_renderer;
305 GstCaps *templ_caps = NULL;
306 gboolean have_video_sink = FALSE;
308 /* we only care about element factories */
309 if (!GST_IS_ELEMENT_FACTORY (feature))
312 factory = GST_ELEMENT_FACTORY_CAST (feature);
314 /* only select elements with autoplugging rank or textoverlay */
315 name = gst_plugin_feature_get_name (feature);
316 rank = gst_plugin_feature_get_rank (feature);
317 if (strcmp ("textoverlay", name) != 0 && rank < GST_RANK_MARGINAL)
320 /* Check if it's a renderer or a parser */
321 if (_is_renderer (factory)) {
323 } else if (_is_parser (factory)) {
329 /* Check if there's a video sink in case of a renderer */
331 templates = gst_element_factory_get_static_pad_templates (factory);
332 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
333 GstStaticPadTemplate *templ = walk->data;
335 /* we only care about the always-sink templates */
336 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
337 if (strcmp (templ->name_template, "video") == 0 ||
338 strcmp (templ->name_template, "video_sink") == 0) {
339 have_video_sink = TRUE;
344 templ_caps = _get_sub_caps (factory);
346 if (is_renderer && have_video_sink && templ_caps) {
347 GST_DEBUG ("Found renderer element %s (%s) with caps %" GST_PTR_FORMAT,
348 gst_element_factory_get_metadata (factory,
349 GST_ELEMENT_METADATA_LONGNAME),
350 gst_plugin_feature_get_name (feature), templ_caps);
351 *subcaps = gst_caps_merge (*subcaps, templ_caps);
353 } else if (!is_renderer && !have_video_sink && templ_caps) {
354 GST_DEBUG ("Found parser element %s (%s) with caps %" GST_PTR_FORMAT,
355 gst_element_factory_get_metadata (factory,
356 GST_ELEMENT_METADATA_LONGNAME),
357 gst_plugin_feature_get_name (feature), templ_caps);
358 *subcaps = gst_caps_merge (*subcaps, templ_caps);
362 gst_caps_unref (templ_caps);
367 /* Call with factories_lock! */
369 gst_subtitle_overlay_update_factory_list (GstSubtitleOverlay * self)
371 GstRegistry *registry;
374 registry = gst_registry_get ();
375 cookie = gst_registry_get_feature_list_cookie (registry);
376 if (!self->factories || self->factories_cookie != cookie) {
380 subcaps = gst_caps_new_empty ();
382 factories = gst_registry_feature_filter (registry,
383 (GstPluginFeatureFilter) _factory_filter, FALSE, &subcaps);
384 GST_DEBUG_OBJECT (self, "Created factory caps: %" GST_PTR_FORMAT, subcaps);
385 gst_caps_replace (&self->factory_caps, subcaps);
386 gst_caps_unref (subcaps);
388 gst_plugin_feature_list_free (self->factories);
389 self->factories = factories;
390 self->factories_cookie = cookie;
393 return (self->factories != NULL);
396 G_LOCK_DEFINE_STATIC (_factory_caps);
397 static GstCaps *_factory_caps = NULL;
398 static guint32 _factory_caps_cookie = 0;
401 gst_subtitle_overlay_create_factory_caps (void)
403 GstRegistry *registry;
405 GstCaps *subcaps = NULL;
408 registry = gst_registry_get ();
409 cookie = gst_registry_get_feature_list_cookie (registry);
410 G_LOCK (_factory_caps);
411 if (!_factory_caps || _factory_caps_cookie != cookie) {
413 gst_caps_unref (_factory_caps);
414 _factory_caps = gst_caps_new_empty ();
416 factories = gst_registry_feature_filter (registry,
417 (GstPluginFeatureFilter) _factory_filter, FALSE, &_factory_caps);
418 GST_DEBUG ("Created factory caps: %" GST_PTR_FORMAT, _factory_caps);
419 gst_plugin_feature_list_free (factories);
420 _factory_caps_cookie = cookie;
422 subcaps = gst_caps_ref (_factory_caps);
423 G_UNLOCK (_factory_caps);
429 check_factory_for_caps (GstElementFactory * factory, const GstCaps * caps)
431 GstCaps *fcaps = _get_sub_caps (factory);
432 gboolean ret = (fcaps) ? gst_caps_is_subset (caps, fcaps) : FALSE;
435 gst_caps_unref (fcaps);
438 gst_object_ref (factory);
443 gst_subtitle_overlay_get_factories_for_caps (const GList * list,
444 const GstCaps * caps)
446 const GList *walk = list;
447 GList *result = NULL;
450 GstElementFactory *factory = walk->data;
452 walk = g_list_next (walk);
454 if (check_factory_for_caps (factory, caps)) {
455 result = g_list_prepend (result, factory);
463 _sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
466 const gchar *rname1, *rname2;
468 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
472 /* If the ranks are the same sort by name to get deterministic results */
473 rname1 = gst_plugin_feature_get_name (f1);
474 rname2 = gst_plugin_feature_get_name (f2);
476 diff = strcmp (rname1, rname2);
482 _get_sub_pad (GstElement * element)
487 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
488 pad = gst_element_get_static_pad (element, _sub_pad_names[i]);
496 _get_video_pad (GstElement * element)
498 static const gchar *const pad_names[] = { "video", "video_sink" };
502 for (i = 0; i < G_N_ELEMENTS (pad_names); i++) {
503 pad = gst_element_get_static_pad (element, pad_names[i]);
511 _create_element (GstSubtitleOverlay * self, GstElement ** element,
512 const gchar * factory_name, GstElementFactory * factory,
513 const gchar * element_name, gboolean mandatory)
517 g_assert (!factory || !factory_name);
520 elt = gst_element_factory_make (factory_name, element_name);
523 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
524 elt = gst_element_factory_create (factory, element_name);
527 if (G_UNLIKELY (!elt)) {
532 gst_missing_element_message_new (GST_ELEMENT_CAST (self),
534 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
537 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
538 ("no '%s' plugin found", factory_name));
540 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
541 ("no '%s' plugin found", factory_name));
544 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
545 ("can't instantiate '%s'", factory_name));
547 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
548 ("can't instantiate '%s'", factory_name));
555 if (G_UNLIKELY (gst_element_set_state (elt,
556 GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS)) {
557 gst_object_unref (elt);
559 GST_ELEMENT_ERROR (self, CORE, STATE_CHANGE, (NULL),
560 ("failed to set '%s' to READY", factory_name));
562 GST_WARNING_OBJECT (self, "Failed to set '%s' to READY", factory_name);
567 if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (self), gst_object_ref (elt)))) {
568 gst_element_set_state (elt, GST_STATE_NULL);
569 gst_object_unref (elt);
571 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
572 ("failed to add '%s' to subtitleoverlay", factory_name));
574 GST_WARNING_OBJECT (self, "Failed to add '%s' to subtitleoverlay",
580 gst_element_sync_state_with_parent (elt);
586 _remove_element (GstSubtitleOverlay * self, GstElement ** element)
589 gst_bin_remove (GST_BIN_CAST (self), *element);
590 gst_element_set_state (*element, GST_STATE_NULL);
591 gst_object_unref (*element);
597 _setup_passthrough (GstSubtitleOverlay * self)
600 GstElement *identity;
602 GST_DEBUG_OBJECT (self, "Doing video passthrough");
604 if (self->passthrough_identity) {
605 GST_DEBUG_OBJECT (self, "Already in passthrough mode");
609 /* Unlink & destroy everything */
610 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
611 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
612 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad), NULL);
613 self->silent_property = NULL;
614 _remove_element (self, &self->post_colorspace);
615 _remove_element (self, &self->overlay);
616 _remove_element (self, &self->parser);
617 _remove_element (self, &self->renderer);
618 _remove_element (self, &self->pre_colorspace);
619 _remove_element (self, &self->passthrough_identity);
621 if (G_UNLIKELY (!_create_element (self, &self->passthrough_identity,
622 "identity", NULL, "passthrough-identity", TRUE))) {
626 identity = self->passthrough_identity;
627 g_object_set (G_OBJECT (identity), "silent", TRUE, "signal-handoffs", FALSE,
630 /* Set src ghostpad target */
631 src = gst_element_get_static_pad (self->passthrough_identity, "src");
632 if (G_UNLIKELY (!src)) {
633 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
634 ("Failed to get srcpad from identity"));
638 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
640 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
641 ("Failed to set srcpad target"));
642 gst_object_unref (src);
645 gst_object_unref (src);
647 sink = gst_element_get_static_pad (self->passthrough_identity, "sink");
648 if (G_UNLIKELY (!sink)) {
649 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
650 ("Failed to get sinkpad from identity"));
654 /* Link sink ghostpads to identity */
655 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
656 (self->video_sinkpad), sink))) {
657 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
658 ("Failed to set video sinkpad target"));
659 gst_object_unref (sink);
662 gst_object_unref (sink);
664 GST_DEBUG_OBJECT (self, "Video passthrough setup successfully");
668 unblock_video (self);
669 unblock_subtitle (self);
674 /* Must be called with subtitleoverlay lock! */
676 _has_property_with_type (GObject * object, const gchar * property, GType type)
678 GObjectClass *gobject_class;
681 gobject_class = G_OBJECT_GET_CLASS (object);
682 pspec = g_object_class_find_property (gobject_class, property);
683 return (pspec && pspec->value_type == type);
687 gst_subtitle_overlay_set_fps (GstSubtitleOverlay * self)
689 if (!self->parser || self->fps_d == 0)
692 if (!_has_property_with_type (G_OBJECT (self->parser), "video-fps",
696 GST_DEBUG_OBJECT (self, "Updating video-fps property in parser");
697 g_object_set (self->parser, "video-fps", self->fps_n, self->fps_d, NULL);
701 _get_silent_property (GstElement * element, gboolean * invert)
712 for (i = 0; i < G_N_ELEMENTS (properties); i++) {
713 if (_has_property_with_type (G_OBJECT (element), properties[i].name,
715 *invert = properties[i].invert;
716 return properties[i].name;
723 _setup_parser (GstSubtitleOverlay * self)
727 /* Try to get the latest video framerate */
728 video_peer = gst_pad_get_peer (self->video_sinkpad);
733 video_caps = gst_pad_get_current_caps (video_peer);
735 video_caps = gst_pad_query_caps (video_peer, NULL);
736 if (!gst_caps_is_fixed (video_caps)) {
737 gst_caps_unref (video_caps);
743 GstStructure *st = gst_caps_get_structure (video_caps, 0);
744 if (gst_structure_get_fraction (st, "framerate", &fps_n, &fps_d)) {
745 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
752 gst_caps_unref (video_caps);
753 gst_object_unref (video_peer);
756 if (_has_property_with_type (G_OBJECT (self->parser), "subtitle-encoding",
758 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
760 /* Try to set video fps on the parser */
761 gst_subtitle_overlay_set_fps (self);
768 _setup_renderer (GstSubtitleOverlay * self, GstElement * renderer)
770 GstElementFactory *factory = gst_element_get_factory (renderer);
772 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
774 if (strcmp (name, "textoverlay") == 0) {
775 /* Set some textoverlay specific properties */
776 gst_util_set_object_arg (G_OBJECT (renderer), "halignment", "center");
777 gst_util_set_object_arg (G_OBJECT (renderer), "valignment", "bottom");
778 g_object_set (G_OBJECT (renderer), "wait-text", FALSE, NULL);
780 g_object_set (G_OBJECT (renderer), "font-desc", self->font_desc, NULL);
781 self->silent_property = "silent";
782 self->silent_property_invert = FALSE;
784 self->silent_property =
785 _get_silent_property (renderer, &self->silent_property_invert);
786 if (_has_property_with_type (G_OBJECT (renderer), "subtitle-encoding",
788 g_object_set (renderer, "subtitle-encoding", self->encoding, NULL);
789 if (_has_property_with_type (G_OBJECT (renderer), "font-desc",
791 g_object_set (renderer, "font-desc", self->font_desc, NULL);
797 /* subtitle_src==NULL means: use subtitle_sink ghostpad */
799 _link_renderer (GstSubtitleOverlay * self, GstElement * renderer,
800 GstPad * subtitle_src)
803 gboolean is_video, is_hw;
805 is_video = _is_video_pad (self->video_sinkpad, &is_hw);
808 gboolean render_is_hw;
810 /* First check that renderer also supports the video format */
811 sink = _get_video_pad (renderer);
812 if (G_UNLIKELY (!sink)) {
813 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
817 if (is_video != _is_video_pad (sink, &render_is_hw) ||
818 is_hw != render_is_hw) {
819 GST_DEBUG_OBJECT (self, "Renderer doesn't support %s video",
820 is_hw ? "surface" : "raw");
821 gst_object_unref (sink);
824 gst_object_unref (sink);
827 /* First link everything internally */
828 if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
829 COLORSPACE, NULL, "post-colorspace", FALSE))) {
832 src = gst_element_get_static_pad (renderer, "src");
833 if (G_UNLIKELY (!src)) {
834 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
838 sink = gst_element_get_static_pad (self->post_colorspace, "sink");
839 if (G_UNLIKELY (!sink)) {
840 GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
841 gst_object_unref (src);
845 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
846 GST_WARNING_OBJECT (self, "Can't link renderer with " COLORSPACE);
847 gst_object_unref (src);
848 gst_object_unref (sink);
851 gst_object_unref (src);
852 gst_object_unref (sink);
854 if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
855 COLORSPACE, NULL, "pre-colorspace", FALSE))) {
859 sink = _get_video_pad (renderer);
860 if (G_UNLIKELY (!sink)) {
861 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
865 src = gst_element_get_static_pad (self->pre_colorspace, "src");
866 if (G_UNLIKELY (!src)) {
867 GST_WARNING_OBJECT (self, "Can't get srcpad from " COLORSPACE);
868 gst_object_unref (sink);
872 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
873 GST_WARNING_OBJECT (self, "Can't link " COLORSPACE " to renderer");
874 gst_object_unref (src);
875 gst_object_unref (sink);
878 gst_object_unref (src);
879 gst_object_unref (sink);
881 /* Set src ghostpad target */
882 src = gst_element_get_static_pad (self->post_colorspace, "src");
883 if (G_UNLIKELY (!src)) {
884 GST_WARNING_OBJECT (self, "Can't get src pad from " COLORSPACE);
888 /* Set src ghostpad target in the harware accelerated case */
890 src = gst_element_get_static_pad (renderer, "src");
891 if (G_UNLIKELY (!src)) {
892 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
896 } else { /* No video pad */
897 GstCaps *allowed_caps, *video_caps = NULL;
899 gboolean is_subset = FALSE;
901 video_peer = gst_pad_get_peer (self->video_sinkpad);
903 video_caps = gst_pad_get_current_caps (video_peer);
905 video_caps = gst_pad_query_caps (video_peer, NULL);
907 gst_object_unref (video_peer);
910 sink = _get_video_pad (renderer);
911 if (G_UNLIKELY (!sink)) {
912 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
915 allowed_caps = gst_pad_query_caps (sink, NULL);
916 gst_object_unref (sink);
918 if (allowed_caps && video_caps)
919 is_subset = gst_caps_is_subset (video_caps, allowed_caps);
922 gst_caps_unref (allowed_caps);
925 gst_caps_unref (video_caps);
927 if (G_UNLIKELY (!is_subset)) {
928 GST_WARNING_OBJECT (self, "Renderer with custom caps is not "
929 "compatible with video stream");
933 src = gst_element_get_static_pad (renderer, "src");
934 if (G_UNLIKELY (!src)) {
935 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
940 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
941 (self->srcpad), src))) {
942 GST_WARNING_OBJECT (self, "Can't set srcpad target");
943 gst_object_unref (src);
946 gst_object_unref (src);
948 /* Set the sink ghostpad targets */
949 if (self->pre_colorspace) {
950 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
951 if (G_UNLIKELY (!sink)) {
952 GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
956 sink = _get_video_pad (renderer);
957 if (G_UNLIKELY (!sink)) {
958 GST_WARNING_OBJECT (self, "Can't get sink pad from %" GST_PTR_FORMAT,
964 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
965 (self->video_sinkpad), sink))) {
966 GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
967 gst_object_unref (sink);
970 gst_object_unref (sink);
972 sink = _get_sub_pad (renderer);
973 if (G_UNLIKELY (!sink)) {
974 GST_WARNING_OBJECT (self, "Failed to get subpad");
979 if (G_UNLIKELY (gst_pad_link (subtitle_src, sink) != GST_PAD_LINK_OK)) {
980 GST_WARNING_OBJECT (self, "Failed to link subtitle srcpad with renderer");
981 gst_object_unref (sink);
985 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
986 (self->subtitle_sinkpad), sink))) {
987 GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
988 gst_object_unref (sink);
992 gst_object_unref (sink);
997 static GstPadProbeReturn
998 _pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
1000 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (user_data);
1002 GList *l, *factories = NULL;
1004 GST_DEBUG_OBJECT (pad, "Pad blocked");
1006 GST_SUBTITLE_OVERLAY_LOCK (self);
1007 if (pad == self->video_block_pad)
1008 self->video_sink_blocked = TRUE;
1009 else if (pad == self->subtitle_block_pad)
1010 self->subtitle_sink_blocked = TRUE;
1012 /* Now either both or the video sink are blocked */
1014 /* Get current subtitle caps */
1015 subcaps = self->subcaps;
1019 peer = gst_pad_get_peer (self->subtitle_sinkpad);
1021 subcaps = gst_pad_get_current_caps (peer);
1023 subcaps = gst_pad_query_caps (peer, NULL);
1024 if (!gst_caps_is_fixed (subcaps)) {
1025 gst_caps_unref (subcaps);
1029 gst_object_unref (peer);
1031 gst_caps_replace (&self->subcaps, subcaps);
1033 gst_caps_unref (subcaps);
1035 GST_DEBUG_OBJECT (self, "Current subtitle caps: %" GST_PTR_FORMAT, subcaps);
1037 /* If there are no subcaps but the subtitle sink is blocked upstream
1038 * must behave wrong as there are no fixed caps set for the first
1039 * buffer or in-order event */
1040 if (G_UNLIKELY (!subcaps && self->subtitle_sink_blocked)) {
1041 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION, (NULL),
1042 ("Subtitle sink is blocked but we have no subtitle caps"));
1046 if (self->subtitle_error || (self->silent && !self->silent_property)) {
1047 _setup_passthrough (self);
1048 do_async_done (self);
1052 /* Now do something with the caps */
1053 if (subcaps && !self->subtitle_flush) {
1055 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1057 if (target && gst_pad_query_accept_caps (target, subcaps)) {
1058 GST_DEBUG_OBJECT (pad, "Target accepts caps");
1060 gst_object_unref (target);
1063 unblock_video (self);
1064 unblock_subtitle (self);
1066 } else if (target) {
1067 gst_object_unref (target);
1071 if (self->subtitle_sink_blocked && !self->video_sink_blocked) {
1072 GST_DEBUG_OBJECT (self, "Subtitle sink blocked but video not blocked");
1077 self->subtitle_flush = FALSE;
1079 /* Find our factories */
1080 g_mutex_lock (&self->factories_lock);
1081 gst_subtitle_overlay_update_factory_list (self);
1084 gst_subtitle_overlay_get_factories_for_caps (self->factories, subcaps);
1088 msg = gst_missing_decoder_message_new (GST_ELEMENT_CAST (self), subcaps);
1089 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
1090 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
1091 ("no suitable subtitle plugin found"));
1093 self->subtitle_error = TRUE;
1096 g_mutex_unlock (&self->factories_lock);
1099 _setup_passthrough (self);
1100 do_async_done (self);
1104 /* Now the interesting parts are done: subtitle overlaying! */
1106 /* Sort the factories by rank */
1107 factories = g_list_sort (factories, (GCompareFunc) _sort_by_ranks);
1109 for (l = factories; l; l = l->next) {
1110 GstElementFactory *factory = l->data;
1111 gboolean is_renderer = _is_renderer (factory);
1114 /* Unlink & destroy everything */
1116 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1117 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1118 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1120 self->silent_property = NULL;
1121 _remove_element (self, &self->post_colorspace);
1122 _remove_element (self, &self->overlay);
1123 _remove_element (self, &self->parser);
1124 _remove_element (self, &self->renderer);
1125 _remove_element (self, &self->pre_colorspace);
1126 _remove_element (self, &self->passthrough_identity);
1128 GST_DEBUG_OBJECT (self, "Trying factory '%s'",
1129 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1132 if (G_UNLIKELY ((is_renderer
1133 && !_create_element (self, &self->renderer, NULL, factory,
1134 "renderer", FALSE)) || (!is_renderer
1135 && !_create_element (self, &self->parser, NULL, factory,
1140 GstCaps *parser_caps;
1141 GList *overlay_factories, *k;
1143 if (!_setup_parser (self))
1146 /* Find our factories */
1147 src = gst_element_get_static_pad (self->parser, "src");
1148 parser_caps = gst_pad_query_caps (src, NULL);
1149 gst_object_unref (src);
1151 g_assert (parser_caps != NULL);
1153 g_mutex_lock (&self->factories_lock);
1154 gst_subtitle_overlay_update_factory_list (self);
1155 GST_DEBUG_OBJECT (self,
1156 "Searching overlay factories for caps %" GST_PTR_FORMAT, parser_caps);
1158 gst_subtitle_overlay_get_factories_for_caps (self->factories,
1160 g_mutex_unlock (&self->factories_lock);
1162 if (!overlay_factories) {
1163 GST_WARNING_OBJECT (self,
1164 "Found no suitable overlay factories for caps %" GST_PTR_FORMAT,
1166 gst_caps_unref (parser_caps);
1169 gst_caps_unref (parser_caps);
1171 /* Sort the factories by rank */
1173 g_list_sort (overlay_factories, (GCompareFunc) _sort_by_ranks);
1175 for (k = overlay_factories; k; k = k->next) {
1176 GstElementFactory *overlay_factory = k->data;
1178 GST_DEBUG_OBJECT (self, "Trying overlay factory '%s'",
1179 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1180 (overlay_factory))));
1182 /* Try this factory and link it, otherwise unlink everything
1183 * again and remove the overlay. Up to this point only the
1184 * parser was instantiated and setup, nothing was linked
1187 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1188 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad),
1190 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1192 self->silent_property = NULL;
1193 _remove_element (self, &self->post_colorspace);
1194 _remove_element (self, &self->overlay);
1195 _remove_element (self, &self->pre_colorspace);
1197 if (!_create_element (self, &self->overlay, NULL, overlay_factory,
1198 "overlay", FALSE)) {
1199 GST_DEBUG_OBJECT (self, "Could not create element");
1203 if (!_setup_renderer (self, self->overlay)) {
1204 GST_DEBUG_OBJECT (self, "Could not setup element");
1208 src = gst_element_get_static_pad (self->parser, "src");
1209 if (!_link_renderer (self, self->overlay, src)) {
1210 GST_DEBUG_OBJECT (self, "Could not link element");
1211 gst_object_unref (src);
1214 gst_object_unref (src);
1216 /* Everything done here, go out of loop */
1217 GST_DEBUG_OBJECT (self, "%s is a suitable element",
1218 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1219 (overlay_factory))));
1223 if (overlay_factories)
1224 gst_plugin_feature_list_free (overlay_factories);
1226 if (G_UNLIKELY (k == NULL)) {
1227 GST_WARNING_OBJECT (self, "Failed to find usable overlay factory");
1231 /* Now link subtitle sinkpad of the bin and the parser */
1232 sink = gst_element_get_static_pad (self->parser, "sink");
1233 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1234 (self->subtitle_sinkpad), sink)) {
1235 gst_object_unref (sink);
1238 gst_object_unref (sink);
1240 /* Everything done here, go out of loop */
1243 /* Is renderer factory */
1245 if (!_setup_renderer (self, self->renderer))
1248 /* subtitle_src==NULL means: use subtitle_sink ghostpad */
1249 if (!_link_renderer (self, self->renderer, NULL))
1252 /* Everything done here, go out of loop */
1257 if (G_UNLIKELY (l == NULL)) {
1258 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
1259 ("Failed to find any usable factories"));
1260 self->subtitle_error = TRUE;
1261 _setup_passthrough (self);
1262 do_async_done (self);
1266 GST_DEBUG_OBJECT (self, "Everything worked, unblocking pads");
1267 unblock_video (self);
1268 unblock_subtitle (self);
1269 do_async_done (self);
1273 gst_plugin_feature_list_free (factories);
1274 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1276 return GST_PAD_PROBE_OK;
1279 static GstStateChangeReturn
1280 gst_subtitle_overlay_change_state (GstElement * element,
1281 GstStateChange transition)
1283 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (element);
1284 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1286 switch (transition) {
1287 case GST_STATE_CHANGE_NULL_TO_READY:
1288 GST_DEBUG_OBJECT (self, "State change NULL->READY");
1289 g_mutex_lock (&self->factories_lock);
1290 if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self))) {
1291 g_mutex_unlock (&self->factories_lock);
1292 return GST_STATE_CHANGE_FAILURE;
1294 g_mutex_unlock (&self->factories_lock);
1296 GST_SUBTITLE_OVERLAY_LOCK (self);
1297 /* Set the internal pads to blocking */
1299 block_subtitle (self);
1300 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1302 case GST_STATE_CHANGE_READY_TO_PAUSED:
1303 GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
1305 self->fps_n = self->fps_d = 0;
1307 self->subtitle_flush = FALSE;
1308 self->subtitle_error = FALSE;
1310 self->downstream_chain_error = FALSE;
1312 do_async_start (self);
1313 ret = GST_STATE_CHANGE_ASYNC;
1316 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1317 GST_DEBUG_OBJECT (self, "State change PAUSED->PLAYING");
1323 GstStateChangeReturn bret;
1325 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1326 GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", bret);
1327 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
1329 else if (bret == GST_STATE_CHANGE_ASYNC)
1331 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
1332 do_async_done (self);
1337 switch (transition) {
1338 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1339 GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
1341 case GST_STATE_CHANGE_PAUSED_TO_READY:
1342 GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
1344 /* Set the pads back to blocking state */
1345 GST_SUBTITLE_OVERLAY_LOCK (self);
1347 block_subtitle (self);
1348 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1350 do_async_done (self);
1353 case GST_STATE_CHANGE_READY_TO_NULL:
1354 GST_DEBUG_OBJECT (self, "State change READY->NULL");
1356 GST_SUBTITLE_OVERLAY_LOCK (self);
1357 gst_caps_replace (&self->subcaps, NULL);
1359 /* Unlink ghost pads */
1360 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1361 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1362 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1366 unblock_video (self);
1367 unblock_subtitle (self);
1369 /* Remove elements */
1370 self->silent_property = NULL;
1371 _remove_element (self, &self->post_colorspace);
1372 _remove_element (self, &self->overlay);
1373 _remove_element (self, &self->parser);
1374 _remove_element (self, &self->renderer);
1375 _remove_element (self, &self->pre_colorspace);
1376 _remove_element (self, &self->passthrough_identity);
1377 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1388 gst_subtitle_overlay_handle_message (GstBin * bin, GstMessage * message)
1390 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (bin);
1392 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1393 GstObject *src = GST_MESSAGE_SRC (message);
1395 /* Convert error messages from the subtitle pipeline to
1396 * warnings and switch to passthrough mode */
1399 && gst_object_has_ancestor (src,
1400 GST_OBJECT_CAST (self->overlay))) || (self->parser
1401 && gst_object_has_ancestor (src,
1402 GST_OBJECT_CAST (self->parser))) || (self->renderer
1403 && gst_object_has_ancestor (src,
1404 GST_OBJECT_CAST (self->renderer))))) {
1406 gchar *debug = NULL;
1409 gst_message_parse_error (message, &err, &debug);
1410 GST_DEBUG_OBJECT (self,
1411 "Got error message from subtitle element %s: %s (%s)",
1412 GST_MESSAGE_SRC_NAME (message), GST_STR_NULL (err->message),
1413 GST_STR_NULL (debug));
1415 wmsg = gst_message_new_warning (src, err, debug);
1416 gst_message_unref (message);
1421 GST_SUBTITLE_OVERLAY_LOCK (self);
1422 self->subtitle_error = TRUE;
1424 block_subtitle (self);
1426 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1430 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1434 gst_subtitle_overlay_get_property (GObject * object, guint prop_id,
1435 GValue * value, GParamSpec * pspec)
1437 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1441 g_value_set_boolean (value, self->silent);
1443 case PROP_FONT_DESC:
1444 GST_SUBTITLE_OVERLAY_LOCK (self);
1445 g_value_set_string (value, self->font_desc);
1446 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1448 case PROP_SUBTITLE_ENCODING:
1449 GST_SUBTITLE_OVERLAY_LOCK (self);
1450 g_value_set_string (value, self->encoding);
1451 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1454 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1460 gst_subtitle_overlay_set_property (GObject * object, guint prop_id,
1461 const GValue * value, GParamSpec * pspec)
1463 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1467 GST_SUBTITLE_OVERLAY_LOCK (self);
1468 self->silent = g_value_get_boolean (value);
1469 if (self->silent_property) {
1470 gboolean silent = self->silent;
1472 if (self->silent_property_invert)
1476 g_object_set (self->overlay, self->silent_property, silent, NULL);
1477 else if (self->renderer)
1478 g_object_set (self->renderer, self->silent_property, silent, NULL);
1480 block_subtitle (self);
1483 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1485 case PROP_FONT_DESC:
1486 GST_SUBTITLE_OVERLAY_LOCK (self);
1487 g_free (self->font_desc);
1488 self->font_desc = g_value_dup_string (value);
1490 && _has_property_with_type (G_OBJECT (self->overlay), "font-desc",
1492 g_object_set (self->overlay, "font-desc", self->font_desc, NULL);
1493 else if (self->renderer
1494 && _has_property_with_type (G_OBJECT (self->renderer), "font-desc",
1496 g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1497 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1499 case PROP_SUBTITLE_ENCODING:
1500 GST_SUBTITLE_OVERLAY_LOCK (self);
1501 g_free (self->encoding);
1502 self->encoding = g_value_dup_string (value);
1504 && _has_property_with_type (G_OBJECT (self->renderer),
1505 "subtitle-encoding", G_TYPE_STRING))
1506 g_object_set (self->renderer, "subtitle-encoding", self->encoding,
1509 && _has_property_with_type (G_OBJECT (self->parser),
1510 "subtitle-encoding", G_TYPE_STRING))
1511 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
1512 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1515 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1521 gst_subtitle_overlay_class_init (GstSubtitleOverlayClass * klass)
1523 GObjectClass *gobject_class = (GObjectClass *) klass;
1524 GstElementClass *element_class = (GstElementClass *) klass;
1525 GstBinClass *bin_class = (GstBinClass *) klass;
1527 gobject_class->finalize = gst_subtitle_overlay_finalize;
1528 gobject_class->set_property = gst_subtitle_overlay_set_property;
1529 gobject_class->get_property = gst_subtitle_overlay_get_property;
1531 g_object_class_install_property (gobject_class, PROP_SILENT,
1532 g_param_spec_boolean ("silent",
1534 "Whether to show subtitles", FALSE,
1535 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1537 g_object_class_install_property (gobject_class, PROP_FONT_DESC,
1538 g_param_spec_string ("font-desc",
1539 "Subtitle font description",
1540 "Pango font description of font "
1541 "to be used for subtitle rendering", NULL,
1542 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1544 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
1545 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
1546 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
1547 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
1548 "be checked for an encoding to use. If that is not set either, "
1549 "ISO-8859-15 will be assumed.", NULL,
1550 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1552 gst_element_class_add_pad_template (element_class,
1553 gst_static_pad_template_get (&srctemplate));
1555 gst_element_class_add_pad_template (element_class,
1556 gst_static_pad_template_get (&video_sinktemplate));
1557 gst_element_class_add_pad_template (element_class,
1558 gst_static_pad_template_get (&subtitle_sinktemplate));
1560 gst_element_class_set_static_metadata (element_class, "Subtitle Overlay",
1561 "Video/Overlay/Subtitle",
1562 "Overlays a video stream with subtitles",
1563 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1565 element_class->change_state =
1566 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_change_state);
1568 bin_class->handle_message =
1569 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_handle_message);
1572 static GstFlowReturn
1573 gst_subtitle_overlay_src_proxy_chain (GstPad * proxypad, GstObject * parent,
1577 GstSubtitleOverlay *self;
1580 ghostpad = GST_PAD_CAST (parent);
1581 if (G_UNLIKELY (!ghostpad)) {
1582 gst_buffer_unref (buffer);
1583 return GST_FLOW_ERROR;
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;
1592 ret = gst_proxy_pad_chain_default (proxypad, parent, buffer);
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;
1600 gst_object_unref (self);
1606 gst_subtitle_overlay_src_proxy_event (GstPad * proxypad, GstObject * parent,
1609 GstPad *ghostpad = NULL;
1610 GstSubtitleOverlay *self = NULL;
1611 gboolean ret = FALSE;
1612 const GstStructure *s;
1614 ghostpad = GST_PAD_CAST (parent);
1615 if (G_UNLIKELY (!ghostpad))
1617 self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1618 if (G_UNLIKELY (!self || self->srcpad != ghostpad))
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,
1624 "Dropping event with marker: %" GST_PTR_FORMAT,
1625 gst_event_get_structure (event));
1626 gst_event_unref (event);
1630 ret = gst_pad_event_default (proxypad, parent, event);
1636 gst_event_unref (event);
1638 gst_object_unref (self);
1644 gst_subtitle_overlay_video_sink_setcaps (GstSubtitleOverlay * self,
1648 gboolean ret = TRUE;
1651 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1653 if (!gst_video_info_from_caps (&info, caps)) {
1654 GST_ERROR_OBJECT (self, "Failed to parse caps");
1656 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1660 target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->video_sinkpad));
1662 GST_SUBTITLE_OVERLAY_LOCK (self);
1664 if (!target || !gst_pad_query_accept_caps (target, caps)) {
1665 GST_DEBUG_OBJECT (target, "Target did not accept caps -- reconfiguring");
1667 block_subtitle (self);
1671 if (self->fps_n != info.fps_n || self->fps_d != info.fps_d) {
1672 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", info.fps_n, info.fps_d);
1673 self->fps_n = info.fps_n;
1674 self->fps_d = info.fps_d;
1675 gst_subtitle_overlay_set_fps (self);
1677 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1680 gst_object_unref (target);
1688 gst_subtitle_overlay_video_sink_event (GstPad * pad, GstObject * parent,
1691 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1694 switch (GST_EVENT_TYPE (event)) {
1695 case GST_EVENT_CAPS:
1699 gst_event_parse_caps (event, &caps);
1700 ret = gst_subtitle_overlay_video_sink_setcaps (self, caps);
1709 ret = gst_pad_event_default (pad, parent, gst_event_ref (event));
1712 gst_event_unref (event);
1717 static GstFlowReturn
1718 gst_subtitle_overlay_video_sink_chain (GstPad * pad, GstObject * parent,
1721 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1722 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1724 if (G_UNLIKELY (self->downstream_chain_error) || self->passthrough_identity) {
1726 } else if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1727 GST_DEBUG_OBJECT (self, "Subtitle renderer produced chain error: %s",
1728 gst_flow_get_name (ret));
1729 GST_SUBTITLE_OVERLAY_LOCK (self);
1730 self->subtitle_error = TRUE;
1731 block_subtitle (self);
1733 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1741 static GstFlowReturn
1742 gst_subtitle_overlay_subtitle_sink_chain (GstPad * pad, GstObject * parent,
1745 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1747 if (self->subtitle_error) {
1748 gst_buffer_unref (buffer);
1751 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1753 if (IS_SUBTITLE_CHAIN_IGNORE_ERROR (ret)) {
1754 GST_DEBUG_OBJECT (self, "Subtitle chain error: %s",
1755 gst_flow_get_name (ret));
1756 GST_SUBTITLE_OVERLAY_LOCK (self);
1757 self->subtitle_error = TRUE;
1758 block_subtitle (self);
1760 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1770 gst_subtitle_overlay_subtitle_sink_getcaps (GstPad * pad, GstCaps * filter)
1775 ret = gst_caps_ref (filter);
1777 ret = gst_caps_new_any ();
1783 gst_subtitle_overlay_subtitle_sink_setcaps (GstSubtitleOverlay * self,
1786 gboolean ret = TRUE;
1787 GstPad *target = NULL;;
1789 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1792 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1794 GST_SUBTITLE_OVERLAY_LOCK (self);
1795 gst_caps_replace (&self->subcaps, caps);
1797 if (target && gst_pad_query_accept_caps (target, caps)) {
1798 GST_DEBUG_OBJECT (self, "Target accepts caps");
1799 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1803 GST_DEBUG_OBJECT (self, "Target did not accept caps");
1805 self->subtitle_error = FALSE;
1806 block_subtitle (self);
1808 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1812 gst_object_unref (target);
1817 static GstPadLinkReturn
1818 gst_subtitle_overlay_subtitle_sink_link (GstPad * pad, GstObject * parent,
1821 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1824 GST_DEBUG_OBJECT (pad, "Linking pad to peer %" GST_PTR_FORMAT, peer);
1826 caps = gst_pad_get_current_caps (peer);
1828 caps = gst_pad_query_caps (peer, NULL);
1829 if (!gst_caps_is_fixed (caps)) {
1830 gst_caps_unref (caps);
1836 GST_SUBTITLE_OVERLAY_LOCK (self);
1837 GST_DEBUG_OBJECT (pad, "Have fixed peer caps: %" GST_PTR_FORMAT, caps);
1838 gst_caps_replace (&self->subcaps, caps);
1840 self->subtitle_error = FALSE;
1842 block_subtitle (self);
1844 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1845 gst_caps_unref (caps);
1848 return GST_PAD_LINK_OK;
1852 gst_subtitle_overlay_subtitle_sink_unlink (GstPad * pad, GstObject * parent)
1854 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1856 /* FIXME: Can't use gst_pad_get_parent() here because this is called with
1857 * the object lock from state changes
1860 GST_DEBUG_OBJECT (pad, "Pad unlinking");
1861 gst_caps_replace (&self->subcaps, NULL);
1863 GST_SUBTITLE_OVERLAY_LOCK (self);
1864 self->subtitle_error = FALSE;
1866 block_subtitle (self);
1868 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1872 gst_subtitle_overlay_subtitle_sink_event (GstPad * pad, GstObject * parent,
1875 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1878 GST_DEBUG_OBJECT (pad, "Got event %" GST_PTR_FORMAT, event);
1880 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB &&
1881 gst_event_has_name (event, "playsink-custom-subtitle-flush")) {
1882 GST_DEBUG_OBJECT (pad, "Custom subtitle flush event");
1883 GST_SUBTITLE_OVERLAY_LOCK (self);
1884 self->subtitle_flush = TRUE;
1885 self->subtitle_error = FALSE;
1886 block_subtitle (self);
1888 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1890 gst_event_unref (event);
1896 switch (GST_EVENT_TYPE (event)) {
1897 case GST_EVENT_CAPS:
1901 gst_event_parse_caps (event, &caps);
1902 ret = gst_subtitle_overlay_subtitle_sink_setcaps (self, caps);
1907 case GST_EVENT_FLUSH_STOP:
1908 case GST_EVENT_FLUSH_START:
1909 case GST_EVENT_SEGMENT:
1912 GstStructure *structure;
1914 /* Add our event marker to make sure no events from here go ever outside
1915 * the element, they're only interesting for our internal elements */
1916 event = GST_EVENT_CAST (gst_event_make_writable (event));
1917 structure = gst_event_writable_structure (event);
1919 gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
1920 G_TYPE_BOOLEAN, TRUE, NULL);
1927 ret = gst_pad_event_default (pad, parent, gst_event_ref (event));
1929 gst_event_unref (event);
1936 gst_subtitle_overlay_subtitle_sink_query (GstPad * pad, GstObject * parent,
1941 switch (GST_QUERY_TYPE (query)) {
1942 case GST_QUERY_ACCEPT_CAPS:
1944 gst_query_set_accept_caps_result (query, TRUE);
1948 case GST_QUERY_CAPS:
1950 GstCaps *filter, *caps;
1952 gst_query_parse_caps (query, &filter);
1953 caps = gst_subtitle_overlay_subtitle_sink_getcaps (pad, filter);
1954 gst_query_set_caps_result (query, caps);
1955 gst_caps_unref (caps);
1960 ret = gst_pad_query_default (pad, parent, query);
1968 gst_subtitle_overlay_init (GstSubtitleOverlay * self)
1970 GstPadTemplate *templ;
1971 GstPad *proxypad = NULL;
1973 g_mutex_init (&self->lock);
1974 g_mutex_init (&self->factories_lock);
1976 templ = gst_static_pad_template_get (&srctemplate);
1977 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
1978 gst_object_unref (templ);
1981 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->srcpad)));
1982 gst_pad_set_event_function (proxypad,
1983 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_event));
1984 gst_pad_set_chain_function (proxypad,
1985 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_chain));
1986 gst_object_unref (proxypad);
1988 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
1990 templ = gst_static_pad_template_get (&video_sinktemplate);
1991 self->video_sinkpad =
1992 gst_ghost_pad_new_no_target_from_template ("video_sink", templ);
1993 gst_object_unref (templ);
1994 gst_pad_set_event_function (self->video_sinkpad,
1995 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_event));
1996 gst_pad_set_chain_function (self->video_sinkpad,
1997 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_chain));
2000 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2001 (self->video_sinkpad)));
2002 self->video_block_pad = proxypad;
2003 gst_object_unref (proxypad);
2004 gst_element_add_pad (GST_ELEMENT_CAST (self), self->video_sinkpad);
2006 templ = gst_static_pad_template_get (&subtitle_sinktemplate);
2007 self->subtitle_sinkpad =
2008 gst_ghost_pad_new_no_target_from_template ("subtitle_sink", templ);
2009 gst_object_unref (templ);
2010 gst_pad_set_link_function (self->subtitle_sinkpad,
2011 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_link));
2012 gst_pad_set_unlink_function (self->subtitle_sinkpad,
2013 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_unlink));
2014 gst_pad_set_event_function (self->subtitle_sinkpad,
2015 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_event));
2016 gst_pad_set_query_function (self->subtitle_sinkpad,
2017 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_query));
2018 gst_pad_set_chain_function (self->subtitle_sinkpad,
2019 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_chain));
2022 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2023 (self->subtitle_sinkpad)));
2024 self->subtitle_block_pad = proxypad;
2025 gst_object_unref (proxypad);
2027 gst_element_add_pad (GST_ELEMENT_CAST (self), self->subtitle_sinkpad);
2034 gst_subtitle_overlay_plugin_init (GstPlugin * plugin)
2036 GST_DEBUG_CATEGORY_INIT (subtitle_overlay_debug, "subtitleoverlay", 0,
2037 "Subtitle Overlay");
2039 _subtitle_overlay_event_marker_id =
2040 g_quark_from_static_string ("gst-subtitle-overlay-event-marker");
2042 return gst_element_register (plugin, "subtitleoverlay", GST_RANK_NONE,
2043 GST_TYPE_SUBTITLE_OVERLAY);