Blame gst-libs/gst/video/gstvideosink.c

Packit 0652a1
/*  GStreamer video sink base class
Packit 0652a1
 *  Copyright (C) <2003> Julien Moutte <julien@moutte.net>
Packit 0652a1
 *  Copyright (C) <2009> Tim-Philipp Müller <tim centricular net>
Packit 0652a1
 *
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * SECTION:gstvideosink
Packit 0652a1
 * @title: GstVideoSink
Packit 0652a1
 * @short_description: Base class for video sinks
Packit 0652a1
 *
Packit 0652a1
 * Provides useful functions and a base class for video sinks.
Packit 0652a1
 *
Packit 0652a1
 * GstVideoSink will configure the default base sink to drop frames that
Packit 0652a1
 * arrive later than 20ms as this is considered the default threshold for
Packit 0652a1
 * observing out-of-sync frames.
Packit 0652a1
 *
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include "config.h"
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include "gstvideosink.h"
Packit 0652a1
Packit 0652a1
enum
Packit 0652a1
{
Packit 0652a1
  PROP_SHOW_PREROLL_FRAME = 1
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
#define DEFAULT_SHOW_PREROLL_FRAME TRUE
Packit 0652a1
Packit 0652a1
struct _GstVideoSinkPrivate
Packit 0652a1
{
Packit 0652a1
  gboolean show_preroll_frame;  /* ATOMIC */
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
G_DEFINE_TYPE_WITH_PRIVATE (GstVideoSink, gst_video_sink, GST_TYPE_BASE_SINK);
Packit 0652a1
Packit 0652a1
#ifndef GST_DISABLE_GST_DEBUG
Packit 0652a1
#define GST_CAT_DEFAULT gst_video_sink_ensure_debug_category()
Packit 0652a1
Packit 0652a1
static GstDebugCategory *
Packit 0652a1
gst_video_sink_ensure_debug_category (void)
Packit 0652a1
{
Packit 0652a1
  static gsize cat_gonce = 0;
Packit 0652a1
Packit 0652a1
  if (g_once_init_enter (&cat_gonce)) {
Packit 0652a1
    GstDebugCategory *cat = NULL;
Packit 0652a1
Packit 0652a1
    GST_DEBUG_CATEGORY_INIT (cat, "videosink", 0, "GstVideoSink");
Packit 0652a1
Packit 0652a1
    g_once_init_leave (&cat_gonce, (gsize) cat);
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  return (GstDebugCategory *) cat_gonce;
Packit 0652a1
}
Packit 0652a1
#endif /* GST_DISABLE_GST_DEBUG */
Packit 0652a1
Packit 0652a1
static GstBaseSinkClass *parent_class = NULL;
Packit 0652a1
Packit 0652a1
static void gst_video_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec);
Packit 0652a1
static void gst_video_sink_get_property (GObject * object, guint prop_id,
Packit 0652a1
    GValue * value, GParamSpec * pspec);
Packit 0652a1
Packit 0652a1
static GstFlowReturn gst_video_sink_show_preroll_frame (GstBaseSink * bsink,
Packit 0652a1
    GstBuffer * buf);
Packit 0652a1
static GstFlowReturn gst_video_sink_show_frame (GstBaseSink * bsink,
Packit 0652a1
    GstBuffer * buf);
Packit 0652a1
Packit 0652a1
/**
Packit 0652a1
 * gst_video_sink_center_rect:
Packit 0652a1
 * @src: the #GstVideoRectangle describing the source area
Packit 0652a1
 * @dst: the #GstVideoRectangle describing the destination area
Packit 0652a1
 * @result: a pointer to a #GstVideoRectangle which will receive the result area
Packit 0652a1
 * @scaling: a #gboolean indicating if scaling should be applied or not
Packit 0652a1
 *
Packit 0652a1
 * Takes @src rectangle and position it at the center of @dst rectangle with or
Packit 0652a1
 * without @scaling. It handles clipping if the @src rectangle is bigger than
Packit 0652a1
 * the @dst one and @scaling is set to FALSE.
Packit 0652a1
 */
Packit 0652a1
void
Packit 0652a1
gst_video_sink_center_rect (GstVideoRectangle src, GstVideoRectangle dst,
Packit 0652a1
    GstVideoRectangle * result, gboolean scaling)
Packit 0652a1
{
Packit 0652a1
  g_return_if_fail (result != NULL);
Packit 0652a1
Packit 0652a1
  if (!scaling) {
Packit 0652a1
    result->w = MIN (src.w, dst.w);
Packit 0652a1
    result->h = MIN (src.h, dst.h);
Packit 0652a1
    result->x = dst.x + (dst.w - result->w) / 2;
Packit 0652a1
    result->y = dst.y + (dst.h - result->h) / 2;
Packit 0652a1
  } else {
Packit 0652a1
    gdouble src_ratio, dst_ratio;
Packit 0652a1
Packit 0652a1
    src_ratio = (gdouble) src.w / src.h;
Packit 0652a1
    dst_ratio = (gdouble) dst.w / dst.h;
Packit 0652a1
Packit 0652a1
    if (src_ratio > dst_ratio) {
Packit 0652a1
      result->w = dst.w;
Packit 0652a1
      result->h = dst.w / src_ratio;
Packit 0652a1
      result->x = dst.x;
Packit 0652a1
      result->y = dst.y + (dst.h - result->h) / 2;
Packit 0652a1
    } else if (src_ratio < dst_ratio) {
Packit 0652a1
      result->w = dst.h * src_ratio;
Packit 0652a1
      result->h = dst.h;
Packit 0652a1
      result->x = dst.x + (dst.w - result->w) / 2;
Packit 0652a1
      result->y = dst.y;
Packit 0652a1
    } else {
Packit 0652a1
      result->x = dst.x;
Packit 0652a1
      result->y = dst.y;
Packit 0652a1
      result->w = dst.w;
Packit 0652a1
      result->h = dst.h;
Packit 0652a1
    }
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  GST_DEBUG ("source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
Packit 0652a1
      src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
/* Initing stuff */
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_sink_init (GstVideoSink * videosink)
Packit 0652a1
{
Packit 0652a1
  videosink->width = 0;
Packit 0652a1
  videosink->height = 0;
Packit 0652a1
Packit 0652a1
  /* 20ms is more than enough, 80-130ms is noticable */
Packit 0652a1
  gst_base_sink_set_processing_deadline (GST_BASE_SINK (videosink),
Packit 0652a1
      15 * GST_MSECOND);
Packit 0652a1
  gst_base_sink_set_max_lateness (GST_BASE_SINK (videosink), 5 * GST_MSECOND);
Packit 0652a1
  gst_base_sink_set_qos_enabled (GST_BASE_SINK (videosink), TRUE);
Packit 0652a1
Packit 0652a1
  videosink->priv = gst_video_sink_get_instance_private (videosink);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_sink_class_init (GstVideoSinkClass * klass)
Packit 0652a1
{
Packit 0652a1
  GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
Packit 0652a1
  GObjectClass *gobject_class = (GObjectClass *) klass;
Packit 0652a1
Packit 0652a1
  parent_class = g_type_class_peek_parent (klass);
Packit 0652a1
Packit 0652a1
  gobject_class->set_property = gst_video_sink_set_property;
Packit 0652a1
  gobject_class->get_property = gst_video_sink_get_property;
Packit 0652a1
Packit 0652a1
  /**
Packit 0652a1
   * GstVideoSink:show-preroll-frame:
Packit 0652a1
   *
Packit 0652a1
   * Whether to show video frames during preroll. If set to %FALSE, video
Packit 0652a1
   * frames will only be rendered in PLAYING state.
Packit 0652a1
   */
Packit 0652a1
  g_object_class_install_property (gobject_class, PROP_SHOW_PREROLL_FRAME,
Packit 0652a1
      g_param_spec_boolean ("show-preroll-frame", "Show preroll frame",
Packit 0652a1
          "Whether to render video frames during preroll",
Packit 0652a1
          DEFAULT_SHOW_PREROLL_FRAME,
Packit 0652a1
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Packit 0652a1
Packit 0652a1
  basesink_class->render = GST_DEBUG_FUNCPTR (gst_video_sink_show_frame);
Packit 0652a1
  basesink_class->preroll =
Packit 0652a1
      GST_DEBUG_FUNCPTR (gst_video_sink_show_preroll_frame);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstFlowReturn
Packit 0652a1
gst_video_sink_show_preroll_frame (GstBaseSink * bsink, GstBuffer * buf)
Packit 0652a1
{
Packit 0652a1
  GstVideoSinkClass *klass;
Packit 0652a1
  GstVideoSink *vsink;
Packit 0652a1
  gboolean do_show;
Packit 0652a1
Packit 0652a1
  vsink = GST_VIDEO_SINK_CAST (bsink);
Packit 0652a1
  klass = GST_VIDEO_SINK_GET_CLASS (vsink);
Packit 0652a1
Packit 0652a1
  do_show = g_atomic_int_get (&vsink->priv->show_preroll_frame);
Packit 0652a1
Packit 0652a1
  if (G_UNLIKELY (!do_show)) {
Packit 0652a1
    GST_DEBUG_OBJECT (bsink, "not rendering frame with ts=%" GST_TIME_FORMAT
Packit 0652a1
        ", preroll rendering disabled",
Packit 0652a1
        GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  if (klass->show_frame == NULL || !do_show) {
Packit 0652a1
    if (parent_class->preroll != NULL)
Packit 0652a1
      return parent_class->preroll (bsink, buf);
Packit 0652a1
    else
Packit 0652a1
      return GST_FLOW_OK;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
Packit 0652a1
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
Packit 0652a1
Packit 0652a1
  return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstFlowReturn
Packit 0652a1
gst_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
Packit 0652a1
{
Packit 0652a1
  GstVideoSinkClass *klass;
Packit 0652a1
Packit 0652a1
  klass = GST_VIDEO_SINK_GET_CLASS (bsink);
Packit 0652a1
Packit 0652a1
  if (klass->show_frame == NULL) {
Packit 0652a1
    if (parent_class->render != NULL)
Packit 0652a1
      return parent_class->render (bsink, buf);
Packit 0652a1
    else
Packit 0652a1
      return GST_FLOW_OK;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  GST_LOG_OBJECT (bsink, "rendering frame, ts=%" GST_TIME_FORMAT,
Packit 0652a1
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
Packit 0652a1
Packit 0652a1
  return klass->show_frame (GST_VIDEO_SINK_CAST (bsink), buf);
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_sink_set_property (GObject * object, guint prop_id,
Packit 0652a1
    const GValue * value, GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstVideoSink *vsink;
Packit 0652a1
Packit 0652a1
  vsink = GST_VIDEO_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_SHOW_PREROLL_FRAME:
Packit 0652a1
      g_atomic_int_set (&vsink->priv->show_preroll_frame,
Packit 0652a1
          g_value_get_boolean (value));
Packit 0652a1
      break;
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static void
Packit 0652a1
gst_video_sink_get_property (GObject * object, guint prop_id,
Packit 0652a1
    GValue * value, GParamSpec * pspec)
Packit 0652a1
{
Packit 0652a1
  GstVideoSink *vsink;
Packit 0652a1
Packit 0652a1
  vsink = GST_VIDEO_SINK (object);
Packit 0652a1
Packit 0652a1
  switch (prop_id) {
Packit 0652a1
    case PROP_SHOW_PREROLL_FRAME:
Packit 0652a1
      g_value_set_boolean (value,
Packit 0652a1
          g_atomic_int_get (&vsink->priv->show_preroll_frame));
Packit 0652a1
      break;
Packit 0652a1
    default:
Packit 0652a1
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
}