gstsubtitleoverlay.c 59.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:element-subtitleoverlay
 *
 * #GstBin that auto-magically overlays a video stream with subtitles by
 * autoplugging the required elements.
 *
 * It supports raw, timestamped text, different textual subtitle formats and
 * DVD subpicture subtitles.
 *
 * <refsect2>
 * <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. ! "video/x-dvd-subpicture" ! queue2 ! overlay.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 * ]| This will play back the given Matroska file with h264 video and subpicture subtitles.
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstsubtitleoverlay.h"

#include <gst/gstfilter.h>
#include <gst/pbutils/missing-plugins.h>
#include <gst/video/video.h>
#include <string.h>

GST_DEBUG_CATEGORY_STATIC (subtitle_overlay_debug);
#define GST_CAT_DEFAULT subtitle_overlay_debug

51 52 53 54 55 56
#define IS_SUBTITLE_CHAIN_IGNORE_ERROR(flow) \
  G_UNLIKELY (flow == GST_FLOW_ERROR || flow == GST_FLOW_NOT_NEGOTIATED)

#define IS_VIDEO_CHAIN_IGNORE_ERROR(flow) \
  G_UNLIKELY (flow == GST_FLOW_ERROR)

57 58 59
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
60
    GST_STATIC_CAPS_ANY);
61 62

static GstStaticPadTemplate video_sinktemplate =
63
GST_STATIC_PAD_TEMPLATE ("video_sink",
64 65
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
66
    GST_STATIC_CAPS_ANY);
67 68 69 70 71 72 73 74 75 76

static GstStaticPadTemplate subtitle_sinktemplate =
GST_STATIC_PAD_TEMPLATE ("subtitle_sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

enum
{
  PROP_0,
77
  PROP_SILENT,
78 79
  PROP_FONT_DESC,
  PROP_SUBTITLE_ENCODING
80 81
};

82 83
#define gst_subtitle_overlay_parent_class parent_class
G_DEFINE_TYPE (GstSubtitleOverlay, gst_subtitle_overlay, GST_TYPE_BIN);
84 85 86 87 88 89 90

static GQuark _subtitle_overlay_event_marker_id = 0;

static void
do_async_start (GstSubtitleOverlay * self)
{
  if (!self->do_async) {
91
    GstMessage *msg = gst_message_new_async_start (GST_OBJECT_CAST (self));
92 93

    GST_DEBUG_OBJECT (self, "Posting async-start");
94
    GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
95 96 97 98 99 100 101 102
    self->do_async = TRUE;
  }
}

static void
do_async_done (GstSubtitleOverlay * self)
{
  if (self->do_async) {
103 104
    GstMessage *msg =
        gst_message_new_async_done (GST_OBJECT_CAST (self), FALSE);
105 106

    GST_DEBUG_OBJECT (self, "Posting async-done");
107
    GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (self), msg);
108 109 110 111
    self->do_async = FALSE;
  }
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static GstProbeReturn
_pad_blocked_cb (GstPad * pad, GstProbeType type, gpointer type_data,
    gpointer user_data);

static void
block_video (GstSubtitleOverlay * self)
{
  if (self->video_block_id != 0)
    return;

  if (self->video_block_pad) {
    self->video_block_id =
        gst_pad_add_probe (self->video_block_pad, GST_PROBE_TYPE_BLOCK,
        _pad_blocked_cb, gst_object_ref (self),
        (GDestroyNotify) gst_object_unref);
  }
}

static void
unblock_video (GstSubtitleOverlay * self)
{
  if (self->video_block_id) {
    gst_pad_remove_probe (self->video_block_pad, self->video_block_id);
    self->video_sink_blocked = FALSE;
    self->video_block_id = 0;
  }
}

static void
block_subtitle (GstSubtitleOverlay * self)
{
  if (self->subtitle_block_id != 0)
    return;

  if (self->subtitle_block_pad) {
    self->subtitle_block_id =
        gst_pad_add_probe (self->subtitle_block_pad, GST_PROBE_TYPE_BLOCK,
        _pad_blocked_cb, gst_object_ref (self),
        (GDestroyNotify) gst_object_unref);
  }
}

static void
unblock_subtitle (GstSubtitleOverlay * self)
{
  if (self->subtitle_block_id) {
    gst_pad_remove_probe (self->subtitle_block_pad, self->subtitle_block_id);
    self->subtitle_sink_blocked = FALSE;
    self->subtitle_block_id = 0;
  }
}

164 165 166 167 168 169 170 171 172 173
static void
gst_subtitle_overlay_finalize (GObject * object)
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (object);

  if (self->lock) {
    g_mutex_free (self->lock);
    self->lock = NULL;
  }

174 175 176 177 178 179 180 181 182 183
  if (self->factories_lock) {
    g_mutex_free (self->factories_lock);
    self->factories_lock = NULL;
  }

  if (self->factories)
    gst_plugin_feature_list_free (self->factories);
  self->factories = NULL;
  gst_caps_replace (&self->factory_caps, NULL);

184 185 186 187 188
  if (self->font_desc) {
    g_free (self->font_desc);
    self->font_desc = NULL;
  }

189 190 191 192 193
  if (self->encoding) {
    g_free (self->encoding);
    self->encoding = NULL;
  }

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
_is_renderer (GstElementFactory * factory)
{
  const gchar *klass, *name;

  klass = gst_element_factory_get_klass (factory);
  name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));

  if (strstr (klass, "Overlay/Subtitle") != NULL ||
      strstr (klass, "Overlay/SubPicture") != NULL)
    return TRUE;
  if (strcmp (name, "textoverlay") == 0)
    return TRUE;
  return FALSE;
}

static gboolean
_is_parser (GstElementFactory * factory)
{
  const gchar *klass;

  klass = gst_element_factory_get_klass (factory);

  if (strstr (klass, "Parser/Subtitle") != NULL)
    return TRUE;
  return FALSE;
}

static const gchar *_sub_pad_names[] = { "subpicture", "subpicture_sink",
  "text", "text_sink",
  "subtitle_sink", "subtitle"
};

static GstCaps *
_get_sub_caps (GstElementFactory * factory)
{
  const GList *templates;
  GList *walk;
  gboolean is_parser = _is_parser (factory);

  templates = gst_element_factory_get_static_pad_templates (factory);
  for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
    GstStaticPadTemplate *templ = walk->data;

    if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
      gboolean found = FALSE;

      if (is_parser) {
        found = TRUE;
      } else {
        guint i;

        for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
          if (strcmp (templ->name_template, _sub_pad_names[i]) == 0) {
            found = TRUE;
            break;
          }
        }
      }
      if (found)
        return gst_static_caps_get (&templ->static_caps);
    }
  }
  return NULL;
}

static gboolean
_factory_filter (GstPluginFeature * feature, GstCaps ** subcaps)
{
  GstElementFactory *factory;
  guint rank;
  const gchar *name;
  const GList *templates;
  GList *walk;
  gboolean is_renderer;
  GstCaps *templ_caps = NULL;
  gboolean have_video_sink = FALSE;

  /* we only care about element factories */
  if (!GST_IS_ELEMENT_FACTORY (feature))
    return FALSE;

  factory = GST_ELEMENT_FACTORY_CAST (feature);

  /* only select elements with autoplugging rank or textoverlay */
  name = gst_plugin_feature_get_name (feature);
  rank = gst_plugin_feature_get_rank (feature);
  if (strcmp ("textoverlay", name) != 0 && rank < GST_RANK_MARGINAL)
    return FALSE;

  /* Check if it's a renderer or a parser */
  if (_is_renderer (factory)) {
    is_renderer = TRUE;
  } else if (_is_parser (factory)) {
    is_renderer = FALSE;
  } else {
    return FALSE;
  }

  /* Check if there's a video sink in case of a renderer */
  if (is_renderer) {
    templates = gst_element_factory_get_static_pad_templates (factory);
    for (walk = (GList *) templates; walk; walk = g_list_next (walk)) {
      GstStaticPadTemplate *templ = walk->data;

      /* we only care about the always-sink templates */
      if (templ->direction == GST_PAD_SINK && templ->presence == GST_PAD_ALWAYS) {
        if (strcmp (templ->name_template, "video") == 0 ||
            strcmp (templ->name_template, "video_sink") == 0) {
          have_video_sink = TRUE;
        }
      }
    }
  }
  templ_caps = _get_sub_caps (factory);

  if (is_renderer && have_video_sink && templ_caps) {
    GstCaps *tmp;

    GST_DEBUG ("Found renderer element %s (%s) with caps %" GST_PTR_FORMAT,
        gst_element_factory_get_longname (factory),
        gst_plugin_feature_get_name (feature), templ_caps);
    tmp = gst_caps_union (*subcaps, templ_caps);
    gst_caps_unref (templ_caps);
    gst_caps_replace (subcaps, tmp);
    gst_caps_unref (tmp);
    return TRUE;
  } else if (!is_renderer && !have_video_sink && templ_caps) {
    GstCaps *tmp;

    GST_DEBUG ("Found parser element %s (%s) with caps %" GST_PTR_FORMAT,
        gst_element_factory_get_longname (factory),
        gst_plugin_feature_get_name (feature), templ_caps);
    tmp = gst_caps_union (*subcaps, templ_caps);
    gst_caps_unref (templ_caps);
    gst_caps_replace (subcaps, tmp);
    gst_caps_unref (tmp);
    return TRUE;
  } else {
    if (templ_caps)
      gst_caps_unref (templ_caps);
    return FALSE;
  }
}

342
/* Call with factories_lock! */
343
static gboolean
344
gst_subtitle_overlay_update_factory_list (GstSubtitleOverlay * self)
345
{
346 347 348
  if (!self->factories
      || self->factories_cookie !=
      gst_default_registry_get_feature_list_cookie ()) {
349 350 351 352 353 354 355 356 357 358
    GstCaps *subcaps;
    GList *factories;

    subcaps = gst_caps_new_empty ();

    factories = gst_default_registry_feature_filter (
        (GstPluginFeatureFilter) _factory_filter, FALSE, &subcaps);
    GST_DEBUG_OBJECT (self, "Created factory caps: %" GST_PTR_FORMAT, subcaps);
    gst_caps_replace (&self->factory_caps, subcaps);
    gst_caps_unref (subcaps);
359 360 361 362
    if (self->factories)
      gst_plugin_feature_list_free (self->factories);
    self->factories = factories;
    self->factories_cookie = gst_default_registry_get_feature_list_cookie ();
363 364 365 366 367
  }

  return (self->factories != NULL);
}

368 369 370 371
G_LOCK_DEFINE_STATIC (_factory_caps);
static GstCaps *_factory_caps = NULL;
static guint32 _factory_caps_cookie = 0;

372 373 374 375
GstCaps *
gst_subtitle_overlay_create_factory_caps (void)
{
  GList *factories;
376
  GstCaps *subcaps = NULL;
377

378 379 380 381 382 383 384
  G_LOCK (_factory_caps);
  if (!_factory_caps
      || _factory_caps_cookie !=
      gst_default_registry_get_feature_list_cookie ()) {
    if (_factory_caps)
      gst_caps_unref (_factory_caps);
    _factory_caps = gst_caps_new_empty ();
385

386 387 388 389 390 391 392 393
    factories = gst_default_registry_feature_filter (
        (GstPluginFeatureFilter) _factory_filter, FALSE, &_factory_caps);
    GST_DEBUG ("Created factory caps: %" GST_PTR_FORMAT, _factory_caps);
    gst_plugin_feature_list_free (factories);
    _factory_caps_cookie = gst_default_registry_get_feature_list_cookie ();
  }
  subcaps = gst_caps_ref (_factory_caps);
  G_UNLOCK (_factory_caps);
394 395 396 397 398 399 400 401 402 403 404 405

  return subcaps;
}

static gboolean
_filter_factories_for_caps (GstElementFactory * factory, const GstCaps * caps)
{
  GstCaps *fcaps = _get_sub_caps (factory);
  gboolean ret = (fcaps) ? gst_caps_can_intersect (fcaps, caps) : FALSE;

  if (fcaps)
    gst_caps_unref (fcaps);
406 407 408

  if (ret)
    gst_object_ref (factory);
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  return ret;
}

static gint
_sort_by_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
{
  gint diff;
  const gchar *rname1, *rname2;

  diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
  if (diff != 0)
    return diff;

  /* If the ranks are the same sort by name to get deterministic results */
  rname1 = gst_plugin_feature_get_name (f1);
  rname2 = gst_plugin_feature_get_name (f2);

  diff = strcmp (rname1, rname2);

  return diff;
}

static GstPad *
_get_sub_pad (GstElement * element)
{
  GstPad *pad;
  guint i;

  for (i = 0; i < G_N_ELEMENTS (_sub_pad_names); i++) {
    pad = gst_element_get_static_pad (element, _sub_pad_names[i]);
    if (pad)
      return pad;
  }
  return NULL;
}

static GstPad *
_get_video_pad (GstElement * element)
{
  static const gchar *pad_names[] = { "video", "video_sink" };
  GstPad *pad;
  guint i;

  for (i = 0; i < G_N_ELEMENTS (pad_names); i++) {
    pad = gst_element_get_static_pad (element, pad_names[i]);
    if (pad)
      return pad;
  }
  return NULL;
}

static gboolean
_create_element (GstSubtitleOverlay * self, GstElement ** element,
    const gchar * factory_name, GstElementFactory * factory,
    const gchar * element_name, gboolean mandatory)
{
  GstElement *elt;

  g_assert (!factory || !factory_name);

  if (factory_name) {
    elt = gst_element_factory_make (factory_name, element_name);
  } else {
    factory_name =
        gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));
    elt = gst_element_factory_create (factory, element_name);
  }

  if (G_UNLIKELY (!elt)) {
    if (!factory) {
      GstMessage *msg;

      msg =
          gst_missing_element_message_new (GST_ELEMENT_CAST (self),
          factory_name);
      gst_element_post_message (GST_ELEMENT_CAST (self), msg);

      if (mandatory)
        GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
            ("no '%s' plugin found", factory_name));
      else
        GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
            ("no '%s' plugin found", factory_name));
    } else {
      if (mandatory) {
        GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
            ("can't instantiate '%s'", factory_name));
      } else {
        GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
            ("can't instantiate '%s'", factory_name));
      }
    }

    return FALSE;
  }

  if (G_UNLIKELY (gst_element_set_state (elt,
              GST_STATE_READY) != GST_STATE_CHANGE_SUCCESS)) {
    gst_object_unref (elt);
    if (mandatory) {
      GST_ELEMENT_ERROR (self, CORE, STATE_CHANGE, (NULL),
          ("failed to set '%s' to READY", factory_name));
    } else {
      GST_WARNING_OBJECT (self, "Failed to set '%s' to READY", factory_name);
    }
    return FALSE;
  }

  if (G_UNLIKELY (!gst_bin_add (GST_BIN_CAST (self), gst_object_ref (elt)))) {
    gst_element_set_state (elt, GST_STATE_NULL);
    gst_object_unref (elt);
    if (mandatory) {
      GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
          ("failed to add '%s' to subtitleoverlay", factory_name));
    } else {
      GST_WARNING_OBJECT (self, "Failed to add '%s' to subtitleoverlay",
          factory_name);
    }
    return FALSE;
  }

  gst_element_sync_state_with_parent (elt);
  *element = elt;
  return TRUE;
}

static void
_remove_element (GstSubtitleOverlay * self, GstElement ** element)
{
  if (*element) {
    gst_bin_remove (GST_BIN_CAST (self), *element);
    gst_element_set_state (*element, GST_STATE_NULL);
    gst_object_unref (*element);
    *element = NULL;
  }
}

static void
Wim Taymans's avatar
Wim Taymans committed
547
_generate_update_segment_event (GstSegment * segment, GstEvent ** event1)
548 549
{
  GstEvent *event;
550
  GstStructure *structure;
551

Wim Taymans's avatar
Wim Taymans committed
552 553
  event = gst_event_new_segment (segment);
  structure = gst_event_writable_structure (event);
554
  gst_structure_id_set (structure, _subtitle_overlay_event_marker_id,
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
      G_TYPE_BOOLEAN, TRUE, NULL);
  *event1 = event;
}

static gboolean
_setup_passthrough (GstSubtitleOverlay * self)
{
  GstPad *src, *sink;
  GstElement *identity;

  GST_DEBUG_OBJECT (self, "Doing video passthrough");

  if (self->passthrough_identity) {
    GST_DEBUG_OBJECT (self, "Already in passthrough mode");
    goto out;
  }

  /* Unlink & destroy everything */
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
  gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad), NULL);
576
  self->silent_property = NULL;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  _remove_element (self, &self->post_colorspace);
  _remove_element (self, &self->overlay);
  _remove_element (self, &self->parser);
  _remove_element (self, &self->renderer);
  _remove_element (self, &self->pre_colorspace);
  _remove_element (self, &self->passthrough_identity);

  if (G_UNLIKELY (!_create_element (self, &self->passthrough_identity,
              "identity", NULL, "passthrough-identity", TRUE))) {
    return FALSE;
  }

  identity = self->passthrough_identity;
  g_object_set (G_OBJECT (identity), "silent", TRUE, "signal-handoffs", FALSE,
      NULL);

  /* Set src ghostpad target */
  src = gst_element_get_static_pad (self->passthrough_identity, "src");
  if (G_UNLIKELY (!src)) {
    GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
        ("Failed to get srcpad from identity"));
    return FALSE;
  }

  if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad),
              src))) {
    GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
        ("Failed to set srcpad target"));
    gst_object_unref (src);
    return FALSE;
  }
  gst_object_unref (src);

  sink = gst_element_get_static_pad (self->passthrough_identity, "sink");
  if (G_UNLIKELY (!sink)) {
    GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
        ("Failed to get sinkpad from identity"));
    return FALSE;
  }

  /* Send segment to the identity. This is dropped because identity
   * is not linked downstream yet */
619
  if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
Wim Taymans's avatar
Wim Taymans committed
620
    GstEvent *event1;
621

Wim Taymans's avatar
Wim Taymans committed
622
    _generate_update_segment_event (&self->video_segment, &event1);
623
    GST_DEBUG_OBJECT (self,
Wim Taymans's avatar
Wim Taymans committed
624
        "Pushing video segment event: %" GST_PTR_FORMAT, event1);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    gst_pad_send_event (sink, event1);
  }

  /* Link sink ghostpads to identity */
  if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
              (self->video_sinkpad), sink))) {
    GST_ELEMENT_ERROR (self, CORE, PAD, (NULL),
        ("Failed to set video sinkpad target"));
    gst_object_unref (sink);
    return FALSE;
  }
  gst_object_unref (sink);

  GST_DEBUG_OBJECT (self, "Video passthrough setup successfully");

out:
  /* Unblock pads */
642 643
  unblock_video (self);
  unblock_subtitle (self);
644 645 646 647

  return TRUE;
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
/* Must be called with subtitleoverlay lock! */
static void
gst_subtitle_overlay_set_fps (GstSubtitleOverlay * self)
{
  GObjectClass *gobject_class;
  GParamSpec *pspec;

  if (!self->parser || self->fps_d == 0)
    return;

  gobject_class = G_OBJECT_GET_CLASS (self->parser);
  pspec = g_object_class_find_property (gobject_class, "video-fps");
  if (!pspec || pspec->value_type != GST_TYPE_FRACTION)
    return;

  GST_DEBUG_OBJECT (self, "Updating video-fps property in parser");
  g_object_set (self->parser, "video-fps", self->fps_n, self->fps_d, NULL);
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static const gchar *
_get_silent_property (GstElement * element, gboolean * invert)
{
  static const struct
  {
    const gchar *name;
    gboolean invert;
  } properties[] = { {
  "silent", FALSE}, {
  "enable", TRUE}};
  GObjectClass *gobject_class;
  GParamSpec *pspec;
  guint i;

  gobject_class = G_OBJECT_GET_CLASS (element);

  for (i = 0; i < G_N_ELEMENTS (properties); i++) {
    pspec = g_object_class_find_property (gobject_class, properties[i].name);
    if (pspec && pspec->value_type == G_TYPE_BOOLEAN) {
      *invert = properties[i].invert;
      return properties[i].name;
    }
  }
  return NULL;
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
static gboolean
_has_subtitle_encoding_property (GstElement * element)
{
  GParamSpec *pspec;

  pspec =
      g_object_class_find_property (G_OBJECT_GET_CLASS (element),
      "subtitle-encoding");
  return (pspec && pspec->value_type == G_TYPE_STRING);
}

static gboolean
_has_font_desc_property (GstElement * element)
{
  GParamSpec *pspec;

  pspec =
      g_object_class_find_property (G_OBJECT_GET_CLASS (element), "font-desc");
  return (pspec && pspec->value_type == G_TYPE_STRING);
}

714 715 716
static GstProbeReturn
_pad_blocked_cb (GstPad * pad, GstProbeType type, gpointer type_data,
    gpointer user_data)
717 718 719 720 721
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (user_data);
  GstCaps *subcaps;
  GList *l, *factories = NULL;

722
  GST_DEBUG_OBJECT (pad, "Pad blocked");
723 724 725

  GST_SUBTITLE_OVERLAY_LOCK (self);
  if (pad == self->video_block_pad)
726
    self->video_sink_blocked = TRUE;
727
  else if (pad == self->subtitle_block_pad)
728
    self->subtitle_sink_blocked = TRUE;
729 730 731 732 733 734 735 736 737 738 739 740

  /* Now either both or the video sink are blocked */

  /* Get current subtitle caps */
  subcaps = self->subcaps;
  if (!subcaps) {
    GstPad *peer;

    peer = gst_pad_get_peer (self->subtitle_sinkpad);
    if (peer) {
      subcaps = gst_pad_get_negotiated_caps (peer);
      if (!subcaps) {
741
        subcaps = gst_pad_get_caps (peer, NULL);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
        if (!gst_caps_is_fixed (subcaps)) {
          gst_caps_unref (subcaps);
          subcaps = NULL;
        }
      }
      gst_object_unref (peer);
    }
    gst_caps_replace (&self->subcaps, subcaps);
    if (subcaps)
      gst_caps_unref (subcaps);
  }
  GST_DEBUG_OBJECT (self, "Current subtitle caps: %" GST_PTR_FORMAT, subcaps);

  /* If there are no subcaps but the subtitle sink is blocked upstream
   * must behave wrong as there are no fixed caps set for the first
   * buffer or in-order event */
  if (G_UNLIKELY (!subcaps && self->subtitle_sink_blocked)) {
    GST_ELEMENT_WARNING (self, CORE, NEGOTIATION, (NULL),
        ("Subtitle sink is blocked but we have no subtitle caps"));
    subcaps = NULL;
  }

764
  if (self->subtitle_error || (self->silent && !self->silent_property)) {
765 766 767 768 769
    _setup_passthrough (self);
    do_async_done (self);
    goto out;
  }

770 771 772 773 774 775 776 777 778 779 780
  /* Now do something with the caps */
  if (subcaps && !self->subtitle_flush) {
    GstPad *target =
        gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad));

    if (target && gst_pad_accept_caps (target, subcaps)) {
      GST_DEBUG_OBJECT (pad, "Target accepts caps");

      gst_object_unref (target);

      /* Unblock pads */
781 782
      unblock_video (self);
      unblock_subtitle (self);
783 784 785 786 787 788 789 790 791

      goto out;
    } else if (target) {
      gst_object_unref (target);
    }
  }

  if (self->subtitle_sink_blocked && !self->video_sink_blocked) {
    GST_DEBUG_OBJECT (self, "Subtitle sink blocked but video not blocked");
792
    block_video (self);
793 794 795 796 797 798
    goto out;
  }

  self->subtitle_flush = FALSE;

  /* Find our factories */
799 800
  g_mutex_lock (self->factories_lock);
  gst_subtitle_overlay_update_factory_list (self);
801 802 803 804 805 806 807 808 809 810 811
  if (subcaps) {
    factories = gst_filter_run (self->factories,
        (GstFilterFunc) _filter_factories_for_caps, FALSE, subcaps);
    if (!factories) {
      GstMessage *msg;

      msg = gst_missing_decoder_message_new (GST_ELEMENT_CAST (self), subcaps);
      gst_element_post_message (GST_ELEMENT_CAST (self), msg);
      GST_ELEMENT_WARNING (self, CORE, MISSING_PLUGIN, (NULL),
          ("no suitable subtitle plugin found"));
      subcaps = NULL;
812
      self->subtitle_error = TRUE;
813 814
    }
  }
815
  g_mutex_unlock (self->factories_lock);
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

  if (!subcaps) {
    _setup_passthrough (self);
    do_async_done (self);
    goto out;
  }

  /* Now the interesting parts are done: subtitle overlaying! */

  /* Sort the factories by rank */
  factories = g_list_sort (factories, (GCompareFunc) _sort_by_ranks);

  for (l = factories; l; l = l->next) {
    GstElementFactory *factory = l->data;
    gboolean is_renderer = _is_renderer (factory);
    GstElement *element;
    GstPad *sink, *src;

    /* Unlink & destroy everything */

    gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
    gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
    gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
        NULL);
840
    self->silent_property = NULL;
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
    _remove_element (self, &self->post_colorspace);
    _remove_element (self, &self->overlay);
    _remove_element (self, &self->parser);
    _remove_element (self, &self->renderer);
    _remove_element (self, &self->pre_colorspace);
    _remove_element (self, &self->passthrough_identity);

    GST_DEBUG_OBJECT (self, "Trying factory '%s'",
        GST_STR_NULL (gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST
                (factory))));

    if (G_UNLIKELY ((is_renderer
                && !_create_element (self, &self->renderer, NULL, factory,
                    "renderer", FALSE)) || (!is_renderer
                && !_create_element (self, &self->parser, NULL, factory,
                    "parser", FALSE))))
      continue;

    element = is_renderer ? self->renderer : self->parser;

    /* If this is a parser, create textoverlay and link video and the parser to it
     * Else link the renderer to the output colorspace */
    if (!is_renderer) {
      GstElement *overlay;
865 866 867 868 869 870 871 872 873 874
      GstPad *video_peer;

      /* Try to get the latest video framerate */
      video_peer = gst_pad_get_peer (self->video_sinkpad);
      if (video_peer) {
        GstCaps *video_caps;
        gint fps_n, fps_d;

        video_caps = gst_pad_get_negotiated_caps (video_peer);
        if (!video_caps) {
875
          video_caps = gst_pad_get_caps (video_peer, NULL);
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
          if (!gst_caps_is_fixed (video_caps)) {
            gst_caps_unref (video_caps);
            video_caps = NULL;
          }
        }

        if (video_caps
            && gst_video_parse_caps_framerate (video_caps, &fps_n, &fps_d)) {
          if (self->fps_n != fps_n || self->fps_d != fps_d) {
            GST_DEBUG_OBJECT (self, "New video fps: %d/%d", fps_n, fps_d);
            self->fps_n = fps_n;
            self->fps_d = fps_d;
          }
        }

        if (video_caps)
          gst_caps_unref (video_caps);
        gst_object_unref (video_peer);
      }

896 897 898
      if (_has_subtitle_encoding_property (self->parser))
        g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);

899 900
      /* Try to set video fps on the parser */
      gst_subtitle_overlay_set_fps (self);
901 902 903 904 905 906 907

      /* First link everything internally */
      if (G_UNLIKELY (!_create_element (self, &self->overlay, "textoverlay",
                  NULL, "overlay", FALSE))) {
        continue;
      }
      overlay = self->overlay;
908 909
      self->silent_property = "silent";
      self->silent_property_invert = FALSE;
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

      /* Set some properties */
      g_object_set (G_OBJECT (overlay),
          "halign", "center", "valign", "bottom", "wait-text", FALSE, NULL);
      if (self->font_desc)
        g_object_set (G_OBJECT (overlay), "font-desc", self->font_desc, NULL);

      src = gst_element_get_static_pad (element, "src");
      if (G_UNLIKELY (!src)) {
        continue;
      }

      sink = gst_element_get_static_pad (overlay, "text_sink");
      if (G_UNLIKELY (!sink)) {
        GST_WARNING_OBJECT (self, "Can't get text sink from textoverlay");
        gst_object_unref (src);
926
        continue;
927 928 929 930 931 932
      }

      if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
        GST_WARNING_OBJECT (self, "Can't link parser to textoverlay");
        gst_object_unref (sink);
        gst_object_unref (src);
933
        continue;
934 935 936 937 938
      }
      gst_object_unref (sink);
      gst_object_unref (src);

      if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
939
                  "videoconvert", NULL, "post-colorspace", FALSE))) {
940 941 942 943 944 945 946 947 948 949 950
        continue;
      }

      src = gst_element_get_static_pad (overlay, "src");
      if (G_UNLIKELY (!src)) {
        GST_WARNING_OBJECT (self, "Can't get src pad from overlay");
        continue;
      }

      sink = gst_element_get_static_pad (self->post_colorspace, "sink");
      if (G_UNLIKELY (!sink)) {
951
        GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
952 953 954 955 956
        gst_object_unref (src);
        continue;
      }

      if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
957
        GST_WARNING_OBJECT (self, "Can't link overlay with videoconvert");
958 959 960 961 962 963 964 965
        gst_object_unref (src);
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (src);
      gst_object_unref (sink);

      if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
966
                  "videoconvert", NULL, "pre-colorspace", FALSE))) {
967 968 969 970 971 972 973 974 975 976 977
        continue;
      }

      sink = gst_element_get_static_pad (overlay, "video_sink");
      if (G_UNLIKELY (!sink)) {
        GST_WARNING_OBJECT (self, "Can't get video sink from textoverlay");
        continue;
      }

      src = gst_element_get_static_pad (self->pre_colorspace, "src");
      if (G_UNLIKELY (!src)) {
978
        GST_WARNING_OBJECT (self, "Can't get srcpad from videoconvert");
979 980 981 982 983
        gst_object_unref (sink);
        continue;
      }

      if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
984
        GST_WARNING_OBJECT (self, "Can't link videoconvert to textoverlay");
985 986 987 988 989 990 991 992 993 994
        gst_object_unref (src);
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (src);
      gst_object_unref (sink);

      /* Set src ghostpad target */
      src = gst_element_get_static_pad (self->post_colorspace, "src");
      if (G_UNLIKELY (!src)) {
995
        GST_WARNING_OBJECT (self, "Can't get src pad from videoconvert");
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->srcpad), src))) {
        GST_WARNING_OBJECT (self, "Can't set srcpad target");
        gst_object_unref (src);
        continue;
      }
      gst_object_unref (src);

      /* Send segments to the parser/overlay if necessary. These are not sent
       * outside this element because of the proxy pad event function */
1009
      if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
Wim Taymans's avatar
Wim Taymans committed
1010
        GstEvent *event1;
1011 1012 1013

        sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
        if (G_UNLIKELY (!sink)) {
1014
          GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
1015 1016 1017
          continue;
        }

Wim Taymans's avatar
Wim Taymans committed
1018
        _generate_update_segment_event (&self->video_segment, &event1);
1019
        GST_DEBUG_OBJECT (self,
Wim Taymans's avatar
Wim Taymans committed
1020
            "Pushing video segment event: %" GST_PTR_FORMAT, event1);
1021 1022 1023 1024 1025
        gst_pad_send_event (sink, event1);

        gst_object_unref (sink);
      }

1026
      if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
Wim Taymans's avatar
Wim Taymans committed
1027
        GstEvent *event1;
1028 1029 1030 1031 1032 1033 1034

        sink = gst_element_get_static_pad (element, "sink");
        if (G_UNLIKELY (!sink)) {
          GST_WARNING_OBJECT (self, "Failed to get subpad");
          continue;
        }

Wim Taymans's avatar
Wim Taymans committed
1035
        _generate_update_segment_event (&self->subtitle_segment, &event1);
1036
        GST_DEBUG_OBJECT (self,
Wim Taymans's avatar
Wim Taymans committed
1037
            "Pushing subtitle segment event: %" GST_PTR_FORMAT, event1);
1038 1039 1040 1041 1042 1043 1044 1045
        gst_pad_send_event (sink, event1);

        gst_object_unref (sink);
      }

      /* Set the sink ghostpad targets */
      sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
      if (G_UNLIKELY (!sink)) {
1046
        GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->video_sinkpad), sink))) {
        GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (sink);

      /* Link subtitle identity to subtitle pad of our element */
      sink = gst_element_get_static_pad (element, "sink");
      if (G_UNLIKELY (!sink)) {
        GST_WARNING_OBJECT (self, "Failed to get subpad");
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->subtitle_sinkpad), sink))) {
        GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (sink);
    } else {
      const gchar *name =
          gst_plugin_feature_get_name (GST_PLUGIN_FEATURE_CAST (factory));

      if (strcmp (name, "textoverlay") == 0) {
        /* Set some textoverlay specific properties */
        g_object_set (G_OBJECT (element),
            "halign", "center", "valign", "bottom", "wait-text", FALSE, NULL);
        if (self->font_desc)
          g_object_set (G_OBJECT (element), "font-desc", self->font_desc, NULL);
1082 1083 1084 1085 1086
        self->silent_property = "silent";
        self->silent_property_invert = FALSE;
      } else {
        self->silent_property =
            _get_silent_property (element, &self->silent_property_invert);
1087 1088 1089 1090 1091
        if (_has_subtitle_encoding_property (self->renderer))
          g_object_set (self->renderer, "subtitle-encoding", self->encoding,
              NULL);
        if (_has_font_desc_property (self->renderer))
          g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
1092 1093 1094 1095
      }

      /* First link everything internally */
      if (G_UNLIKELY (!_create_element (self, &self->post_colorspace,
1096
                  "videoconvert", NULL, "post-colorspace", FALSE))) {
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
        continue;
      }

      src = gst_element_get_static_pad (element, "src");
      if (G_UNLIKELY (!src)) {
        GST_WARNING_OBJECT (self, "Can't get src pad from renderer");
        continue;
      }

      sink = gst_element_get_static_pad (self->post_colorspace, "sink");
      if (G_UNLIKELY (!sink)) {
1108
        GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
1109 1110 1111 1112 1113
        gst_object_unref (src);
        continue;
      }

      if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
1114
        GST_WARNING_OBJECT (self, "Can't link renderer with videoconvert");
1115 1116 1117 1118 1119 1120 1121 1122
        gst_object_unref (src);
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (src);
      gst_object_unref (sink);

      if (G_UNLIKELY (!_create_element (self, &self->pre_colorspace,
1123
                  "videoconvert", NULL, "pre-colorspace", FALSE))) {
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
        continue;
      }

      sink = _get_video_pad (element);
      if (G_UNLIKELY (!sink)) {
        GST_WARNING_OBJECT (self, "Can't get video sink from renderer");
        continue;
      }

      src = gst_element_get_static_pad (self->pre_colorspace, "src");
      if (G_UNLIKELY (!src)) {
1135
        GST_WARNING_OBJECT (self, "Can't get srcpad from videoconvert");
1136 1137 1138 1139 1140
        gst_object_unref (sink);
        continue;
      }

      if (G_UNLIKELY (gst_pad_link (src, sink) != GST_PAD_LINK_OK)) {
1141
        GST_WARNING_OBJECT (self, "Can't link videoconvert to renderer");
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
        gst_object_unref (src);
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (src);
      gst_object_unref (sink);

      /* Set src ghostpad target */
      src = gst_element_get_static_pad (self->post_colorspace, "src");
      if (G_UNLIKELY (!src)) {
1152
        GST_WARNING_OBJECT (self, "Can't get src pad from videoconvert");
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->srcpad), src))) {
        GST_WARNING_OBJECT (self, "Can't set srcpad target");
        gst_object_unref (src);
        continue;
      }
      gst_object_unref (src);

      /* Send segments to the renderer if necessary. These are not sent
       * outside this element because of the proxy pad event handler */
1166
      if (self->video_segment.format != GST_FORMAT_UNDEFINED) {
Wim Taymans's avatar
Wim Taymans committed
1167
        GstEvent *event1;
1168 1169 1170

        sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
        if (G_UNLIKELY (!sink)) {
1171
          GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
1172 1173 1174
          continue;
        }

Wim Taymans's avatar
Wim Taymans committed
1175
        _generate_update_segment_event (&self->video_segment, &event1);
1176
        GST_DEBUG_OBJECT (self,
Wim Taymans's avatar
Wim Taymans committed
1177
            "Pushing video segment event: %" GST_PTR_FORMAT, event1);
1178 1179 1180 1181
        gst_pad_send_event (sink, event1);
        gst_object_unref (sink);
      }

1182
      if (self->subtitle_segment.format != GST_FORMAT_UNDEFINED) {
Wim Taymans's avatar
Wim Taymans committed
1183
        GstEvent *event1;
1184 1185 1186 1187 1188 1189 1190

        sink = _get_sub_pad (element);
        if (G_UNLIKELY (!sink)) {
          GST_WARNING_OBJECT (self, "Failed to get subpad");
          continue;
        }

Wim Taymans's avatar
Wim Taymans committed
1191
        _generate_update_segment_event (&self->subtitle_segment, &event1);
1192
        GST_DEBUG_OBJECT (self,
Wim Taymans's avatar
Wim Taymans committed
1193
            "Pushing subtitle segment event: %" GST_PTR_FORMAT, event1);
1194 1195 1196 1197 1198 1199 1200
        gst_pad_send_event (sink, event1);
        gst_object_unref (sink);
      }

      /* Set the sink ghostpad targets */
      sink = gst_element_get_static_pad (self->pre_colorspace, "sink");
      if (G_UNLIKELY (!sink)) {
1201
        GST_WARNING_OBJECT (self, "Can't get sink pad from videoconvert");
1202 1203 1204 1205 1206 1207 1208 1209 1210
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->video_sinkpad), sink))) {
        GST_WARNING_OBJECT (self, "Can't set video sinkpad target");
        gst_object_unref (sink);
        continue;
      }
1211
      gst_object_unref (sink);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

      sink = _get_sub_pad (element);
      if (G_UNLIKELY (!sink)) {
        GST_WARNING_OBJECT (self, "Failed to get subpad");
        continue;
      }

      if (G_UNLIKELY (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST
                  (self->subtitle_sinkpad), sink))) {
        GST_WARNING_OBJECT (self, "Failed to set subtitle sink target");
        gst_object_unref (sink);
        continue;
      }
      gst_object_unref (sink);
    }

    break;
  }

  if (G_UNLIKELY (l == NULL)) {
    GST_ELEMENT_WARNING (self, CORE, FAILED, (NULL),
        ("Failed to find any usable factories"));
1234
    self->subtitle_error = TRUE;
1235 1236 1237 1238
    _setup_passthrough (self);
    do_async_done (self);
  } else {
    GST_DEBUG_OBJECT (self, "Everything worked, unblocking pads");
1239 1240
    unblock_video (self);
    unblock_subtitle (self);
1241 1242 1243 1244 1245
    do_async_done (self);
  }

out:
  if (factories)
1246
    gst_plugin_feature_list_free (factories);
1247
  GST_SUBTITLE_OVERLAY_UNLOCK (self);
1248 1249

  return GST_PROBE_OK;
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
}

static GstStateChangeReturn
gst_subtitle_overlay_change_state (GstElement * element,
    GstStateChange transition)
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY (element);
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      GST_DEBUG_OBJECT (self, "State change NULL->READY");
1262 1263 1264
      g_mutex_lock (self->factories_lock);
      if (G_UNLIKELY (!gst_subtitle_overlay_update_factory_list (self))) {
        g_mutex_unlock (self->factories_lock);
1265
        return GST_STATE_CHANGE_FAILURE;
1266 1267
      }
      g_mutex_unlock (self->factories_lock);
1268 1269 1270

      GST_SUBTITLE_OVERLAY_LOCK (self);
      /* Set the internal pads to blocking */
1271 1272
      block_video (self);
      block_subtitle (self);
1273 1274 1275 1276 1277 1278 1279
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
      gst_segment_init (&self->video_segment, GST_FORMAT_UNDEFINED);
      gst_segment_init (&self->subtitle_segment, GST_FORMAT_UNDEFINED);

1280 1281
      self->fps_n = self->fps_d = 0;

1282
      self->subtitle_flush = FALSE;
1283 1284 1285
      self->subtitle_error = FALSE;

      self->downstream_chain_error = FALSE;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

      do_async_start (self);
      ret = GST_STATE_CHANGE_ASYNC;

      break;
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
      GST_DEBUG_OBJECT (self, "State change PAUSED->PLAYING");
    default:
      break;
  }

  {
    GstStateChangeReturn bret;

    bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
    GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", bret);
    if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
      return ret;
    else if (bret == GST_STATE_CHANGE_ASYNC)
      ret = bret;
    else if (G_UNLIKELY (bret == GST_STATE_CHANGE_NO_PREROLL)) {
      do_async_done (self);
      ret = bret;
    }
  }

  switch (transition) {
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
      GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
      do_async_done (self);

      break;
1321
    case GST_STATE_CHANGE_READY_TO_NULL:
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
      GST_DEBUG_OBJECT (self, "State change READY->NULL");

      GST_SUBTITLE_OVERLAY_LOCK (self);
      gst_caps_replace (&self->subcaps, NULL);

      /* Unlink ghost pads */
      gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->srcpad), NULL);
      gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->video_sinkpad), NULL);
      gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->subtitle_sinkpad),
          NULL);

      /* Unblock pads */
1334 1335
      unblock_video (self);
      unblock_subtitle (self);
1336 1337

      /* Remove elements */
1338
      self->silent_property = NULL;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
      _remove_element (self, &self->post_colorspace);
      _remove_element (self, &self->overlay);
      _remove_element (self, &self->parser);
      _remove_element (self, &self->renderer);
      _remove_element (self, &self->pre_colorspace);
      _remove_element (self, &self->passthrough_identity);
      GST_SUBTITLE_OVERLAY_UNLOCK (self);

      break;
    default:
      break;
  }

  return ret;
}

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
static void
gst_subtitle_overlay_handle_message (GstBin * bin, GstMessage * message)
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (bin);

  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
    GstObject *src = GST_MESSAGE_SRC (message);

    /* Convert error messages from the subtitle pipeline to
     * warnings and switch to passthrough mode */
    if (src && (
            (self->overlay
                && gst_object_has_ancestor (src,
                    GST_OBJECT_CAST (self->overlay))) || (self->parser
                && gst_object_has_ancestor (src,
                    GST_OBJECT_CAST (self->parser))) || (self->renderer
                && gst_object_has_ancestor (src,
                    GST_OBJECT_CAST (self->renderer))))) {
      GError *err = NULL;
      gchar *debug = NULL;
      GstMessage *wmsg;

      gst_message_parse_error (message, &err, &debug);
      GST_DEBUG_OBJECT (self,
          "Got error message from subtitle element %s: %s (%s)",
          GST_MESSAGE_SRC_NAME (message), GST_STR_NULL (err->message),
          GST_STR_NULL (debug));

      wmsg = gst_message_new_warning (src, err, debug);
      gst_message_unref (message);
      g_error_free (err);
      g_free (debug);
      message = wmsg;

      GST_SUBTITLE_OVERLAY_LOCK (self);
      self->subtitle_error = TRUE;

1392 1393
      block_subtitle (self);
      block_video (self);
1394 1395 1396 1397 1398 1399 1400
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
    }
  }

  GST_BIN_CLASS (parent_class)->handle_message (bin, message);
}

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
static void
gst_subtitle_overlay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);

  switch (prop_id) {
    case PROP_SILENT:
      g_value_set_boolean (value, self->silent);
      break;
    case PROP_FONT_DESC:
      GST_SUBTITLE_OVERLAY_LOCK (self);
      g_value_set_string (value, self->font_desc);
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
1416 1417 1418 1419 1420
    case PROP_SUBTITLE_ENCODING:
      GST_SUBTITLE_OVERLAY_LOCK (self);
      g_value_set_string (value, self->encoding);
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
1421 1422 1423 1424 1425 1426
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

1427 1428 1429 1430 1431 1432 1433
static void
gst_subtitle_overlay_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstSubtitleOverlay *self = GST_SUBTITLE_OVERLAY_CAST (object);

  switch (prop_id) {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    case PROP_SILENT:
      GST_SUBTITLE_OVERLAY_LOCK (self);
      self->silent = g_value_get_boolean (value);
      if (self->silent_property) {
        gboolean silent = self->silent;

        if (self->silent_property_invert)
          silent = !silent;

        if (self->overlay)
          g_object_set (self->overlay, self->silent_property, silent, NULL);
        else if (self->renderer)
          g_object_set (self->renderer, self->silent_property, silent, NULL);
      } else {
1448 1449
        block_subtitle (self);
        block_video (self);
1450 1451 1452
      }
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
1453 1454 1455 1456
    case PROP_FONT_DESC:
      GST_SUBTITLE_OVERLAY_LOCK (self);
      g_free (self->font_desc);
      self->font_desc = g_value_dup_string (value);
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
      if (self->overlay)
        g_object_set (self->overlay, "font-desc", self->font_desc, NULL);
      else if (self->renderer && _has_font_desc_property (self->renderer))
        g_object_set (self->renderer, "font-desc", self->font_desc, NULL);
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
    case PROP_SUBTITLE_ENCODING:
      GST_SUBTITLE_OVERLAY_LOCK (self);
      g_free (self->encoding);
      self->encoding = g_value_dup_string (value);
      if (self->renderer && _has_subtitle_encoding_property (self->renderer))
        g_object_set (self->renderer, "subtitle-encoding", self->encoding,
            NULL);
      if (self->parser && _has_subtitle_encoding_property (self->parser))
        g_object_set (self->parser, "subtitle-encoding", self->encoding, NULL);
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
      GST_SUBTITLE_OVERLAY_UNLOCK (self);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_subtitle_overlay_class_init (GstSubtitleOverlayClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *element_class = (GstElementClass *) klass;
1485
  GstBinClass *bin_class = (GstBinClass *) klass;
1486 1487 1488

  gobject_class->finalize = gst_subtitle_overlay_finalize;
  gobject_class->set_property = gst_subtitle_overlay_set_property;
1489 1490 1491 1492 1493 1494 1495
  gobject_class->get_property = gst_subtitle_overlay_get_property;

  g_object_class_install_property (gobject_class, PROP_SILENT,
      g_param_spec_boolean ("silent",
          "Silent",
          "Whether to show subtitles", FALSE,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1496 1497 1498 1499 1500 1501

  g_object_class_install_property (gobject_class, PROP_FONT_DESC,
      g_param_spec_string ("font-desc",
          "Subtitle font description",
          "Pango font description of font "
          "to be used for subtitle rendering", NULL,
1502
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1503

1504 1505 1506 1507 1508 1509 1510 1511
  g_object_class_install_property (gobject_class, PROP_SUBTITLE_ENCODING,
      g_param_spec_string ("subtitle-encoding", "subtitle encoding",
          "Encoding to assume if input subtitles are not in UTF-8 encoding. "
          "If not set, the GST_SUBTITLE_ENCODING environment variable will "
          "be checked for an encoding to use. If that is not set either, "
          "ISO-8859-15 will be assumed.", NULL,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&srctemplate));

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&video_sinktemplate));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&subtitle_sinktemplate));

  gst_element_class_set_details_simple (element_class, "Subtitle Overlay",
      "Video/Overlay/Subtitle",
      "Overlays a video stream with subtitles",
      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");

1525 1526
  element_class->change_state =
      GST_DEBUG_FUNCPTR (gst_subtitle_overlay_change_state);
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

  bin_class->handle_message =
      GST_DEBUG_FUNCPTR (gst_subtitle_overlay_handle_message);
}

static GstFlowReturn
gst_subtitle_overlay_src_proxy_chain (GstPad * proxypad, GstBuffer * buffer)
{
  GstPad *ghostpad;
  GstSubtitleOverlay *self;
  GstFlowReturn ret;

  ghostpad = GST_PAD_CAST (gst_pad_get_parent (proxypad));
  if (G_UNLIKELY (!ghostpad)) {
    gst_buffer_unref (buffer);
    return GST_FLOW_ERROR;
  }
  self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
  if (G_UNLIKELY (!self || self->srcpad != ghostpad)) {
    gst_buffer_unref (buffer);
    gst_object_unref (ghostpad);
    return GST_FLOW_ERROR;
  }

1551
  ret = gst_proxy_pad_chain_default (proxypad, buffer);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562

  if (IS_VIDEO_CHAIN_IGNORE_ERROR (ret)) {
    GST_ERROR_OBJECT (self, "Downstream chain error: %s",
        gst_flow_get_name (ret));
    self->downstream_chain_error = TRUE;
  }

  gst_object_unref (self);
  gst_object_unref (ghostpad);

  return ret;
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
}

static gboolean
gst_subtitle_overlay_src_proxy_event (GstPad * proxypad, GstEvent * event)
{
  GstPad *ghostpad = NULL;
  GstSubtitleOverlay *self = NULL;
  gboolean ret = FALSE;
  const GstStructure *s;

  ghostpad = GST_PAD_CAST (gst_pad_get_parent (proxypad));
  if (G_UNLIKELY (!ghostpad))
    goto out;
  self = GST_SUBTITLE_OVERLAY_CAST (gst_pad_get_parent (ghostpad));
  if (G_UNLIKELY (!self || self->srcpad != ghostpad))
    goto out;

  s = gst_event_get_structure (event);
  if (s && gst_structure_id_has_field (s, _subtitle_overlay_event_marker_id)) {
    GST_DEBUG_OBJECT (ghostpad, "Dropping event with marker: %" GST_PTR_FORMAT,
1583
        event);