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"
223 static inline gboolean
224 _is_raw_video (GstStructure * s)
228 name = gst_structure_get_name (s);
230 if (g_str_equal (name, "video/x-raw"))
236 _is_video_pad (GstPad * pad, gboolean * hw_accelerated)
238 GstPad *peer = gst_pad_get_peer (pad);
244 caps = gst_pad_get_current_caps (peer);
246 caps = gst_pad_query_caps (peer, NULL);
248 gst_object_unref (peer);
250 caps = gst_pad_query_caps (pad, NULL);
253 name = gst_structure_get_name (gst_caps_get_structure (caps, 0));
254 if (g_str_equal (name, "video/x-raw")) {
257 *hw_accelerated = FALSE;
259 } else if (g_str_has_prefix (name, "video/x-surface")) {
262 *hw_accelerated = TRUE;
267 *hw_accelerated = FALSE;
270 gst_caps_unref (caps);
276 _get_sub_caps (GstElementFactory * factory)
278 const GList *templates;
280 gboolean is_parser = _is_parser (factory);
282 templates = gst_element_factory_get_static_pad_templates (factory);
283 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
284 GstStaticPadTemplate *templ = walk->data;
286 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
287 gboolean found = FALSE;
294 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
295 if (strcmp (templ->name_template, _sub_pad_names[i]) == 0) {
302 return gst_static_caps_get (&templ->static_caps);
309 _factory_filter (GstPluginFeature * feature, GstCaps ** subcaps)
311 GstElementFactory *factory;
314 const GList *templates;
316 gboolean is_renderer;
317 GstCaps *templ_caps = NULL;
318 gboolean have_video_sink = FALSE;
320 /* we only care about element factories */
321 if (!GST_IS_ELEMENT_FACTORY (feature))
324 factory = GST_ELEMENT_FACTORY_CAST (feature);
326 /* only select elements with autoplugging rank or textoverlay */
327 name = gst_plugin_feature_get_name (feature);
328 rank = gst_plugin_feature_get_rank (feature);
329 if (strcmp ("textoverlay", name) != 0 && rank < GST_RANK_MARGINAL)
332 /* Check if it's a renderer or a parser */
333 if (_is_renderer (factory)) {
335 } else if (_is_parser (factory)) {
341 /* Check if there's a video sink in case of a renderer */
343 templates = gst_element_factory_get_static_pad_templates (factory);
344 for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
345 GstStaticPadTemplate *templ = walk->data;
347 /* we only care about the always-sink templates */
348 if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
349 if (strcmp (templ->name_template, "video") == 0 ||
350 strcmp (templ->name_template, "video_sink") == 0) {
351 have_video_sink = TRUE;
356 templ_caps = _get_sub_caps (factory);
358 if (is_renderer && have_video_sink && templ_caps) {
359 GST_DEBUG ("Found renderer element %s (%s) with caps %" GST_PTR_FORMAT,
360 gst_element_factory_get_metadata (factory,
361 GST_ELEMENT_METADATA_LONGNAME),
362 gst_plugin_feature_get_name (feature), templ_caps);
363 *subcaps = gst_caps_merge (*subcaps, templ_caps);
365 } else if (!is_renderer && !have_video_sink && templ_caps) {
366 GST_DEBUG ("Found parser element %s (%s) with caps %" GST_PTR_FORMAT,
367 gst_element_factory_get_metadata (factory,
368 GST_ELEMENT_METADATA_LONGNAME),
369 gst_plugin_feature_get_name (feature), templ_caps);
370 *subcaps = gst_caps_merge (*subcaps, templ_caps);
374 gst_caps_unref (templ_caps);
379 /* Call with factories_lock! */
381 gst_subtitle_overlay_update_factory_list (GstSubtitleOverlay * self)
383 GstRegistry *registry;
386 registry = gst_registry_get ();
387 cookie = gst_registry_get_feature_list_cookie (registry);
388 if (!self->factories || self->factories_cookie != cookie) {
392 subcaps = gst_caps_new_empty ();
394 factories = gst_registry_feature_filter (registry,
395 (GstPluginFeatureFilter) _factory_filter, FALSE, &subcaps);
396 GST_DEBUG_OBJECT (self, "Created factory caps: %" GST_PTR_FORMAT, subcaps);
397 gst_caps_replace (&self->factory_caps, subcaps);
398 gst_caps_unref (subcaps);
400 gst_plugin_feature_list_free (self->factories);
401 self->factories = factories;
402 self->factories_cookie = cookie;
405 return (self->factories != NULL);
408 G_LOCK_DEFINE_STATIC (_factory_caps);
409 static GstCaps *_factory_caps = NULL;
410 static guint32 _factory_caps_cookie = 0;
413 gst_subtitle_overlay_create_factory_caps (void)
415 GstRegistry *registry;
417 GstCaps *subcaps = NULL;
420 registry = gst_registry_get ();
421 cookie = gst_registry_get_feature_list_cookie (registry);
422 G_LOCK (_factory_caps);
423 if (!_factory_caps || _factory_caps_cookie != cookie) {
425 gst_caps_unref (_factory_caps);
426 _factory_caps = gst_caps_new_empty ();
428 factories = gst_registry_feature_filter (registry,
429 (GstPluginFeatureFilter) _factory_filter, FALSE, &_factory_caps);
430 GST_DEBUG ("Created factory caps: %" GST_PTR_FORMAT, _factory_caps);
431 gst_plugin_feature_list_free (factories);
432 _factory_caps_cookie = cookie;
434 subcaps = gst_caps_ref (_factory_caps);
435 G_UNLOCK (_factory_caps);
441 check_factory_for_caps (GstElementFactory * factory, const GstCaps * caps)
443 GstCaps *fcaps = _get_sub_caps (factory);
444 gboolean ret = (fcaps) ? gst_caps_can_intersect (fcaps, caps) : FALSE;
447 gst_caps_unref (fcaps);
450 gst_object_ref (factory);
455 gst_subtitle_overlay_get_factories_for_caps (const GList * list,
456 const GstCaps * caps)
458 const GList *walk = list;
459 GList *result = NULL;
462 GstElementFactory *factory = walk->data;
464 walk = g_list_next (walk);
466 if (check_factory_for_caps (factory, caps)) {
467 result = g_list_prepend (result, factory);
475 _sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
478 const gchar *rname1, *rname2;
480 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
484 /* If the ranks are the same sort by name to get deterministic results */
485 rname1 = gst_plugin_feature_get_name (f1);
486 rname2 = gst_plugin_feature_get_name (f2);
488 diff = strcmp (rname1, rname2);
494 _get_sub_pad (GstElement * element)
499 for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
500 pad = gst_element_get_static_pad (element, _sub_pad_names[i]);
508 _get_video_pad (GstElement * element)
510 static const gchar *const pad_names[] = { "video", "video_sink" };
514 for (i = 0; i < G_N_ELEMENTS (pad_names); i++) {
515 pad = gst_element_get_static_pad (element, pad_names[i]);
523 _create_element (GstSubtitleOverlay * self, GstElement ** element,
524 const gchar * factory_name, GstElementFactory * factory,
525 const gchar * element_name, gboolean mandatory)
529 g_assert (!factory || !factory_name);
532 elt = gst_element_factory_make (factory_name, element_name);
535 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
536 elt = gst_element_factory_create (factory, element_name);
539 if (G_UNLIKELY (!elt)) {
544 gst_missing_element_message_new (GST_ELEMENT_CAST (self),
546 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
549 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
550 ("no '%s' plugin found", factory_name));
552 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
553 ("no '%s' plugin found", factory_name));
556 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
557 ("can't instantiate '%s'", factory_name));
559 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
560 ("can't instantiate '%s'", factory_name));
567 if (G_UNLIKELY (gst_element_set_state (elt,
568 GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS)) {
569 gst_object_unref (elt);
571 GST_ELEMENT_ERROR (self, CORE, STATE_CHANGE, (NULL),
572 ("failed to set '%s' to READY", factory_name));
574 GST_WARNING_OBJECT (self, "Failed to set '%s' to READY", factory_name);
579 if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (self), gst_object_ref (elt)))) {
580 gst_element_set_state (elt, GST_STATE_NULL);
581 gst_object_unref (elt);
583 GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
584 ("failed to add '%s' to subtitleoverlay", factory_name));
586 GST_WARNING_OBJECT (self, "Failed to add '%s' to subtitleoverlay",
592 gst_element_sync_state_with_parent (elt);
598 _remove_element (GstSubtitleOverlay * self, GstElement ** element)
601 gst_bin_remove (GST_BIN_CAST (self), *element);
602 gst_element_set_state (*element, GST_STATE_NULL);
603 gst_object_unref (*element);
609 _pad_query_convert_to_time (GstPad * pad, GstFormat src_format, gint64 src_val,
612 GstFormat dest_format;
614 if (src_val == GST_CLOCK_TIME_NONE) {
615 *dest_val = GST_CLOCK_TIME_NONE;
619 dest_format = GST_FORMAT_TIME;
620 return gst_pad_query_convert (pad, src_format, src_val, dest_format,
625 _generate_update_segment_event (GstPad * pad, GstSegment * segment,
629 GstStructure *structure;
630 GstSegment newsegment;
631 gboolean use_newseg = FALSE;
632 gint64 start, stop, base, time, position, duration;
634 /* always push newsegment with format TIME */
635 if (segment->format != GST_FORMAT_TIME) {
639 peer = gst_pad_get_peer (pad);
641 res = _pad_query_convert_to_time (peer, segment->format,
642 segment->start, &start);
643 res = res && _pad_query_convert_to_time (peer, segment->format,
644 segment->stop, &stop);
645 res = res && _pad_query_convert_to_time (peer, segment->format,
646 segment->time, &time);
647 res = res && _pad_query_convert_to_time (peer, segment->format,
648 segment->base, &base);
649 res = res && _pad_query_convert_to_time (peer, segment->format,
650 segment->position, &position);
651 res = res && _pad_query_convert_to_time (peer, segment->format,
652 segment->duration, &duration);
655 stop = GST_CLOCK_TIME_NONE;
658 duration = GST_CLOCK_TIME_NONE;
662 gst_segment_init (&newsegment, GST_FORMAT_TIME);
663 newsegment.rate = segment->rate;
664 newsegment.applied_rate = segment->applied_rate;
665 newsegment.start = start;
666 newsegment.stop = stop;
667 newsegment.time = time;
668 newsegment.base = base;
669 newsegment.position = position;
670 newsegment.duration = duration;
673 gst_object_unref (peer);
678 event = gst_event_new_segment (&newsegment);
680 event = gst_event_new_segment (segment);
682 structure = gst_event_writable_structure (event);
683 gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
684 G_TYPE_BOOLEAN, TRUE, NULL);
689 _setup_passthrough (GstSubtitleOverlay * self)
692 GstElement *identity;
694 GST_DEBUG_OBJECT (self, "Doing video passthrough");
696 if (self->passthrough_identity) {
697 GST_DEBUG_OBJECT (self, "Already in passthrough mode");
701 /* Unlink & destroy everything */
702 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
703 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
704 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad), NULL);
705 self->silent_property = NULL;
706 _remove_element (self, &self->post_colorspace);
707 _remove_element (self, &self->overlay);
708 _remove_element (self, &self->parser);
709 _remove_element (self, &self->renderer);
710 _remove_element (self, &self->pre_colorspace);
711 _remove_element (self, &self->passthrough_identity);
713 if (G_UNLIKELY (!_create_element (self, &self->passthrough_identity,
714 "identity", NULL, "passthrough-identity", TRUE))) {
718 identity = self->passthrough_identity;
719 g_object_set (G_OBJECT (identity), "silent", TRUE, "signal-handoffs", FALSE,
722 /* Set src ghostpad target */
723 src = gst_element_get_static_pad (self->passthrough_identity, "src");
724 if (G_UNLIKELY (!src)) {
725 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
726 ("Failed to get srcpad from identity"));
730 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
732 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
733 ("Failed to set srcpad target"));
734 gst_object_unref (src);
737 gst_object_unref (src);
739 sink = gst_element_get_static_pad (self->passthrough_identity, "sink");
740 if (G_UNLIKELY (!sink)) {
741 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
742 ("Failed to get sinkpad from identity"));
746 /* Send segment to the identity. This is dropped because identity
747 * is not linked downstream yet */
748 if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
751 _generate_update_segment_event (sink, &self->video_segment, &event1);
752 GST_DEBUG_OBJECT (self,
753 "Pushing video segment event: %" GST_PTR_FORMAT, event1);
754 gst_pad_send_event (sink, event1);
757 /* Link sink ghostpads to identity */
758 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
759 (self->video_sinkpad), sink))) {
760 GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
761 ("Failed to set video sinkpad target"));
762 gst_object_unref (sink);
765 gst_object_unref (sink);
767 GST_DEBUG_OBJECT (self, "Video passthrough setup successfully");
771 unblock_video (self);
772 unblock_subtitle (self);
777 /* Must be called with subtitleoverlay lock! */
779 _has_property_with_type (GObject * object, const gchar * property, GType type)
781 GObjectClass *gobject_class;
784 gobject_class = G_OBJECT_GET_CLASS (object);
785 pspec = g_object_class_find_property (gobject_class, property);
786 return (pspec && pspec->value_type == type);
790 gst_subtitle_overlay_set_fps (GstSubtitleOverlay * self)
792 if (!self->parser || self->fps_d == 0)
795 if (!_has_property_with_type (G_OBJECT (self->parser), "video-fps",
799 GST_DEBUG_OBJECT (self, "Updating video-fps property in parser");
800 g_object_set (self->parser, "video-fps", self->fps_n, self->fps_d, NULL);
804 _get_silent_property (GstElement * element, gboolean * invert)
815 for (i = 0; i < G_N_ELEMENTS (properties); i++) {
816 if (_has_property_with_type (G_OBJECT (element), properties[i].name,
818 *invert = properties[i].invert;
819 return properties[i].name;
826 _setup_parser (GstSubtitleOverlay * self)
830 /* Try to get the latest video framerate */
831 video_peer = gst_pad_get_peer (self->video_sinkpad);
836 video_caps = gst_pad_get_current_caps (video_peer);
838 video_caps = gst_pad_query_caps (video_peer, NULL);
839 if (!gst_caps_is_fixed (video_caps)) {
840 gst_caps_unref (video_caps);
846 GstStructure *st = gst_caps_get_structure (video_caps, 0);
847 if (gst_structure_get_fraction (st, "framerate", &fps_n, &fps_d)) {
848 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
855 gst_caps_unref (video_caps);
856 gst_object_unref (video_peer);
859 if (_has_property_with_type (G_OBJECT (self->parser), "subtitle-encoding",
861 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
863 /* Try to set video fps on the parser */
864 gst_subtitle_overlay_set_fps (self);
871 _setup_renderer (GstSubtitleOverlay * self, GstElement * renderer)
873 GstElementFactory *factory = gst_element_get_factory (renderer);
875 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
877 if (strcmp (name, "textoverlay") == 0) {
878 /* Set some textoverlay specific properties */
879 gst_util_set_object_arg (G_OBJECT (renderer), "halignment", "center");
880 gst_util_set_object_arg (G_OBJECT (renderer), "valignment", "bottom");
881 g_object_set (G_OBJECT (renderer), "wait-text", FALSE, NULL);
883 g_object_set (G_OBJECT (renderer), "font-desc", self->font_desc, NULL);
884 self->silent_property = "silent";
885 self->silent_property_invert = FALSE;
887 self->silent_property =
888 _get_silent_property (renderer, &self->silent_property_invert);
889 if (_has_property_with_type (G_OBJECT (renderer), "subtitle-encoding",
891 g_object_set (renderer, "subtitle-encoding", self->encoding, NULL);
892 if (_has_property_with_type (G_OBJECT (renderer), "font-desc",
894 g_object_set (renderer, "font-desc", self->font_desc, NULL);
900 /* subtitle_src==NULL means: use subtitle_sink ghostpad */
902 _link_renderer (GstSubtitleOverlay * self, GstElement * renderer,
903 GstPad * subtitle_src)
906 gboolean is_video, is_hw;
908 is_video = _is_video_pad (self->video_sinkpad, &is_hw);
911 gboolean render_is_hw;
913 /* First check that renderer also supports the video format */
914 sink = _get_video_pad (renderer);
915 if (G_UNLIKELY (!sink)) {
916 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
920 if (is_video != _is_video_pad (sink, &render_is_hw) ||
921 is_hw != render_is_hw) {
922 GST_DEBUG_OBJECT (self, "Renderer doesn't support %s video",
923 is_hw ? "surface" : "raw");
924 gst_object_unref (sink);
927 gst_object_unref (sink);
930 /* First link everything internally */
931 if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
932 COLORSPACE, NULL, "post-colorspace", FALSE))) {
935 src = gst_element_get_static_pad (renderer, "src");
936 if (G_UNLIKELY (!src)) {
937 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
941 sink = gst_element_get_static_pad (self->post_colorspace, "sink");
942 if (G_UNLIKELY (!sink)) {
943 GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
944 gst_object_unref (src);
948 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
949 GST_WARNING_OBJECT (self, "Can't link renderer with " COLORSPACE);
950 gst_object_unref (src);
951 gst_object_unref (sink);
954 gst_object_unref (src);
955 gst_object_unref (sink);
957 if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
958 COLORSPACE, NULL, "pre-colorspace", FALSE))) {
962 sink = _get_video_pad (renderer);
963 if (G_UNLIKELY (!sink)) {
964 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
968 src = gst_element_get_static_pad (self->pre_colorspace, "src");
969 if (G_UNLIKELY (!src)) {
970 GST_WARNING_OBJECT (self, "Can't get srcpad from " COLORSPACE);
971 gst_object_unref (sink);
975 if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
976 GST_WARNING_OBJECT (self, "Can't link " COLORSPACE " to renderer");
977 gst_object_unref (src);
978 gst_object_unref (sink);
981 gst_object_unref (src);
982 gst_object_unref (sink);
984 /* Set src ghostpad target */
985 src = gst_element_get_static_pad (self->post_colorspace, "src");
986 if (G_UNLIKELY (!src)) {
987 GST_WARNING_OBJECT (self, "Can't get src pad from " COLORSPACE);
991 /* Set src ghostpad target in the harware accelerated case */
993 src = gst_element_get_static_pad (renderer, "src");
994 if (G_UNLIKELY (!src)) {
995 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
999 } else { /* No video pad */
1000 GstCaps *allowed_caps, *video_caps = NULL;
1002 gboolean can_intersect = FALSE;
1004 video_peer = gst_pad_get_peer (self->video_sinkpad);
1006 video_caps = gst_pad_get_current_caps (video_peer);
1008 video_caps = gst_pad_query_caps (video_peer, NULL);
1010 gst_object_unref (video_peer);
1013 sink = _get_video_pad (renderer);
1014 if (G_UNLIKELY (!sink)) {
1015 GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
1018 allowed_caps = gst_pad_query_caps (sink, NULL);
1019 gst_object_unref (sink);
1021 if (allowed_caps && video_caps)
1022 can_intersect = gst_caps_can_intersect (allowed_caps, video_caps);
1025 gst_caps_unref (allowed_caps);
1028 gst_caps_unref (video_caps);
1030 if (G_UNLIKELY (!can_intersect)) {
1031 GST_WARNING_OBJECT (self, "Renderer with custom caps is not "
1032 "compatible with video stream");
1036 src = gst_element_get_static_pad (renderer, "src");
1037 if (G_UNLIKELY (!src)) {
1038 GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
1043 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1044 (self->srcpad), src))) {
1045 GST_WARNING_OBJECT (self, "Can't set srcpad target");
1046 gst_object_unref (src);
1049 gst_object_unref (src);
1051 /* Set the sink ghostpad targets */
1052 if (self->pre_colorspace) {
1053 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1054 if (G_UNLIKELY (!sink)) {
1055 GST_WARNING_OBJECT (self, "Can't get sink pad from " COLORSPACE);
1059 sink = _get_video_pad (renderer);
1060 if (G_UNLIKELY (!sink)) {
1061 GST_WARNING_OBJECT (self, "Can't get sink pad from %" GST_PTR_FORMAT,
1067 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1068 (self->video_sinkpad), sink))) {
1069 GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
1070 gst_object_unref (sink);
1073 gst_object_unref (sink);
1075 sink = _get_sub_pad (renderer);
1076 if (G_UNLIKELY (!sink)) {
1077 GST_WARNING_OBJECT (self, "Failed to get subpad");
1082 if (G_UNLIKELY (gst_pad_link (subtitle_src, sink) != GST_PAD_LINK_OK)) {
1083 GST_WARNING_OBJECT (self, "Failed to link subtitle srcpad with renderer");
1084 gst_object_unref (sink);
1088 if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1089 (self->subtitle_sinkpad), sink))) {
1090 GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
1091 gst_object_unref (sink);
1095 gst_object_unref (sink);
1100 static GstPadProbeReturn
1101 _pad_blocked_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
1103 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (user_data);
1105 GList *l, *factories = NULL;
1107 GST_DEBUG_OBJECT (pad, "Pad blocked");
1109 GST_SUBTITLE_OVERLAY_LOCK (self);
1110 if (pad == self->video_block_pad)
1111 self->video_sink_blocked = TRUE;
1112 else if (pad == self->subtitle_block_pad)
1113 self->subtitle_sink_blocked = TRUE;
1115 /* Now either both or the video sink are blocked */
1117 /* Get current subtitle caps */
1118 subcaps = self->subcaps;
1122 peer = gst_pad_get_peer (self->subtitle_sinkpad);
1124 subcaps = gst_pad_get_current_caps (peer);
1126 subcaps = gst_pad_query_caps (peer, NULL);
1127 if (!gst_caps_is_fixed (subcaps)) {
1128 gst_caps_unref (subcaps);
1132 gst_object_unref (peer);
1134 gst_caps_replace (&self->subcaps, subcaps);
1136 gst_caps_unref (subcaps);
1138 GST_DEBUG_OBJECT (self, "Current subtitle caps: %" GST_PTR_FORMAT, subcaps);
1140 /* If there are no subcaps but the subtitle sink is blocked upstream
1141 * must behave wrong as there are no fixed caps set for the first
1142 * buffer or in-order event */
1143 if (G_UNLIKELY (!subcaps && self->subtitle_sink_blocked)) {
1144 GST_ELEMENT_WARNING (self, CORE, NEGOTIATION, (NULL),
1145 ("Subtitle sink is blocked but we have no subtitle caps"));
1149 if (self->subtitle_error || (self->silent && !self->silent_property)) {
1150 _setup_passthrough (self);
1151 do_async_done (self);
1155 /* Now do something with the caps */
1156 if (subcaps && !self->subtitle_flush) {
1158 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1160 if (target && gst_pad_query_accept_caps (target, subcaps)) {
1161 GST_DEBUG_OBJECT (pad, "Target accepts caps");
1163 gst_object_unref (target);
1166 unblock_video (self);
1167 unblock_subtitle (self);
1169 } else if (target) {
1170 gst_object_unref (target);
1174 if (self->subtitle_sink_blocked && !self->video_sink_blocked) {
1175 GST_DEBUG_OBJECT (self, "Subtitle sink blocked but video not blocked");
1180 self->subtitle_flush = FALSE;
1182 /* Find our factories */
1183 g_mutex_lock (&self->factories_lock);
1184 gst_subtitle_overlay_update_factory_list (self);
1187 gst_subtitle_overlay_get_factories_for_caps (self->factories, subcaps);
1191 msg = gst_missing_decoder_message_new (GST_ELEMENT_CAST (self), subcaps);
1192 gst_element_post_message (GST_ELEMENT_CAST (self), msg);
1193 GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
1194 ("no suitable subtitle plugin found"));
1196 self->subtitle_error = TRUE;
1199 g_mutex_unlock (&self->factories_lock);
1202 _setup_passthrough (self);
1203 do_async_done (self);
1207 /* Now the interesting parts are done: subtitle overlaying! */
1209 /* Sort the factories by rank */
1210 factories = g_list_sort (factories, (GCompareFunc) _sort_by_ranks);
1212 for (l = factories; l; l = l->next) {
1213 GstElementFactory *factory = l->data;
1214 gboolean is_renderer = _is_renderer (factory);
1217 /* Unlink & destroy everything */
1219 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1220 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1221 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1223 self->silent_property = NULL;
1224 _remove_element (self, &self->post_colorspace);
1225 _remove_element (self, &self->overlay);
1226 _remove_element (self, &self->parser);
1227 _remove_element (self, &self->renderer);
1228 _remove_element (self, &self->pre_colorspace);
1229 _remove_element (self, &self->passthrough_identity);
1231 GST_DEBUG_OBJECT (self, "Trying factory '%s'",
1232 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1235 if (G_UNLIKELY ((is_renderer
1236 && !_create_element (self, &self->renderer, NULL, factory,
1237 "renderer", FALSE)) || (!is_renderer
1238 && !_create_element (self, &self->parser, NULL, factory,
1243 GstCaps *parser_caps;
1244 GList *overlay_factories, *k;
1246 if (!_setup_parser (self))
1249 /* Find our factories */
1250 src = gst_element_get_static_pad (self->parser, "src");
1251 parser_caps = gst_pad_query_caps (src, NULL);
1252 gst_object_unref (src);
1254 g_assert (parser_caps != NULL);
1256 g_mutex_lock (&self->factories_lock);
1257 gst_subtitle_overlay_update_factory_list (self);
1258 GST_DEBUG_OBJECT (self,
1259 "Searching overlay factories for caps %" GST_PTR_FORMAT, parser_caps);
1261 gst_subtitle_overlay_get_factories_for_caps (self->factories,
1263 g_mutex_unlock (&self->factories_lock);
1265 if (!overlay_factories) {
1266 GST_WARNING_OBJECT (self,
1267 "Found no suitable overlay factories for caps %" GST_PTR_FORMAT,
1269 gst_caps_unref (parser_caps);
1272 gst_caps_unref (parser_caps);
1274 /* Sort the factories by rank */
1276 g_list_sort (overlay_factories, (GCompareFunc) _sort_by_ranks);
1278 for (k = overlay_factories; k; k = k->next) {
1279 GstElementFactory *overlay_factory = k->data;
1281 GST_DEBUG_OBJECT (self, "Trying overlay factory '%s'",
1282 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1283 (overlay_factory))));
1285 /* Try this factory and link it, otherwise unlink everything
1286 * again and remove the overlay. Up to this point only the
1287 * parser was instantiated and setup, nothing was linked
1290 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1291 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad),
1293 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1295 self->silent_property = NULL;
1296 _remove_element (self, &self->post_colorspace);
1297 _remove_element (self, &self->overlay);
1298 _remove_element (self, &self->pre_colorspace);
1300 if (!_create_element (self, &self->overlay, NULL, overlay_factory,
1301 "overlay", FALSE)) {
1302 GST_DEBUG_OBJECT (self, "Could not create element");
1306 if (!_setup_renderer (self, self->overlay)) {
1307 GST_DEBUG_OBJECT (self, "Could not setup element");
1311 src = gst_element_get_static_pad (self->parser, "src");
1312 if (!_link_renderer (self, self->overlay, src)) {
1313 GST_DEBUG_OBJECT (self, "Could not link element");
1314 gst_object_unref (src);
1317 gst_object_unref (src);
1319 /* Everything done here, go out of loop */
1320 GST_DEBUG_OBJECT (self, "%s is a suitable element",
1321 GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
1322 (overlay_factory))));
1326 if (overlay_factories)
1327 gst_plugin_feature_list_free (overlay_factories);
1329 if (G_UNLIKELY (k == NULL)) {
1330 GST_WARNING_OBJECT (self, "Failed to find usable overlay factory");
1334 /* Now link subtitle sinkpad of the bin and the parser */
1335 sink = gst_element_get_static_pad (self->parser, "sink");
1336 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
1337 (self->subtitle_sinkpad), sink)) {
1338 gst_object_unref (sink);
1341 gst_object_unref (sink);
1343 /* Everything done here, go out of loop */
1346 /* Is renderer factory */
1348 if (!_setup_renderer (self, self->renderer))
1351 /* subtitle_src==NULL means: use subtitle_sink ghostpad */
1352 if (!_link_renderer (self, self->renderer, NULL))
1355 /* Everything done here, go out of loop */
1360 if (G_UNLIKELY (l == NULL)) {
1361 GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
1362 ("Failed to find any usable factories"));
1363 self->subtitle_error = TRUE;
1364 _setup_passthrough (self);
1365 do_async_done (self);
1369 /* Send segments to the renderer if necessary. These are not sent
1370 * outside this element because of the proxy pad event handler */
1371 if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
1375 if (self->pre_colorspace) {
1376 sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
1378 sink = _get_video_pad ((self->renderer) ? self->renderer : self->overlay);
1381 _generate_update_segment_event (sink, &self->video_segment, &event1);
1382 GST_DEBUG_OBJECT (self,
1383 "Pushing video update segment event: %" GST_PTR_FORMAT,
1384 gst_event_get_structure (event1));
1385 gst_pad_send_event (sink, event1);
1386 gst_object_unref (sink);
1389 if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
1394 sink = _get_sub_pad (self->renderer);
1396 sink = gst_element_get_static_pad (self->parser, "sink");
1398 _generate_update_segment_event (sink, &self->subtitle_segment, &event1);
1399 GST_DEBUG_OBJECT (self,
1400 "Pushing subtitle update segment event: %" GST_PTR_FORMAT,
1401 gst_event_get_structure (event1));
1402 gst_pad_send_event (sink, event1);
1403 gst_object_unref (sink);
1406 GST_DEBUG_OBJECT (self, "Everything worked, unblocking pads");
1407 unblock_video (self);
1408 unblock_subtitle (self);
1409 do_async_done (self);
1413 gst_plugin_feature_list_free (factories);
1414 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1416 return GST_PAD_PROBE_OK;
1419 static GstStateChangeReturn
1420 gst_subtitle_overlay_change_state (GstElement * element,
1421 GstStateChange transition)
1423 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (element);
1424 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1426 switch (transition) {
1427 case GST_STATE_CHANGE_NULL_TO_READY:
1428 GST_DEBUG_OBJECT (self, "State change NULL->READY");
1429 g_mutex_lock (&self->factories_lock);
1430 if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self))) {
1431 g_mutex_unlock (&self->factories_lock);
1432 return GST_STATE_CHANGE_FAILURE;
1434 g_mutex_unlock (&self->factories_lock);
1436 GST_SUBTITLE_OVERLAY_LOCK (self);
1437 /* Set the internal pads to blocking */
1439 block_subtitle (self);
1440 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1442 case GST_STATE_CHANGE_READY_TO_PAUSED:
1443 GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
1444 gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1445 gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
1447 self->fps_n = self->fps_d = 0;
1449 self->subtitle_flush = FALSE;
1450 self->subtitle_error = FALSE;
1452 self->downstream_chain_error = FALSE;
1454 do_async_start (self);
1455 ret = GST_STATE_CHANGE_ASYNC;
1458 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1459 GST_DEBUG_OBJECT (self, "State change PAUSED->PLAYING");
1465 GstStateChangeReturn bret;
1467 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1468 GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", bret);
1469 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
1471 else if (bret == GST_STATE_CHANGE_ASYNC)
1473 else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
1474 do_async_done (self);
1479 switch (transition) {
1480 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1481 GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
1483 case GST_STATE_CHANGE_PAUSED_TO_READY:
1484 GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
1486 /* Set the pads back to blocking state */
1487 GST_SUBTITLE_OVERLAY_LOCK (self);
1489 block_subtitle (self);
1490 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1492 do_async_done (self);
1495 case GST_STATE_CHANGE_READY_TO_NULL:
1496 GST_DEBUG_OBJECT (self, "State change READY->NULL");
1498 GST_SUBTITLE_OVERLAY_LOCK (self);
1499 gst_caps_replace (&self->subcaps, NULL);
1501 /* Unlink ghost pads */
1502 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
1503 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
1504 gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
1508 unblock_video (self);
1509 unblock_subtitle (self);
1511 /* Remove elements */
1512 self->silent_property = NULL;
1513 _remove_element (self, &self->post_colorspace);
1514 _remove_element (self, &self->overlay);
1515 _remove_element (self, &self->parser);
1516 _remove_element (self, &self->renderer);
1517 _remove_element (self, &self->pre_colorspace);
1518 _remove_element (self, &self->passthrough_identity);
1519 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1530 gst_subtitle_overlay_handle_message (GstBin * bin, GstMessage * message)
1532 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (bin);
1534 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
1535 GstObject *src = GST_MESSAGE_SRC (message);
1537 /* Convert error messages from the subtitle pipeline to
1538 * warnings and switch to passthrough mode */
1541 && gst_object_has_ancestor (src,
1542 GST_OBJECT_CAST (self->overlay))) || (self->parser
1543 && gst_object_has_ancestor (src,
1544 GST_OBJECT_CAST (self->parser))) || (self->renderer
1545 && gst_object_has_ancestor (src,
1546 GST_OBJECT_CAST (self->renderer))))) {
1548 gchar *debug = NULL;
1551 gst_message_parse_error (message, &err, &debug);
1552 GST_DEBUG_OBJECT (self,
1553 "Got error message from subtitle element %s: %s (%s)",
1554 GST_MESSAGE_SRC_NAME (message), GST_STR_NULL (err->message),
1555 GST_STR_NULL (debug));
1557 wmsg = gst_message_new_warning (src, err, debug);
1558 gst_message_unref (message);
1563 GST_SUBTITLE_OVERLAY_LOCK (self);
1564 self->subtitle_error = TRUE;
1566 block_subtitle (self);
1568 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1572 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
1576 gst_subtitle_overlay_get_property (GObject * object, guint prop_id,
1577 GValue * value, GParamSpec * pspec)
1579 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1583 g_value_set_boolean (value, self->silent);
1585 case PROP_FONT_DESC:
1586 GST_SUBTITLE_OVERLAY_LOCK (self);
1587 g_value_set_string (value, self->font_desc);
1588 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1590 case PROP_SUBTITLE_ENCODING:
1591 GST_SUBTITLE_OVERLAY_LOCK (self);
1592 g_value_set_string (value, self->encoding);
1593 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1596 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1602 gst_subtitle_overlay_set_property (GObject * object, guint prop_id,
1603 const GValue * value, GParamSpec * pspec)
1605 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);
1609 GST_SUBTITLE_OVERLAY_LOCK (self);
1610 self->silent = g_value_get_boolean (value);
1611 if (self->silent_property) {
1612 gboolean silent = self->silent;
1614 if (self->silent_property_invert)
1618 g_object_set (self->overlay, self->silent_property, silent, NULL);
1619 else if (self->renderer)
1620 g_object_set (self->renderer, self->silent_property, silent, NULL);
1622 block_subtitle (self);
1625 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1627 case PROP_FONT_DESC:
1628 GST_SUBTITLE_OVERLAY_LOCK (self);
1629 g_free (self->font_desc);
1630 self->font_desc = g_value_dup_string (value);
1632 && _has_property_with_type (G_OBJECT (self->overlay), "font-desc",
1634 g_object_set (self->overlay, "font-desc", self->font_desc, NULL);
1635 else if (self->renderer
1636 && _has_property_with_type (G_OBJECT (self->renderer), "font-desc",
1638 g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1639 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1641 case PROP_SUBTITLE_ENCODING:
1642 GST_SUBTITLE_OVERLAY_LOCK (self);
1643 g_free (self->encoding);
1644 self->encoding = g_value_dup_string (value);
1646 && _has_property_with_type (G_OBJECT (self->renderer),
1647 "subtitle-encoding", G_TYPE_STRING))
1648 g_object_set (self->renderer, "subtitle-encoding", self->encoding,
1651 && _has_property_with_type (G_OBJECT (self->parser),
1652 "subtitle-encoding", G_TYPE_STRING))
1653 g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
1654 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1657 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1663 gst_subtitle_overlay_class_init (GstSubtitleOverlayClass * klass)
1665 GObjectClass *gobject_class = (GObjectClass *) klass;
1666 GstElementClass *element_class = (GstElementClass *) klass;
1667 GstBinClass *bin_class = (GstBinClass *) klass;
1669 gobject_class->finalize = gst_subtitle_overlay_finalize;
1670 gobject_class->set_property = gst_subtitle_overlay_set_property;
1671 gobject_class->get_property = gst_subtitle_overlay_get_property;
1673 g_object_class_install_property (gobject_class, PROP_SILENT,
1674 g_param_spec_boolean ("silent",
1676 "Whether to show subtitles", FALSE,
1677 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1679 g_object_class_install_property (gobject_class, PROP_FONT_DESC,
1680 g_param_spec_string ("font-desc",
1681 "Subtitle font description",
1682 "Pango font description of font "
1683 "to be used for subtitle rendering", NULL,
1684 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1686 g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
1687 g_param_spec_string ("subtitle-encoding", "subtitle encoding",
1688 "Encoding to assume if input subtitles are not in UTF-8 encoding. "
1689 "If not set, the GST_SUBTITLE_ENCODING environment variable will "
1690 "be checked for an encoding to use. If that is not set either, "
1691 "ISO-8859-15 will be assumed.", NULL,
1692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1694 gst_element_class_add_pad_template (element_class,
1695 gst_static_pad_template_get (&srctemplate));
1697 gst_element_class_add_pad_template (element_class,
1698 gst_static_pad_template_get (&video_sinktemplate));
1699 gst_element_class_add_pad_template (element_class,
1700 gst_static_pad_template_get (&subtitle_sinktemplate));
1702 gst_element_class_set_static_metadata (element_class, "Subtitle Overlay",
1703 "Video/Overlay/Subtitle",
1704 "Overlays a video stream with subtitles",
1705 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
1707 element_class->change_state =
1708 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_change_state);
1710 bin_class->handle_message =
1711 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_handle_message);
1714 static GstFlowReturn
1715 gst_subtitle_overlay_src_proxy_chain (GstPad * proxypad, GstObject * parent,
1719 GstSubtitleOverlay *self;
1722 ghostpad = GST_PAD_CAST (parent);
1723 if (G_UNLIKELY (!ghostpad)) {
1724 gst_buffer_unref (buffer);
1725 return GST_FLOW_ERROR;
1727 self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1728 if (G_UNLIKELY (!self || self->srcpad != ghostpad)) {
1729 gst_buffer_unref (buffer);
1730 gst_object_unref (ghostpad);
1731 return GST_FLOW_ERROR;
1734 ret = gst_proxy_pad_chain_default (proxypad, parent, buffer);
1736 if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1737 GST_ERROR_OBJECT (self, "Downstream chain error: %s",
1738 gst_flow_get_name (ret));
1739 self->downstream_chain_error = TRUE;
1742 gst_object_unref (self);
1748 gst_subtitle_overlay_src_proxy_event (GstPad * proxypad, GstObject * parent,
1751 GstPad *ghostpad = NULL;
1752 GstSubtitleOverlay *self = NULL;
1753 gboolean ret = FALSE;
1754 const GstStructure *s;
1756 ghostpad = GST_PAD_CAST (parent);
1757 if (G_UNLIKELY (!ghostpad))
1759 self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
1760 if (G_UNLIKELY (!self || self->srcpad != ghostpad))
1763 s = gst_event_get_structure (event);
1764 if (s && gst_structure_id_has_field (s, _subtitle_overlay_event_marker_id)) {
1765 GST_DEBUG_OBJECT (ghostpad,
1766 "Dropping event with marker: %" GST_PTR_FORMAT,
1767 gst_event_get_structure (event));
1768 gst_event_unref (event);
1772 ret = gst_pad_event_default (proxypad, parent, event);
1778 gst_event_unref (event);
1780 gst_object_unref (self);
1786 gst_subtitle_overlay_video_sink_setcaps (GstSubtitleOverlay * self,
1790 gboolean ret = TRUE;
1793 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1795 if (!gst_video_info_from_caps (&info, caps)) {
1796 GST_ERROR_OBJECT (self, "Failed to parse caps");
1798 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1802 target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->video_sinkpad));
1804 GST_SUBTITLE_OVERLAY_LOCK (self);
1806 if (!target || !gst_pad_query_accept_caps (target, caps)) {
1807 GST_DEBUG_OBJECT (target, "Target did not accept caps -- reconfiguring");
1809 block_subtitle (self);
1813 if (self->fps_n != info.fps_n || self->fps_d != info.fps_d) {
1814 GST_DEBUG_OBJECT (self, "New video fps: %d/%d", info.fps_n, info.fps_d);
1815 self->fps_n = info.fps_n;
1816 self->fps_d = info.fps_d;
1817 gst_subtitle_overlay_set_fps (self);
1819 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1822 gst_object_unref (target);
1830 gst_subtitle_overlay_video_sink_event (GstPad * pad, GstObject * parent,
1833 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1836 switch (GST_EVENT_TYPE (event)) {
1837 case GST_EVENT_FLUSH_STOP:
1839 GST_DEBUG_OBJECT (pad,
1840 "Resetting video segment because of flush-stop event");
1841 gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
1842 self->fps_n = self->fps_d = 0;
1845 case GST_EVENT_CAPS:
1849 gst_event_parse_caps (event, &caps);
1850 ret = gst_subtitle_overlay_video_sink_setcaps (self, caps);
1859 ret = gst_pad_event_default (pad, parent, gst_event_ref (event));
1861 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
1862 GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
1863 gst_event_copy_segment (event, &self->video_segment);
1865 if (self->video_segment.format != GST_FORMAT_TIME)
1866 goto invalid_format;
1870 gst_event_unref (event);
1877 GST_ERROR_OBJECT (pad, "Segment event in non-time format: %s",
1878 gst_format_get_name (self->video_segment.format));
1884 static GstFlowReturn
1885 gst_subtitle_overlay_video_sink_chain (GstPad * pad, GstObject * parent,
1888 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1889 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1891 if (G_UNLIKELY (self->downstream_chain_error) || self->passthrough_identity) {
1893 } else if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
1894 GST_DEBUG_OBJECT (self, "Subtitle renderer produced chain error: %s",
1895 gst_flow_get_name (ret));
1896 GST_SUBTITLE_OVERLAY_LOCK (self);
1897 self->subtitle_error = TRUE;
1898 block_subtitle (self);
1900 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1908 static GstFlowReturn
1909 gst_subtitle_overlay_subtitle_sink_chain (GstPad * pad, GstObject * parent,
1912 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1914 if (self->subtitle_error) {
1915 gst_buffer_unref (buffer);
1918 GstFlowReturn ret = gst_proxy_pad_chain_default (pad, parent, buffer);
1920 if (IS_SUBTITLE_CHAIN_IGNORE_ERROR (ret)) {
1921 GST_DEBUG_OBJECT (self, "Subtitle chain error: %s",
1922 gst_flow_get_name (ret));
1923 GST_SUBTITLE_OVERLAY_LOCK (self);
1924 self->subtitle_error = TRUE;
1925 block_subtitle (self);
1927 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1937 gst_subtitle_overlay_subtitle_sink_getcaps (GstPad * pad, GstCaps * filter)
1942 ret = gst_caps_ref (filter);
1944 ret = gst_caps_new_any ();
1950 gst_subtitle_overlay_subtitle_sink_setcaps (GstSubtitleOverlay * self,
1953 gboolean ret = TRUE;
1954 GstPad *target = NULL;;
1956 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
1959 gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));
1961 GST_SUBTITLE_OVERLAY_LOCK (self);
1962 gst_caps_replace (&self->subcaps, caps);
1964 if (target && gst_pad_query_accept_caps (target, caps)) {
1965 GST_DEBUG_OBJECT (self, "Target accepts caps");
1966 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1970 GST_DEBUG_OBJECT (self, "Target did not accept caps");
1972 self->subtitle_error = FALSE;
1973 block_subtitle (self);
1975 GST_SUBTITLE_OVERLAY_UNLOCK (self);
1979 gst_object_unref (target);
1984 static GstPadLinkReturn
1985 gst_subtitle_overlay_subtitle_sink_link (GstPad * pad, GstObject * parent,
1988 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
1991 GST_DEBUG_OBJECT (pad, "Linking pad to peer %" GST_PTR_FORMAT, peer);
1993 caps = gst_pad_get_current_caps (peer);
1995 caps = gst_pad_query_caps (peer, NULL);
1996 if (!gst_caps_is_fixed (caps)) {
1997 gst_caps_unref (caps);
2003 GST_SUBTITLE_OVERLAY_LOCK (self);
2004 GST_DEBUG_OBJECT (pad, "Have fixed peer caps: %" GST_PTR_FORMAT, caps);
2005 gst_caps_replace (&self->subcaps, caps);
2007 self->subtitle_error = FALSE;
2009 block_subtitle (self);
2011 GST_SUBTITLE_OVERLAY_UNLOCK (self);
2012 gst_caps_unref (caps);
2015 return GST_PAD_LINK_OK;
2019 gst_subtitle_overlay_subtitle_sink_unlink (GstPad * pad, GstObject * parent)
2021 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
2023 /* FIXME: Can't use gst_pad_get_parent() here because this is called with
2024 * the object lock from state changes
2027 GST_DEBUG_OBJECT (pad, "Pad unlinking");
2028 gst_caps_replace (&self->subcaps, NULL);
2030 GST_SUBTITLE_OVERLAY_LOCK (self);
2031 self->subtitle_error = FALSE;
2033 block_subtitle (self);
2035 GST_SUBTITLE_OVERLAY_UNLOCK (self);
2039 gst_subtitle_overlay_subtitle_sink_event (GstPad * pad, GstObject * parent,
2042 GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (parent);
2045 GST_DEBUG_OBJECT (pad, "Got event %" GST_PTR_FORMAT, event);
2047 if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_OOB &&
2048 gst_event_has_name (event, "playsink-custom-subtitle-flush")) {
2049 GST_DEBUG_OBJECT (pad, "Custom subtitle flush event");
2050 GST_SUBTITLE_OVERLAY_LOCK (self);
2051 self->subtitle_flush = TRUE;
2052 self->subtitle_error = FALSE;
2053 block_subtitle (self);
2055 GST_SUBTITLE_OVERLAY_UNLOCK (self);
2057 gst_event_unref (event);
2063 switch (GST_EVENT_TYPE (event)) {
2064 case GST_EVENT_CAPS:
2068 gst_event_parse_caps (event, &caps);
2069 ret = gst_subtitle_overlay_subtitle_sink_setcaps (self, caps);
2074 case GST_EVENT_FLUSH_STOP:
2075 GST_DEBUG_OBJECT (pad,
2076 "Resetting subtitle segment because of flush-stop");
2077 gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);
2079 case GST_EVENT_FLUSH_START:
2080 case GST_EVENT_SEGMENT:
2083 GstStructure *structure;
2085 /* Add our event marker to make sure no events from here go ever outside
2086 * the element, they're only interesting for our internal elements */
2087 event = GST_EVENT_CAST (gst_event_make_writable (event));
2088 structure = gst_event_writable_structure (event);
2090 gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
2091 G_TYPE_BOOLEAN, TRUE, NULL);
2098 ret = gst_pad_event_default (pad, parent, gst_event_ref (event));
2100 if (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT) {
2101 const GstSegment *eventsegment;
2102 GST_DEBUG_OBJECT (pad, "segment event: %" GST_PTR_FORMAT, event);
2104 gst_event_parse_segment (event, &eventsegment);
2106 if (eventsegment->format == GST_FORMAT_TIME) {
2107 gst_event_copy_segment (event, &self->subtitle_segment);
2110 gboolean res = FALSE;
2112 gint64 tstop = GST_CLOCK_TIME_NONE;
2113 gint64 tposition = 0;
2114 gint64 tduration = 0;
2117 GstFormat format = eventsegment->format;
2119 GST_DEBUG_OBJECT (pad, "Subtitle newsegment event (%s) not in TIME "
2120 "format, converting", gst_format_get_name (format));
2121 peer = gst_pad_get_peer (pad);
2125 _pad_query_convert_to_time (peer, format, eventsegment->start,
2128 && _pad_query_convert_to_time (peer, format, eventsegment->stop,
2131 && _pad_query_convert_to_time (peer, format, eventsegment->position,
2134 && _pad_query_convert_to_time (peer, format, eventsegment->base,
2137 && _pad_query_convert_to_time (peer, format, eventsegment->time,
2140 && _pad_query_convert_to_time (peer, format, eventsegment->duration,
2143 gst_object_unref (peer);
2148 tstop = GST_CLOCK_TIME_NONE;
2150 tduration = GST_CLOCK_TIME_NONE;
2155 gst_segment_init (&self->subtitle_segment, GST_FORMAT_TIME);
2156 self->subtitle_segment.start = tstart;
2157 self->subtitle_segment.stop = tstop;
2158 self->subtitle_segment.base = tbase;
2159 self->subtitle_segment.time = ttime;
2160 self->subtitle_segment.position = tposition;
2161 self->subtitle_segment.duration = tduration;
2163 GST_DEBUG_OBJECT (pad, "New subtitle segment: %" GST_SEGMENT_FORMAT,
2164 &self->subtitle_segment);
2166 gst_event_unref (event);
2173 gst_subtitle_overlay_subtitle_sink_query (GstPad * pad, GstObject * parent,
2178 switch (GST_QUERY_TYPE (query)) {
2179 case GST_QUERY_ACCEPT_CAPS:
2181 gst_query_set_accept_caps_result (query, TRUE);
2185 case GST_QUERY_CAPS:
2187 GstCaps *filter, *caps;
2189 gst_query_parse_caps (query, &filter);
2190 caps = gst_subtitle_overlay_subtitle_sink_getcaps (pad, filter);
2191 gst_query_set_caps_result (query, caps);
2192 gst_caps_unref (caps);
2197 ret = gst_pad_query_default (pad, parent, query);
2205 gst_subtitle_overlay_init (GstSubtitleOverlay * self)
2207 GstPadTemplate *templ;
2208 GstPad *proxypad = NULL;
2210 g_mutex_init (&self->lock);
2211 g_mutex_init (&self->factories_lock);
2213 templ = gst_static_pad_template_get (&srctemplate);
2214 self->srcpad = gst_ghost_pad_new_no_target_from_template ("src", templ);
2215 gst_object_unref (templ);
2218 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD (self->srcpad)));
2219 gst_pad_set_event_function (proxypad,
2220 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_event));
2221 gst_pad_set_chain_function (proxypad,
2222 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_src_proxy_chain));
2223 gst_object_unref (proxypad);
2225 gst_element_add_pad (GST_ELEMENT_CAST (self), self->srcpad);
2227 templ = gst_static_pad_template_get (&video_sinktemplate);
2228 self->video_sinkpad =
2229 gst_ghost_pad_new_no_target_from_template ("video_sink", templ);
2230 gst_object_unref (templ);
2231 gst_pad_set_event_function (self->video_sinkpad,
2232 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_event));
2233 gst_pad_set_chain_function (self->video_sinkpad,
2234 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_video_sink_chain));
2237 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2238 (self->video_sinkpad)));
2239 self->video_block_pad = proxypad;
2240 gst_object_unref (proxypad);
2241 gst_element_add_pad (GST_ELEMENT_CAST (self), self->video_sinkpad);
2243 templ = gst_static_pad_template_get (&subtitle_sinktemplate);
2244 self->subtitle_sinkpad =
2245 gst_ghost_pad_new_no_target_from_template ("subtitle_sink", templ);
2246 gst_object_unref (templ);
2247 gst_pad_set_link_function (self->subtitle_sinkpad,
2248 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_link));
2249 gst_pad_set_unlink_function (self->subtitle_sinkpad,
2250 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_unlink));
2251 gst_pad_set_event_function (self->subtitle_sinkpad,
2252 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_event));
2253 gst_pad_set_query_function (self->subtitle_sinkpad,
2254 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_query));
2255 gst_pad_set_chain_function (self->subtitle_sinkpad,
2256 GST_DEBUG_FUNCPTR (gst_subtitle_overlay_subtitle_sink_chain));
2259 GST_PAD_CAST (gst_proxy_pad_get_internal (GST_PROXY_PAD
2260 (self->subtitle_sinkpad)));
2261 self->subtitle_block_pad = proxypad;
2262 gst_object_unref (proxypad);
2264 gst_element_add_pad (GST_ELEMENT_CAST (self), self->subtitle_sinkpad);
2271 gst_subtitle_overlay_plugin_init (GstPlugin * plugin)
2273 GST_DEBUG_CATEGORY_INIT (subtitle_overlay_debug, "subtitleoverlay", 0,
2274 "Subtitle Overlay");
2276 _subtitle_overlay_event_marker_id =
2277 g_quark_from_static_string ("gst-subtitle-overlay-event-marker");
2279 return gst_element_register (plugin, "subtitleoverlay", GST_RANK_NONE,
2280 GST_TYPE_SUBTITLE_OVERLAY);