Blame gst/gststreamcollection.c

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