All pastes #15318 Raw Edit

Stuff

public text v1 · immutable
#15318 ·published 2005-06-21 14:42 UTC
rendered paste body
/* GStreamer
 * Copyright (C) <2005> Philippe Khalaf <burger@speedy.org> 
 *
 * 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.
 */

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

#include <gst/gst.h>

#include "gstrtpbin.h"

static GstElementDetails gst_rtp_bin_details = {
  "RTP Bin",
  "Generic/Bin/RTP",
  "Encapsulates RTP session management and sending/receiving",
  "Philippe Khalaf <burger@speedy.org>"
};

/* generic templates */
static GstStaticPadTemplate rtp_bin_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstStaticPadTemplate rtp_bin_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_SOMETIMES,
    GST_STATIC_CAPS_ANY);

GST_DEBUG_CATEGORY_STATIC (gst_rtp_bin_debug);
#define GST_CAT_DEFAULT gst_rtp_bin_debug

/* props */
enum
{
  ARG_0,
  ARG_DESTADDRS,
  ARG_LOCALPORT,
};

/* signals */
enum
{
  LAST_SIGNAL
};

static void gst_rtp_bin_class_init (GstRTPBinClass * klass);
static void gst_rtp_bin_init (GstRTPBin * rtp_bin);
static void gst_rtp_bin_dispose (GObject * object);

static GstElementStateReturn
gst_rtp_bin_get_state (GstElement * element, GstElementState * state,
    GstElementState * pending, GTimeVal * timeout);

static void gst_rtp_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * spec);
static void gst_rtp_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * spec);

static GstElementClass *parent_class = NULL;

GType
gst_rtp_bin_get_type (void)
{
  static GType gst_rtp_bin_type = 0;

  if (!gst_rtp_bin_type) {
    static const GTypeInfo gst_rtp_bin_info = {
      sizeof (GstRTPBinClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_rtp_bin_class_init,
      NULL,
      NULL,
      sizeof (GstRTPBin),
      0,
      (GInstanceInitFunc) gst_rtp_bin_init,
      NULL
    };

    gst_rtp_bin_type =
        g_type_register_static (GST_TYPE_BIN, "GstRTPBin",
                &gst_rtp_bin_info, 0);
  }

  return gst_rtp_bin_type;
}

static void
gst_rtp_bin_class_init (GstRTPBinClass * klass)
{
  GObjectClass *gobject_klass;
  GstElementClass *gstelement_klass;
  GstBinClass *gstbin_klass;

  gobject_klass = (GObjectClass *) klass;
  gstelement_klass = (GstElementClass *) klass;
  gstbin_klass = (GstBinClass *) klass;

  parent_class = g_type_class_ref (gst_rtp_bin_get_type ());

  gobject_klass->set_property = gst_rtp_bin_set_property;
  gobject_klass->get_property = gst_rtp_bin_get_property;

  gobject_klass->dispose = GST_DEBUG_FUNCPTR (gst_rtp_bin_dispose);

  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LOCALPORT,
          g_param_spec_uint ("localport", "Local Port", 
              "An even upd port for the rtp socket, rtcp is bound to +1",
              0, G_MAXUINT16, 0, G_PARAM_READWRITE));

  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESTADDRS,
          g_param_spec_string ("destaddrs", "Destination Address:Port;Address:Port...",
              "Destination addresses and ports",
              "", G_PARAM_READWRITE));

  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&rtp_bin_sink_template));
  gst_element_class_add_pad_template (gstelement_klass,
      gst_static_pad_template_get (&rtp_bin_src_template));

  gst_element_class_set_details (gstelement_klass, &gst_rtp_bin_details);

  // ok lets overload our get_state function now
  gstelement_klass->get_state = gst_rtp_bin_get_state;
 
  GST_DEBUG_CATEGORY_INIT (gst_rtp_bin_debug, "rtpbin", 0, "RTP Bin");
}

static void
gst_rtp_bin_init (GstRTPBin * rtp_bin)
{
    // Create all our elements
    rtp_bin->rtpsession = gst_element_factory_make ("rtpsession", "rtpsession");

    rtp_bin->rtpsink = gst_element_factory_make ("udpdynsink", "rtpsinkelement");

    rtp_bin->rtcpsink =  gst_element_factory_make ("udpdynsink", "rtcpsinkelement");

    rtp_bin->rtpsrc = gst_element_factory_make ("udpsrc", "rtpsrcelement");

    rtp_bin->rtcpsrc = gst_element_factory_make ("udpsrc", "rtcpsrcelement");

    // Add our elements
    gst_bin_add_many (GST_BIN (rtp_bin), rtp_bin->rtpsession, rtp_bin->rtpsink, 
            rtp_bin->rtcpsink, rtp_bin->rtpsrc, rtp_bin->rtcpsrc, NULL);

    // and link them
    gst_element_link_pads (rtp_bin->rtpsession, "rtpsrc", rtp_bin->rtpsink, "sink");
    gst_element_link_pads (rtp_bin->rtpsession, "rtcpsrc", rtp_bin->rtcpsink, "sink");
    gst_element_link_pads (rtp_bin->rtpsrc, "src", rtp_bin->rtpsession, "rtpsink");
    gst_element_link_pads (rtp_bin->rtcpsrc, "src", rtp_bin->rtpsession, "rtcpsink");

    // let's link our ghost pads
    gst_element_add_pad (GST_ELEMENT (rtp_bin),
            gst_ghost_pad_new("sink",
                gst_element_get_pad (rtp_bin->rtpsession, "datasink")));
    gst_element_add_pad (GST_ELEMENT (rtp_bin),
            gst_ghost_pad_new("src",
                gst_element_get_pad (rtp_bin->rtpsession, "datasrc")));
}

static void
gst_rtp_bin_dispose (GObject * object)
{
  GstRTPBin *rtp_bin;
  GList *dyns;

  rtp_bin = GST_RTP_BIN (object);

  gst_bin_remove_many (GST_BIN (rtp_bin), rtp_bin->rtpsession, rtp_bin->rtpsink, 
            rtp_bin->rtcpsink, rtp_bin->rtpsrc, rtp_bin->rtcpsrc, NULL);

  g_free(rtp_bin->destaddrs);

  if (G_OBJECT_CLASS (parent_class)->dispose) {
    G_OBJECT_CLASS (parent_class)->dispose (object);
  }
}

static GstElementStateReturn
gst_rtp_bin_get_state (GstElement * element, GstElementState * state,
    GstElementState * pending, GTimeVal * timeout)
{
    GST_DEBUG("Going into get_state");
  GstBin *bin = GST_BIN (element);
  GstElementStateReturn ret;
  GList *children;
  guint32 children_cookie;

  /* we cannot take the state lock yet as we might block when querying
   * the children, holding the lock too long for no reason. */

  /* next we poll all children for their state to see if one of them
   * is still busy with its state change. */
  GST_LOCK (bin);
restart:
  GST_DEBUG("restarting");
  ret = GST_STATE_SUCCESS;
  children = bin->children;
  children_cookie = bin->children_cookie;
  while (children) {
    GstElement *child = GST_ELEMENT_CAST (children->data);

    gst_object_ref (GST_OBJECT_CAST (child));
    GST_UNLOCK (bin);

    /* ret is ASYNC if some child is still performing the state change */
    ret = gst_element_get_state (child, NULL, NULL, timeout);

    gst_object_unref (GST_OBJECT_CAST (child));

    /* now grab the lock to iterate to the next child */
    GST_LOCK (bin);
    if (G_UNLIKELY (children_cookie != bin->children_cookie))
      /* child added/removed during state change, restart */
      goto restart;

    if (ret != GST_STATE_SUCCESS) {
      /* some child is still busy or in error, we can report that
       * right away. */
      break;
    }

    children = g_list_next (children);
  }
  GST_UNLOCK (bin);

  /* now we can take the state lock */
  GST_STATE_LOCK (bin);
  switch (ret) {
    case GST_STATE_SUCCESS:
      /* we can commit the state */
      gst_element_commit_state (element);
      break;
    case GST_STATE_FAILURE:
      /* some element failed, abort the state change */
      gst_element_abort_state (element);
      break;
    default:
      /* other cases are just passed along */
      break;
  }

  /* and report the state if needed */
  if (state)
    *state = GST_STATE (element);
  if (pending)
    *pending = GST_STATE_PENDING (element);

  GST_STATE_UNLOCK (bin);

  return ret;
}

static void
gst_rtp_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstRTPBin *rtp_bin;

  g_return_if_fail (GST_IS_RTP_BIN (object));

  rtp_bin = GST_RTP_BIN (object);

  switch (prop_id) {
      case ARG_LOCALPORT:
          rtp_bin->localport = g_value_get_uint (value);
          g_object_set (G_OBJECT(rtp_bin->rtpsession), "localportbase", rtp_bin->localport, NULL);
          /* temp */
          g_object_set (G_OBJECT(rtp_bin->rtpsession), "defaultpt", 5, NULL);
          g_object_set (G_OBJECT(rtp_bin->rtpsession), "defaulttsinc", 10, NULL);
          g_object_set (G_OBJECT(rtp_bin->rtpsession), "defaultmark", 0, NULL);
          /*end temp*/
          g_object_set (G_OBJECT(rtp_bin->rtpsrc), "port", rtp_bin->localport, NULL);
          g_object_set (G_OBJECT(rtp_bin->rtpsrc), "port", rtp_bin->localport+1, NULL);
          GST_DEBUG("setting port -> %d", rtp_bin->localport);
          break;
      case ARG_DESTADDRS:
          g_free(rtp_bin->destaddrs);
          rtp_bin->destaddrs = g_value_dup_string(value);
          g_object_set (G_OBJECT(rtp_bin->rtpsession), "destaddrs", rtp_bin->destaddrs, NULL);
          break;
      default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
          break;
  }
}

static void
gst_rtp_bin_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstRTPBin *rtp_bin;

  g_return_if_fail (GST_IS_RTP_BIN (object));

  rtp_bin = GST_RTP_BIN (object);

  switch (prop_id) {
      case ARG_LOCALPORT:
          g_value_set_uint (value, rtp_bin->localport);
          break;
      case ARG_DESTADDRS:
          g_value_set_string(value, rtp_bin->destaddrs);
          break;
      default:
          G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
          break;
  }
}