Blame gst-libs/gst/tag/gsttagmux.c

Packit Service 4387a0
/* GStreamer tag muxer base class
Packit Service 4387a0
 * Copyright (C) 2006 Christophe Fergeau  <teuf@gnome.org>
Packit Service 4387a0
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
Packit Service 4387a0
 * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
Packit Service 4387a0
 * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
Packit Service 4387a0
 *
Packit Service 4387a0
 * This library is free software; you can redistribute it and/or
Packit Service 4387a0
 * modify it under the terms of the GNU Library General Public
Packit Service 4387a0
 * License as published by the Free Software Foundation; either
Packit Service 4387a0
 * version 2 of the License, or (at your option) any later version.
Packit Service 4387a0
 *
Packit Service 4387a0
 * This library is distributed in the hope that it will be useful,
Packit Service 4387a0
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4387a0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4387a0
 * Library General Public License for more details.
Packit Service 4387a0
 *
Packit Service 4387a0
 * You should have received a copy of the GNU Library General Public
Packit Service 4387a0
 * License along with this library; if not, write to the
Packit Service 4387a0
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit Service 4387a0
 * Boston, MA 02110-1301, USA.
Packit Service 4387a0
 */
Packit Service 4387a0
Packit Service 4387a0
/**
Packit Service 4387a0
 * SECTION:gsttagmux
Packit Service 4387a0
 * @title: GstTagMux
Packit Service 4387a0
 * @see_also: GstApeMux, GstId3Mux
Packit Service 4387a0
 * @short_description: Base class for adding tags that are in one single chunk
Packit Service 4387a0
 *                     directly at the beginning or at the end of a file
Packit Service 4387a0
 *
Packit Service 4387a0
 * Provides a base class for adding tags at the beginning or end of a
Packit Service 4387a0
 * stream.
Packit Service 4387a0
 *
Packit Service 4387a0
 * ## Deriving from GstTagMux
Packit Service 4387a0
 *
Packit Service 4387a0
 * Subclasses have to do the following things:
Packit Service 4387a0
 *
Packit Service 4387a0
 *  * In their base init function, they must add pad templates for the sink
Packit Service 4387a0
 *    pad and the source pad to the element class, describing the media type
Packit Service 4387a0
 *    they accept and output in the caps of the pad template.
Packit Service 4387a0
 *  * In their class init function, they must override the
Packit Service 4387a0
 *    GST_TAG_MUX_CLASS(mux_klass)->render_start_tag and/or
Packit Service 4387a0
 *    GST_TAG_MUX_CLASS(mux_klass)->render_end_tag vfuncs and set up a render
Packit Service 4387a0
 *    function.
Packit Service 4387a0
 *
Packit Service 4387a0
 */
Packit Service 4387a0
#ifdef HAVE_CONFIG_H
Packit Service 4387a0
#include <config.h>
Packit Service 4387a0
#endif
Packit Service 4387a0
Packit Service 4387a0
#include <string.h>
Packit Service 4387a0
#include <gst/gsttagsetter.h>
Packit Service 4387a0
#include <gst/tag/tag.h>
Packit Service 4387a0
Packit Service 4387a0
#include "gsttagmux.h"
Packit Service 4387a0
Packit Service 4387a0
struct _GstTagMuxPrivate
Packit Service 4387a0
{
Packit Service 4387a0
  GstPad *srcpad;
Packit Service 4387a0
  GstPad *sinkpad;
Packit Service 4387a0
  GstTagList *event_tags;       /* tags received from upstream elements */
Packit Service 4387a0
  GstTagList *final_tags;       /* Final set of tags used for muxing */
Packit Service 4387a0
  gsize start_tag_size;
Packit Service 4387a0
  gsize end_tag_size;
Packit Service 4387a0
  gboolean render_start_tag;
Packit Service 4387a0
  gboolean render_end_tag;
Packit Service 4387a0
Packit Service 4387a0
  gint64 current_offset;
Packit Service 4387a0
  gint64 max_offset;
Packit Service 4387a0
Packit Service 4387a0
  GstEvent *newsegment_ev;      /* cached newsegment event from upstream */
Packit Service 4387a0
};
Packit Service 4387a0
Packit Service 4387a0
GST_DEBUG_CATEGORY_STATIC (gst_tag_mux_debug);
Packit Service 4387a0
#define GST_CAT_DEFAULT gst_tag_mux_debug
Packit Service 4387a0
Packit Service 4387a0
static GstElementClass *parent_class;
Packit Service 4387a0
static gint private_offset = 0;
Packit Service 4387a0
Packit Service 4387a0
static void gst_tag_mux_class_init (GstTagMuxClass * klass);
Packit Service 4387a0
static void gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class);
Packit Service 4387a0
static GstStateChangeReturn
Packit Service 4387a0
gst_tag_mux_change_state (GstElement * element, GstStateChange transition);
Packit Service 4387a0
static GstFlowReturn gst_tag_mux_chain (GstPad * pad, GstObject * parent,
Packit Service 4387a0
    GstBuffer * buffer);
Packit Service 4387a0
static gboolean gst_tag_mux_sink_event (GstPad * pad, GstObject * parent,
Packit Service 4387a0
    GstEvent * event);
Packit Service 4387a0
Packit Service 4387a0
/* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
Packit Service 4387a0
 * method to get to the padtemplates */
Packit Service 4387a0
GType
Packit Service 4387a0
gst_tag_mux_get_type (void)
Packit Service 4387a0
{
Packit Service 4387a0
  static volatile gsize tag_mux_type = 0;
Packit Service 4387a0
Packit Service 4387a0
  if (g_once_init_enter (&tag_mux_type)) {
Packit Service 4387a0
    const GInterfaceInfo interface_info = { NULL, NULL, NULL };
Packit Service 4387a0
    GType _type;
Packit Service 4387a0
Packit Service 4387a0
    _type = g_type_register_static_simple (GST_TYPE_ELEMENT,
Packit Service 4387a0
        "GstTagMux", sizeof (GstTagMuxClass),
Packit Service 4387a0
        (GClassInitFunc) gst_tag_mux_class_init, sizeof (GstTagMux),
Packit Service 4387a0
        (GInstanceInitFunc) gst_tag_mux_init, G_TYPE_FLAG_ABSTRACT);
Packit Service 4387a0
Packit Service 4387a0
    private_offset =
Packit Service 4387a0
        g_type_add_instance_private (_type, sizeof (GstTagMuxPrivate));
Packit Service 4387a0
Packit Service 4387a0
    g_type_add_interface_static (_type, GST_TYPE_TAG_SETTER, &interface_info);
Packit Service 4387a0
Packit Service 4387a0
    g_once_init_leave (&tag_mux_type, _type);
Packit Service 4387a0
  }
Packit Service 4387a0
  return tag_mux_type;
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static inline GstTagMuxPrivate *
Packit Service 4387a0
gst_tag_mux_get_instance_private (GstTagMux * self)
Packit Service 4387a0
{
Packit Service 4387a0
  return (G_STRUCT_MEMBER_P (self, private_offset));
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static void
Packit Service 4387a0
gst_tag_mux_finalize (GObject * obj)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMux *mux = GST_TAG_MUX (obj);
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->newsegment_ev) {
Packit Service 4387a0
    gst_event_unref (mux->priv->newsegment_ev);
Packit Service 4387a0
    mux->priv->newsegment_ev = NULL;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->event_tags) {
Packit Service 4387a0
    gst_tag_list_unref (mux->priv->event_tags);
Packit Service 4387a0
    mux->priv->event_tags = NULL;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->final_tags) {
Packit Service 4387a0
    gst_tag_list_unref (mux->priv->final_tags);
Packit Service 4387a0
    mux->priv->final_tags = NULL;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  G_OBJECT_CLASS (parent_class)->finalize (obj);
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static void
Packit Service 4387a0
gst_tag_mux_class_init (GstTagMuxClass * klass)
Packit Service 4387a0
{
Packit Service 4387a0
  GObjectClass *gobject_class;
Packit Service 4387a0
  GstElementClass *gstelement_class;
Packit Service 4387a0
Packit Service 4387a0
  gobject_class = (GObjectClass *) klass;
Packit Service 4387a0
  gstelement_class = (GstElementClass *) klass;
Packit Service 4387a0
Packit Service 4387a0
  parent_class = g_type_class_peek_parent (klass);
Packit Service 4387a0
Packit Service 4387a0
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tag_mux_finalize);
Packit Service 4387a0
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_tag_mux_change_state);
Packit Service 4387a0
Packit Service 4387a0
  if (private_offset != 0)
Packit Service 4387a0
    g_type_class_adjust_private_offset (klass, &private_offset);
Packit Service 4387a0
Packit Service 4387a0
  GST_DEBUG_CATEGORY_INIT (gst_tag_mux_debug, "tagmux", 0,
Packit Service 4387a0
      "tag muxer base class");
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static void
Packit Service 4387a0
gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class)
Packit Service 4387a0
{
Packit Service 4387a0
  GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
Packit Service 4387a0
  GstPadTemplate *tmpl;
Packit Service 4387a0
Packit Service 4387a0
  mux->priv = gst_tag_mux_get_instance_private (mux);
Packit Service 4387a0
Packit Service 4387a0
  /* pad through which data comes in to the element */
Packit Service 4387a0
  tmpl = gst_element_class_get_pad_template (element_klass, "sink");
Packit Service 4387a0
  if (tmpl) {
Packit Service 4387a0
    mux->priv->sinkpad = gst_pad_new_from_template (tmpl, "sink");
Packit Service 4387a0
  } else {
Packit Service 4387a0
    g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
Packit Service 4387a0
        G_OBJECT_CLASS_NAME (element_klass), "sink");
Packit Service 4387a0
    mux->priv->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
Packit Service 4387a0
  }
Packit Service 4387a0
  gst_pad_set_chain_function (mux->priv->sinkpad,
Packit Service 4387a0
      GST_DEBUG_FUNCPTR (gst_tag_mux_chain));
Packit Service 4387a0
  gst_pad_set_event_function (mux->priv->sinkpad,
Packit Service 4387a0
      GST_DEBUG_FUNCPTR (gst_tag_mux_sink_event));
Packit Service 4387a0
  gst_element_add_pad (GST_ELEMENT (mux), mux->priv->sinkpad);
Packit Service 4387a0
Packit Service 4387a0
  /* pad through which data goes out of the element */
Packit Service 4387a0
  tmpl = gst_element_class_get_pad_template (element_klass, "src");
Packit Service 4387a0
  if (tmpl) {
Packit Service 4387a0
    GstCaps *tmpl_caps = gst_pad_template_get_caps (tmpl);
Packit Service 4387a0
Packit Service 4387a0
    mux->priv->srcpad = gst_pad_new_from_template (tmpl, "src");
Packit Service 4387a0
    gst_pad_use_fixed_caps (mux->priv->srcpad);
Packit Service 4387a0
    if (tmpl_caps != NULL && gst_caps_is_fixed (tmpl_caps)) {
Packit Service 4387a0
      gst_pad_set_caps (mux->priv->srcpad, tmpl_caps);
Packit Service 4387a0
    }
Packit Service 4387a0
  } else {
Packit Service 4387a0
    g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
Packit Service 4387a0
        G_OBJECT_CLASS_NAME (element_klass), "source");
Packit Service 4387a0
    mux->priv->srcpad = gst_pad_new ("src", GST_PAD_SRC);
Packit Service 4387a0
  }
Packit Service 4387a0
  gst_element_add_pad (GST_ELEMENT (mux), mux->priv->srcpad);
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->render_start_tag = TRUE;
Packit Service 4387a0
  mux->priv->render_end_tag = TRUE;
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static GstTagList *
Packit Service 4387a0
gst_tag_mux_get_tags (GstTagMux * mux)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagSetter *tagsetter = GST_TAG_SETTER (mux);
Packit Service 4387a0
  const GstTagList *tagsetter_tags;
Packit Service 4387a0
  GstTagMergeMode merge_mode;
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->final_tags)
Packit Service 4387a0
    return mux->priv->final_tags;
Packit Service 4387a0
Packit Service 4387a0
  tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
Packit Service 4387a0
  merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
Packit Service 4387a0
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->priv->event_tags);
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "set   tags: %" GST_PTR_FORMAT, tagsetter_tags);
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->final_tags =
Packit Service 4387a0
      gst_tag_list_merge (tagsetter_tags, mux->priv->event_tags, merge_mode);
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->final_tags == NULL)
Packit Service 4387a0
    mux->priv->final_tags = gst_tag_list_new_empty ();
Packit Service 4387a0
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, mux->priv->final_tags);
Packit Service 4387a0
Packit Service 4387a0
  return mux->priv->final_tags;
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static GstFlowReturn
Packit Service 4387a0
gst_tag_mux_render_start_tag (GstTagMux * mux)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMuxClass *klass;
Packit Service 4387a0
  GstBuffer *buffer;
Packit Service 4387a0
  GstTagList *taglist;
Packit Service 4387a0
  GstEvent *event;
Packit Service 4387a0
  GstFlowReturn ret;
Packit Service 4387a0
  GstSegment segment;
Packit Service 4387a0
Packit Service 4387a0
  taglist = gst_tag_mux_get_tags (mux);
Packit Service 4387a0
Packit Service 4387a0
  klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
Packit Service 4387a0
Packit Service 4387a0
  if (klass->render_start_tag == NULL)
Packit Service 4387a0
    goto no_vfunc;
Packit Service 4387a0
Packit Service 4387a0
  buffer = klass->render_start_tag (mux, taglist);
Packit Service 4387a0
Packit Service 4387a0
  /* Null buffer is ok, just means we're not outputting anything */
Packit Service 4387a0
  if (buffer == NULL) {
Packit Service 4387a0
    GST_INFO_OBJECT (mux, "No start tag generated");
Packit Service 4387a0
    mux->priv->start_tag_size = 0;
Packit Service 4387a0
    return GST_FLOW_OK;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->start_tag_size = gst_buffer_get_size (buffer);
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
Packit Service 4387a0
      mux->priv->start_tag_size);
Packit Service 4387a0
Packit Service 4387a0
  /* Send newsegment event from byte position 0, so the tag really gets
Packit Service 4387a0
   * written to the start of the file, independent of the upstream segment */
Packit Service 4387a0
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit Service 4387a0
  gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
Packit Service 4387a0
Packit Service 4387a0
  /* Send an event about the new tags to downstream elements */
Packit Service 4387a0
  /* gst_event_new_tag takes ownership of the list, so use a copy */
Packit Service 4387a0
  event = gst_event_new_tag (gst_tag_list_ref (taglist));
Packit Service 4387a0
  gst_pad_push_event (mux->priv->srcpad, event);
Packit Service 4387a0
Packit Service 4387a0
  GST_BUFFER_OFFSET (buffer) = 0;
Packit Service 4387a0
  ret = gst_pad_push (mux->priv->srcpad, buffer);
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->current_offset = mux->priv->start_tag_size;
Packit Service 4387a0
  mux->priv->max_offset =
Packit Service 4387a0
      MAX (mux->priv->max_offset, mux->priv->current_offset);
Packit Service 4387a0
Packit Service 4387a0
  return ret;
Packit Service 4387a0
Packit Service 4387a0
no_vfunc:
Packit Service 4387a0
  {
Packit Service 4387a0
    GST_ERROR_OBJECT (mux, "Subclass does not implement "
Packit Service 4387a0
        "render_start_tag vfunc!");
Packit Service 4387a0
    return GST_FLOW_ERROR;
Packit Service 4387a0
  }
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static GstFlowReturn
Packit Service 4387a0
gst_tag_mux_render_end_tag (GstTagMux * mux)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMuxClass *klass;
Packit Service 4387a0
  GstBuffer *buffer;
Packit Service 4387a0
  GstTagList *taglist;
Packit Service 4387a0
  GstFlowReturn ret;
Packit Service 4387a0
  GstSegment segment;
Packit Service 4387a0
Packit Service 4387a0
  taglist = gst_tag_mux_get_tags (mux);
Packit Service 4387a0
Packit Service 4387a0
  klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
Packit Service 4387a0
Packit Service 4387a0
  if (klass->render_end_tag == NULL)
Packit Service 4387a0
    goto no_vfunc;
Packit Service 4387a0
Packit Service 4387a0
  buffer = klass->render_end_tag (mux, taglist);
Packit Service 4387a0
Packit Service 4387a0
  if (buffer == NULL) {
Packit Service 4387a0
    GST_INFO_OBJECT (mux, "No end tag generated");
Packit Service 4387a0
    mux->priv->end_tag_size = 0;
Packit Service 4387a0
    return GST_FLOW_OK;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->end_tag_size = gst_buffer_get_size (buffer);
Packit Service 4387a0
  GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
Packit Service 4387a0
      mux->priv->end_tag_size);
Packit Service 4387a0
Packit Service 4387a0
  /* Send newsegment event from the end of the file, so it gets written there,
Packit Service 4387a0
     independent of whatever new segment events upstream has sent us */
Packit Service 4387a0
  gst_segment_init (&segment, GST_FORMAT_BYTES);
Packit Service 4387a0
  segment.start = mux->priv->max_offset;
Packit Service 4387a0
  gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
Packit Service 4387a0
Packit Service 4387a0
  GST_BUFFER_OFFSET (buffer) = mux->priv->max_offset;
Packit Service 4387a0
  ret = gst_pad_push (mux->priv->srcpad, buffer);
Packit Service 4387a0
Packit Service 4387a0
  return ret;
Packit Service 4387a0
Packit Service 4387a0
no_vfunc:
Packit Service 4387a0
  {
Packit Service 4387a0
    GST_ERROR_OBJECT (mux, "Subclass does not implement "
Packit Service 4387a0
        "render_end_tag vfunc!");
Packit Service 4387a0
    return GST_FLOW_ERROR;
Packit Service 4387a0
  }
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static GstEvent *
Packit Service 4387a0
gst_tag_mux_adjust_event_offsets (GstTagMux * mux,
Packit Service 4387a0
    const GstEvent * newsegment_event)
Packit Service 4387a0
{
Packit Service 4387a0
  GstSegment segment;
Packit Service 4387a0
Packit Service 4387a0
  gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
Packit Service 4387a0
Packit Service 4387a0
  g_assert (segment.format == GST_FORMAT_BYTES);
Packit Service 4387a0
Packit Service 4387a0
  if (segment.start != -1)
Packit Service 4387a0
    segment.start += mux->priv->start_tag_size;
Packit Service 4387a0
  if (segment.stop != -1)
Packit Service 4387a0
    segment.stop += mux->priv->start_tag_size;
Packit Service 4387a0
  if (segment.time != -1)
Packit Service 4387a0
    segment.time += mux->priv->start_tag_size;
Packit Service 4387a0
Packit Service 4387a0
  GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
Packit Service 4387a0
      G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
Packit Service 4387a0
      " (delta = +%" G_GSIZE_FORMAT ")", segment.start, segment.stop,
Packit Service 4387a0
      segment.time, mux->priv->start_tag_size);
Packit Service 4387a0
Packit Service 4387a0
  return gst_event_new_segment (&segment);
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static GstFlowReturn
Packit Service 4387a0
gst_tag_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMux *mux = GST_TAG_MUX (parent);
Packit Service 4387a0
  GstFlowReturn ret;
Packit Service 4387a0
  int length;
Packit Service 4387a0
Packit Service 4387a0
  if (mux->priv->render_start_tag) {
Packit Service 4387a0
Packit Service 4387a0
    GST_INFO_OBJECT (mux, "Adding tags to stream");
Packit Service 4387a0
    ret = gst_tag_mux_render_start_tag (mux);
Packit Service 4387a0
    if (ret != GST_FLOW_OK) {
Packit Service 4387a0
      GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
Packit Service 4387a0
      gst_buffer_unref (buffer);
Packit Service 4387a0
      return ret;
Packit Service 4387a0
    }
Packit Service 4387a0
Packit Service 4387a0
    /* Now send the cached newsegment event that we got from upstream */
Packit Service 4387a0
    if (mux->priv->newsegment_ev) {
Packit Service 4387a0
      GstEvent *newseg;
Packit Service 4387a0
      GstSegment segment;
Packit Service 4387a0
Packit Service 4387a0
      GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
Packit Service 4387a0
      newseg = gst_tag_mux_adjust_event_offsets (mux, mux->priv->newsegment_ev);
Packit Service 4387a0
      gst_event_unref (mux->priv->newsegment_ev);
Packit Service 4387a0
      mux->priv->newsegment_ev = NULL;
Packit Service 4387a0
Packit Service 4387a0
      gst_event_copy_segment (newseg, &segment);
Packit Service 4387a0
Packit Service 4387a0
      gst_pad_push_event (mux->priv->srcpad, newseg);
Packit Service 4387a0
      mux->priv->current_offset = segment.start;
Packit Service 4387a0
      mux->priv->max_offset =
Packit Service 4387a0
          MAX (mux->priv->max_offset, mux->priv->current_offset);
Packit Service 4387a0
    } else {
Packit Service 4387a0
      /* upstream sent no newsegment event or only one in a non-BYTE format */
Packit Service 4387a0
    }
Packit Service 4387a0
Packit Service 4387a0
    mux->priv->render_start_tag = FALSE;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  buffer = gst_buffer_make_writable (buffer);
Packit Service 4387a0
Packit Service 4387a0
  if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
Packit Service 4387a0
    GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
Packit Service 4387a0
        " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
Packit Service 4387a0
        GST_BUFFER_OFFSET (buffer) + mux->priv->start_tag_size);
Packit Service 4387a0
    GST_BUFFER_OFFSET (buffer) += mux->priv->start_tag_size;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  length = gst_buffer_get_size (buffer);
Packit Service 4387a0
Packit Service 4387a0
  ret = gst_pad_push (mux->priv->srcpad, buffer);
Packit Service 4387a0
Packit Service 4387a0
  mux->priv->current_offset += length;
Packit Service 4387a0
  mux->priv->max_offset =
Packit Service 4387a0
      MAX (mux->priv->max_offset, mux->priv->current_offset);
Packit Service 4387a0
Packit Service 4387a0
  return ret;
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
static gboolean
Packit Service 4387a0
gst_tag_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMux *mux;
Packit Service 4387a0
  gboolean result;
Packit Service 4387a0
Packit Service 4387a0
  mux = GST_TAG_MUX (parent);
Packit Service 4387a0
  result = FALSE;
Packit Service 4387a0
Packit Service 4387a0
  switch (GST_EVENT_TYPE (event)) {
Packit Service 4387a0
    case GST_EVENT_TAG:{
Packit Service 4387a0
      GstTagList *tags;
Packit Service 4387a0
Packit Service 4387a0
      gst_event_parse_tag (event, &tags);
Packit Service 4387a0
Packit Service 4387a0
      GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
Packit Service 4387a0
Packit Service 4387a0
      if (mux->priv->event_tags != NULL) {
Packit Service 4387a0
        gst_tag_list_insert (mux->priv->event_tags, tags,
Packit Service 4387a0
            GST_TAG_MERGE_REPLACE);
Packit Service 4387a0
      } else {
Packit Service 4387a0
        mux->priv->event_tags = gst_tag_list_copy (tags);
Packit Service 4387a0
      }
Packit Service 4387a0
Packit Service 4387a0
      GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
Packit Service 4387a0
          mux->priv->event_tags);
Packit Service 4387a0
Packit Service 4387a0
      /* just drop the event, we'll push a new tag event in render_start_tag */
Packit Service 4387a0
      gst_event_unref (event);
Packit Service 4387a0
      result = TRUE;
Packit Service 4387a0
      break;
Packit Service 4387a0
    }
Packit Service 4387a0
    case GST_EVENT_SEGMENT:
Packit Service 4387a0
    {
Packit Service 4387a0
      GstSegment segment;
Packit Service 4387a0
Packit Service 4387a0
      gst_event_copy_segment (event, &segment);
Packit Service 4387a0
Packit Service 4387a0
      if (segment.format != GST_FORMAT_BYTES) {
Packit Service 4387a0
        GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
Packit Service 4387a0
            gst_format_get_name (segment.format));
Packit Service 4387a0
        gst_event_unref (event);
Packit Service 4387a0
        /* drop it quietly, so it is not seen as a failure to push event,
Packit Service 4387a0
         * which will turn into failure to push data as it is sticky */
Packit Service 4387a0
        result = TRUE;
Packit Service 4387a0
        break;
Packit Service 4387a0
      }
Packit Service 4387a0
Packit Service 4387a0
      if (mux->priv->render_start_tag) {
Packit Service 4387a0
        /* we have not rendered the tag yet, which means that we don't know
Packit Service 4387a0
         * how large it is going to be yet, so we can't adjust the offsets
Packit Service 4387a0
         * here at this point and need to cache the newsegment event for now
Packit Service 4387a0
         * (also, there could be tag events coming after this newsegment event
Packit Service 4387a0
         *  and before the first buffer). */
Packit Service 4387a0
        if (mux->priv->newsegment_ev) {
Packit Service 4387a0
          GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
Packit Service 4387a0
          gst_event_unref (mux->priv->newsegment_ev);
Packit Service 4387a0
        }
Packit Service 4387a0
Packit Service 4387a0
        GST_LOG_OBJECT (mux, "caching newsegment event for later");
Packit Service 4387a0
        mux->priv->newsegment_ev = event;
Packit Service 4387a0
      } else {
Packit Service 4387a0
        GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
Packit Service 4387a0
        gst_pad_push_event (mux->priv->srcpad,
Packit Service 4387a0
            gst_tag_mux_adjust_event_offsets (mux, event));
Packit Service 4387a0
        gst_event_unref (event);
Packit Service 4387a0
Packit Service 4387a0
        mux->priv->current_offset = segment.start;
Packit Service 4387a0
        mux->priv->max_offset =
Packit Service 4387a0
            MAX (mux->priv->max_offset, mux->priv->current_offset);
Packit Service 4387a0
      }
Packit Service 4387a0
      event = NULL;
Packit Service 4387a0
      result = TRUE;
Packit Service 4387a0
      break;
Packit Service 4387a0
    }
Packit Service 4387a0
    case GST_EVENT_EOS:{
Packit Service 4387a0
      if (mux->priv->render_end_tag) {
Packit Service 4387a0
        GstFlowReturn ret;
Packit Service 4387a0
Packit Service 4387a0
        GST_INFO_OBJECT (mux, "Adding tags to stream");
Packit Service 4387a0
        ret = gst_tag_mux_render_end_tag (mux);
Packit Service 4387a0
        if (ret != GST_FLOW_OK) {
Packit Service 4387a0
          GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
Packit Service 4387a0
          return ret;
Packit Service 4387a0
        }
Packit Service 4387a0
Packit Service 4387a0
        mux->priv->render_end_tag = FALSE;
Packit Service 4387a0
      }
Packit Service 4387a0
Packit Service 4387a0
      /* Now forward EOS */
Packit Service 4387a0
      result = gst_pad_event_default (pad, parent, event);
Packit Service 4387a0
      break;
Packit Service 4387a0
    }
Packit Service 4387a0
    default:
Packit Service 4387a0
      result = gst_pad_event_default (pad, parent, event);
Packit Service 4387a0
      break;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  return result;
Packit Service 4387a0
}
Packit Service 4387a0
Packit Service 4387a0
Packit Service 4387a0
static GstStateChangeReturn
Packit Service 4387a0
gst_tag_mux_change_state (GstElement * element, GstStateChange transition)
Packit Service 4387a0
{
Packit Service 4387a0
  GstTagMux *mux;
Packit Service 4387a0
  GstStateChangeReturn result;
Packit Service 4387a0
Packit Service 4387a0
  mux = GST_TAG_MUX (element);
Packit Service 4387a0
Packit Service 4387a0
  result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
Packit Service 4387a0
  if (result != GST_STATE_CHANGE_SUCCESS) {
Packit Service 4387a0
    return result;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  switch (transition) {
Packit Service 4387a0
    case GST_STATE_CHANGE_PAUSED_TO_READY:{
Packit Service 4387a0
      if (mux->priv->newsegment_ev) {
Packit Service 4387a0
        gst_event_unref (mux->priv->newsegment_ev);
Packit Service 4387a0
        mux->priv->newsegment_ev = NULL;
Packit Service 4387a0
      }
Packit Service 4387a0
      if (mux->priv->event_tags) {
Packit Service 4387a0
        gst_tag_list_unref (mux->priv->event_tags);
Packit Service 4387a0
        mux->priv->event_tags = NULL;
Packit Service 4387a0
      }
Packit Service 4387a0
      if (mux->priv->final_tags) {
Packit Service 4387a0
        gst_tag_list_unref (mux->priv->final_tags);
Packit Service 4387a0
        mux->priv->final_tags = NULL;
Packit Service 4387a0
      }
Packit Service 4387a0
      mux->priv->start_tag_size = 0;
Packit Service 4387a0
      mux->priv->end_tag_size = 0;
Packit Service 4387a0
      mux->priv->render_start_tag = TRUE;
Packit Service 4387a0
      mux->priv->render_end_tag = TRUE;
Packit Service 4387a0
      mux->priv->current_offset = 0;
Packit Service 4387a0
      mux->priv->max_offset = 0;
Packit Service 4387a0
      break;
Packit Service 4387a0
    }
Packit Service 4387a0
    default:
Packit Service 4387a0
      break;
Packit Service 4387a0
  }
Packit Service 4387a0
Packit Service 4387a0
  return result;
Packit Service 4387a0
}