Blame gst/gststreamcollection.c

Packit Service 963350
/* GStreamer
Packit Service 963350
 *
Packit Service 963350
 * Copyright (C) 2015 Centricular Ltd
Packit Service 963350
 *  @author: Edward Hervey <edward@centricular.com>
Packit Service 963350
 *  @author: Jan Schmidt <jan@centricular.com>
Packit Service 963350
 *
Packit Service 963350
 * gststreams.c: GstStreamCollection object and methods
Packit Service 963350
 *
Packit Service 963350
 * This library is free software; you can redistribute it and/or
Packit Service 963350
 * modify it under the terms of the GNU Library General Public
Packit Service 963350
 * License as published by the Free Software Foundation; either
Packit Service 963350
 * version 2 of the License, or (at your option) any later version.
Packit Service 963350
 *
Packit Service 963350
 * This library is distributed in the hope that it will be useful,
Packit Service 963350
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 963350
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 963350
 * Library General Public License for more details.
Packit Service 963350
 *
Packit Service 963350
 * You should have received a copy of the GNU Library General Public
Packit Service 963350
 * License along with this library; if not, write to the
Packit Service 963350
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit Service 963350
 * Boston, MA 02110-1301, USA.
Packit Service 963350
 *
Packit Service 963350
 * MT safe.
Packit Service 963350
 */
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * SECTION:gststreamcollection
Packit Service 963350
 * @title: GstStreamCollection
Packit Service 963350
 * @short_description: Base class for collection of streams
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
Packit Service 963350
#include "gst_private.h"
Packit Service 963350
Packit Service 963350
#include "gstenumtypes.h"
Packit Service 963350
#include "gstevent.h"
Packit Service 963350
#include "gststreamcollection.h"
Packit Service 963350
Packit Service 963350
GST_DEBUG_CATEGORY_STATIC (stream_collection_debug);
Packit Service 963350
#define GST_CAT_DEFAULT stream_collection_debug
Packit Service 963350
Packit Service 963350
#define GST_STREAM_COLLECTION_GET_PRIVATE(obj)  \
Packit Service 963350
   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_STREAM_COLLECTION, GstStreamCollectionPrivate))
Packit Service 963350
Packit Service 963350
struct _GstStreamCollectionPrivate
Packit Service 963350
{
Packit Service 963350
  /* Maybe switch this to a GArray if performance is
Packit Service 963350
   * ever an issue? */
Packit Service 963350
  GQueue *streams;
Packit Service 963350
};
Packit Service 963350
Packit Service 963350
/* stream signals and properties */
Packit Service 963350
enum
Packit Service 963350
{
Packit Service 963350
  SIG_STREAM_NOTIFY,
Packit Service 963350
  LAST_SIGNAL
Packit Service 963350
};
Packit Service 963350
Packit Service 963350
enum
Packit Service 963350
{
Packit Service 963350
  PROP_0,
Packit Service 963350
  PROP_UPSTREAM_ID,
Packit Service 963350
  PROP_LAST
Packit Service 963350
};
Packit Service 963350
Packit Service 963350
static guint gst_stream_collection_signals[LAST_SIGNAL] = { 0 };
Packit Service 963350
Packit Service 963350
static void gst_stream_collection_dispose (GObject * object);
Packit Service 963350
static void gst_stream_collection_finalize (GObject * object);
Packit Service 963350
Packit Service 963350
static void gst_stream_collection_set_property (GObject * object, guint prop_id,
Packit Service 963350
    const GValue * value, GParamSpec * pspec);
Packit Service 963350
static void gst_stream_collection_get_property (GObject * object, guint prop_id,
Packit Service 963350
    GValue * value, GParamSpec * pspec);
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
Packit Service 963350
    GstStreamCollection * collection);
Packit Service 963350
Packit Service 963350
#define _do_init				\
Packit Service 963350
{ \
Packit Service 963350
  GST_DEBUG_CATEGORY_INIT (stream_collection_debug, "streamcollection", GST_DEBUG_BOLD, \
Packit Service 963350
      "debugging info for the stream collection objects"); \
Packit Service 963350
  \
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
#define gst_stream_collection_parent_class parent_class
Packit Service 963350
G_DEFINE_TYPE_WITH_CODE (GstStreamCollection, gst_stream_collection,
Packit Service 963350
    GST_TYPE_OBJECT, _do_init);
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_class_init (GstStreamCollectionClass * klass)
Packit Service 963350
{
Packit Service 963350
  GObjectClass *gobject_class;
Packit Service 963350
Packit Service 963350
  gobject_class = (GObjectClass *) klass;
Packit Service 963350
Packit Service 963350
  g_type_class_add_private (klass, sizeof (GstStreamCollectionPrivate));
Packit Service 963350
Packit Service 963350
  gobject_class->set_property = gst_stream_collection_set_property;
Packit Service 963350
  gobject_class->get_property = gst_stream_collection_get_property;
Packit Service 963350
Packit Service 963350
  /**
Packit Service 963350
   * GstStream:upstream-id:
Packit Service 963350
   *
Packit Service 963350
   * stream-id
Packit Service 963350
   */
Packit Service 963350
  g_object_class_install_property (gobject_class, PROP_UPSTREAM_ID,
Packit Service 963350
      g_param_spec_string ("upstream-id", "Upstream ID",
Packit Service 963350
          "The stream ID of the parent stream",
Packit Service 963350
          NULL,
Packit Service 963350
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Packit Service 963350
Packit Service 963350
  /**
Packit Service 963350
   * GstStream::stream-notify:
Packit Service 963350
   * @collection: a #GstStreamCollection
Packit Service 963350
   * @prop_stream: the #GstStream that originated the signal
Packit Service 963350
   * @prop: the property that changed
Packit Service 963350
   *
Packit Service 963350
   * The stream notify signal is used to be notified of property changes to
Packit Service 963350
   * streams within the collection.
Packit Service 963350
   */
Packit Service 963350
  gst_stream_collection_signals[SIG_STREAM_NOTIFY] =
Packit Service 963350
      g_signal_new ("stream-notify", G_TYPE_FROM_CLASS (klass),
Packit Service 963350
      G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
Packit Service 963350
      G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstStreamCollectionClass,
Packit Service 963350
          stream_notify), NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
Packit Service 963350
      2, GST_TYPE_STREAM, G_TYPE_PARAM);
Packit Service 963350
Packit Service 963350
  gobject_class->dispose = gst_stream_collection_dispose;
Packit Service 963350
  gobject_class->finalize = gst_stream_collection_finalize;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_init (GstStreamCollection * collection)
Packit Service 963350
{
Packit Service 963350
  collection->priv = GST_STREAM_COLLECTION_GET_PRIVATE (collection);
Packit Service 963350
  collection->priv->streams = g_queue_new ();
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
release_gst_stream (GstStream * stream, GstStreamCollection * collection)
Packit Service 963350
{
Packit Service 963350
  g_signal_handlers_disconnect_by_func (stream,
Packit Service 963350
      proxy_stream_notify_cb, collection);
Packit Service 963350
  gst_object_unref (stream);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_dispose (GObject * object)
Packit Service 963350
{
Packit Service 963350
  GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
Packit Service 963350
Packit Service 963350
  if (collection->upstream_id) {
Packit Service 963350
    g_free (collection->upstream_id);
Packit Service 963350
    collection->upstream_id = NULL;
Packit Service 963350
  }
Packit Service 963350
Packit Service 963350
  if (collection->priv->streams) {
Packit Service 963350
    g_queue_foreach (collection->priv->streams,
Packit Service 963350
        (GFunc) release_gst_stream, collection);
Packit Service 963350
    g_queue_clear (collection->priv->streams);
Packit Service 963350
  }
Packit Service 963350
Packit Service 963350
  G_OBJECT_CLASS (parent_class)->dispose (object);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_finalize (GObject * object)
Packit Service 963350
{
Packit Service 963350
  GstStreamCollection *collection = GST_STREAM_COLLECTION_CAST (object);
Packit Service 963350
Packit Service 963350
  if (collection->priv->streams)
Packit Service 963350
    g_queue_free (collection->priv->streams);
Packit Service 963350
Packit Service 963350
  G_OBJECT_CLASS (parent_class)->finalize (object);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_stream_collection_new:
Packit Service 963350
 * @upstream_id: (allow-none): The stream id of the parent stream
Packit Service 963350
 *
Packit Service 963350
 * Create a new #GstStreamCollection.
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer full): The new #GstStreamCollection.
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
GstStreamCollection *
Packit Service 963350
gst_stream_collection_new (const gchar * upstream_id)
Packit Service 963350
{
Packit Service 963350
  GstStreamCollection *collection;
Packit Service 963350
Packit Service 963350
  collection =
Packit Service 963350
      g_object_new (GST_TYPE_STREAM_COLLECTION, "upstream-id", upstream_id,
Packit Service 963350
      NULL);
Packit Service 963350
Packit Service 963350
  /* Clear floating flag */
Packit Service 963350
  g_object_ref_sink (collection);
Packit Service 963350
Packit Service 963350
  return collection;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_set_upstream_id (GstStreamCollection * collection,
Packit Service 963350
    const gchar * upstream_id)
Packit Service 963350
{
Packit Service 963350
  g_return_if_fail (collection->upstream_id == NULL);
Packit Service 963350
Packit Service 963350
  /* Upstream ID should only be set once on construction, but let's
Packit Service 963350
   * not leak in case someone does something silly */
Packit Service 963350
  if (collection->upstream_id)
Packit Service 963350
    g_free (collection->upstream_id);
Packit Service 963350
Packit Service 963350
  if (upstream_id)
Packit Service 963350
    collection->upstream_id = g_strdup (upstream_id);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_stream_collection_get_upstream_id:
Packit Service 963350
 * @collection: a #GstStreamCollection
Packit Service 963350
 *
Packit Service 963350
 * Returns the upstream id of the @collection.
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer none): The upstream id
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
const gchar *
Packit Service 963350
gst_stream_collection_get_upstream_id (GstStreamCollection * collection)
Packit Service 963350
{
Packit Service 963350
  const gchar *res;
Packit Service 963350
Packit Service 963350
  res = collection->upstream_id;
Packit Service 963350
Packit Service 963350
  return res;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_set_property (GObject * object, guint prop_id,
Packit Service 963350
    const GValue * value, GParamSpec * pspec)
Packit Service 963350
{
Packit Service 963350
  GstStreamCollection *collection;
Packit Service 963350
Packit Service 963350
  collection = GST_STREAM_COLLECTION_CAST (object);
Packit Service 963350
Packit Service 963350
  switch (prop_id) {
Packit Service 963350
    case PROP_UPSTREAM_ID:
Packit Service 963350
      gst_stream_collection_set_upstream_id (collection,
Packit Service 963350
          g_value_get_string (value));
Packit Service 963350
      break;
Packit Service 963350
    default:
Packit Service 963350
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit Service 963350
      break;
Packit Service 963350
  }
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
gst_stream_collection_get_property (GObject * object, guint prop_id,
Packit Service 963350
    GValue * value, GParamSpec * pspec)
Packit Service 963350
{
Packit Service 963350
  GstStreamCollection *collection;
Packit Service 963350
Packit Service 963350
  collection = GST_STREAM_COLLECTION_CAST (object);
Packit Service 963350
Packit Service 963350
  switch (prop_id) {
Packit Service 963350
    case PROP_UPSTREAM_ID:
Packit Service 963350
      g_value_set_string (value,
Packit Service 963350
          gst_stream_collection_get_upstream_id (collection));
Packit Service 963350
      break;
Packit Service 963350
    default:
Packit Service 963350
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit Service 963350
      break;
Packit Service 963350
  }
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
static void
Packit Service 963350
proxy_stream_notify_cb (GstStream * stream, GParamSpec * pspec,
Packit Service 963350
    GstStreamCollection * collection)
Packit Service 963350
{
Packit Service 963350
  GST_DEBUG_OBJECT (collection, "Stream %" GST_PTR_FORMAT " updated %s",
Packit Service 963350
      stream, pspec->name);
Packit Service 963350
  g_signal_emit (collection, gst_stream_collection_signals[SIG_STREAM_NOTIFY],
Packit Service 963350
      g_quark_from_string (pspec->name), stream, pspec);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_stream_collection_add_stream:
Packit Service 963350
 * @collection: a #GstStreamCollection
Packit Service 963350
 * @stream: (transfer full): the #GstStream to add
Packit Service 963350
 *
Packit Service 963350
 * Add the given @stream to the @collection.
Packit Service 963350
 *
Packit Service 963350
 * Returns: %TRUE if the @stream was properly added, else %FALSE
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
gboolean
Packit Service 963350
gst_stream_collection_add_stream (GstStreamCollection * collection,
Packit Service 963350
    GstStream * stream)
Packit Service 963350
{
Packit Service 963350
  g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), FALSE);
Packit Service 963350
  g_return_val_if_fail (GST_IS_STREAM (stream), FALSE);
Packit Service 963350
  g_return_val_if_fail (collection->priv->streams, FALSE);
Packit Service 963350
Packit Service 963350
  GST_DEBUG_OBJECT (collection, "Adding stream %" GST_PTR_FORMAT, stream);
Packit Service 963350
Packit Service 963350
  g_queue_push_tail (collection->priv->streams, stream);
Packit Service 963350
  g_signal_connect (stream, "notify", (GCallback) proxy_stream_notify_cb,
Packit Service 963350
      collection);
Packit Service 963350
Packit Service 963350
  return TRUE;
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_stream_collection_get_size:
Packit Service 963350
 * @collection: a #GstStreamCollection
Packit Service 963350
 *
Packit Service 963350
 * Get the number of streams this collection contains
Packit Service 963350
 *
Packit Service 963350
 * Returns: The number of streams that @collection contains
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
guint
Packit Service 963350
gst_stream_collection_get_size (GstStreamCollection * collection)
Packit Service 963350
{
Packit Service 963350
  g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), 0);
Packit Service 963350
  g_return_val_if_fail (collection->priv->streams, 0);
Packit Service 963350
Packit Service 963350
  return g_queue_get_length (collection->priv->streams);
Packit Service 963350
}
Packit Service 963350
Packit Service 963350
/**
Packit Service 963350
 * gst_stream_collection_get_stream:
Packit Service 963350
 * @collection: a #GstStreamCollection
Packit Service 963350
 * @index: Index of the stream to retrieve
Packit Service 963350
 *
Packit Service 963350
 * Retrieve the #GstStream with index @index from the collection.
Packit Service 963350
 *
Packit Service 963350
 * The caller should not modify the returned #GstStream
Packit Service 963350
 *
Packit Service 963350
 * Returns: (transfer none) (nullable): A #GstStream
Packit Service 963350
 *
Packit Service 963350
 * Since: 1.10
Packit Service 963350
 */
Packit Service 963350
GstStream *
Packit Service 963350
gst_stream_collection_get_stream (GstStreamCollection * collection, guint index)
Packit Service 963350
{
Packit Service 963350
  g_return_val_if_fail (GST_IS_STREAM_COLLECTION (collection), NULL);
Packit Service 963350
  g_return_val_if_fail (collection->priv->streams, NULL);
Packit Service 963350
Packit Service 963350
  return g_queue_peek_nth (collection->priv->streams, index);
Packit Service 963350
}