gstrtpgstdepay.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* GStreamer
 * Copyright (C) <2010> Wim Taymans <wim.taymans@gmail.com>
 *
 * 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/rtp/gstrtpbuffer.h>

#include <string.h>
#include "gstrtpgstdepay.h"

GST_DEBUG_CATEGORY_STATIC (rtpgstdepay_debug);
#define GST_CAT_DEFAULT (rtpgstdepay_debug)

static GstStaticPadTemplate gst_rtp_gst_depay_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

static GstStaticPadTemplate gst_rtp_gst_depay_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"application\", "
        "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
        "clock-rate = (int) 90000, " "encoding-name = (string) \"X-GST\"")
    );

48 49
#define gst_rtp_gst_depay_parent_class parent_class
G_DEFINE_TYPE (GstRtpGSTDepay, gst_rtp_gst_depay, GST_TYPE_BASE_RTP_DEPAYLOAD);
50 51 52

static void gst_rtp_gst_depay_finalize (GObject * object);

Wim Taymans's avatar
Wim Taymans committed
53 54 55 56
static GstStateChangeReturn gst_rtp_gst_depay_change_state (GstElement *
    element, GstStateChange transition);

static void gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay);
57 58 59 60 61 62 63 64 65
static gboolean gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload,
    GstCaps * caps);
static GstBuffer *gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload,
    GstBuffer * buf);

static void
gst_rtp_gst_depay_class_init (GstRtpGSTDepayClass * klass)
{
  GObjectClass *gobject_class;
Wim Taymans's avatar
Wim Taymans committed
66
  GstElementClass *gstelement_class;
67 68
  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;

69 70 71
  GST_DEBUG_CATEGORY_INIT (rtpgstdepay_debug, "rtpgstdepay", 0,
      "Gstreamer RTP Depayloader");

72
  gobject_class = (GObjectClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
73
  gstelement_class = (GstElementClass *) klass;
74 75 76 77
  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;

  gobject_class->finalize = gst_rtp_gst_depay_finalize;

Wim Taymans's avatar
Wim Taymans committed
78 79
  gstelement_class->change_state = gst_rtp_gst_depay_change_state;

80 81 82 83 84 85 86 87 88 89
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_rtp_gst_depay_src_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_rtp_gst_depay_sink_template));

  gst_element_class_set_details_simple (gstelement_class,
      "GStreamer depayloader", "Codec/Depayloader/Network",
      "Extracts GStreamer buffers from RTP packets",
      "Wim Taymans <wim.taymans@gmail.com>");

90 91 92 93 94
  gstbasertpdepayload_class->set_caps = gst_rtp_gst_depay_setcaps;
  gstbasertpdepayload_class->process = gst_rtp_gst_depay_process;
}

static void
95
gst_rtp_gst_depay_init (GstRtpGSTDepay * rtpgstdepay)
96 97 98 99 100 101 102 103 104 105 106
{
  rtpgstdepay->adapter = gst_adapter_new ();
}

static void
gst_rtp_gst_depay_finalize (GObject * object)
{
  GstRtpGSTDepay *rtpgstdepay;

  rtpgstdepay = GST_RTP_GST_DEPAY (object);

Wim Taymans's avatar
Wim Taymans committed
107
  gst_rtp_gst_depay_reset (rtpgstdepay);
108 109 110 111 112
  g_object_unref (rtpgstdepay->adapter);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

Wim Taymans's avatar
Wim Taymans committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void
store_cache (GstRtpGSTDepay * rtpgstdepay, guint CV, GstCaps * caps)
{
  if (rtpgstdepay->CV_cache[CV])
    gst_caps_unref (rtpgstdepay->CV_cache[CV]);
  rtpgstdepay->CV_cache[CV] = caps;
}

static void
gst_rtp_gst_depay_reset (GstRtpGSTDepay * rtpgstdepay)
{
  guint i;

  gst_adapter_clear (rtpgstdepay->adapter);
  rtpgstdepay->current_CV = 0;
  for (i = 0; i < 8; i++)
    store_cache (rtpgstdepay, i, NULL);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static gboolean
gst_rtp_gst_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
{
  GstRtpGSTDepay *rtpgstdepay;
  GstStructure *structure;
  GstCaps *outcaps;
  gint clock_rate;
  gboolean res;
  const gchar *capsenc;
  gchar *capsstr;

  rtpgstdepay = GST_RTP_GST_DEPAY (depayload);

  structure = gst_caps_get_structure (caps, 0);

  if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
    clock_rate = 90000;
  depayload->clock_rate = clock_rate;

  capsenc = gst_structure_get_string (structure, "caps");
  if (capsenc) {
    gsize out_len;

    capsstr = (gchar *) g_base64_decode (capsenc, &out_len);
    outcaps = gst_caps_from_string (capsstr);
    g_free (capsstr);

    /* we have the SDP caps as output caps */
    rtpgstdepay->current_CV = 0;
Wim Taymans's avatar
Wim Taymans committed
161 162
    gst_caps_ref (outcaps);
    store_cache (rtpgstdepay, 0, outcaps);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  } else {
    outcaps = gst_caps_new_any ();
  }
  res = gst_pad_set_caps (depayload->srcpad, outcaps);
  gst_caps_unref (outcaps);

  return res;
}

static GstBuffer *
gst_rtp_gst_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
{
  GstRtpGSTDepay *rtpgstdepay;
  GstBuffer *subbuf, *outbuf = NULL;
  gint payload_len;
  guint8 *payload;
  guint CV;
Wim Taymans's avatar
Wim Taymans committed
180
  GstRTPBuffer rtp = { NULL };
181 182 183

  rtpgstdepay = GST_RTP_GST_DEPAY (depayload);

Wim Taymans's avatar
Wim Taymans committed
184 185 186
  gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);

  payload_len = gst_rtp_buffer_get_payload_len (&rtp);
187 188 189 190 191 192 193 194 195

  if (payload_len <= 8)
    goto empty_packet;

  if (GST_BUFFER_IS_DISCONT (buf)) {
    GST_WARNING_OBJECT (rtpgstdepay, "DISCONT, clear adapter");
    gst_adapter_clear (rtpgstdepay->adapter);
  }

Wim Taymans's avatar
Wim Taymans committed
196
  payload = gst_rtp_buffer_get_payload (&rtp);
197 198 199 200 201 202 203 204 205 206 207

  /* strip off header
   *
   *  0                   1                   2                   3
   *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   * |C| CV  |D|X|Y|Z|                  MBZ                          |
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   * |                          Frag_offset                          |
   * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   */
208 209 210
  /* frag_offset =
   *   (payload[4] << 24) | (payload[5] << 16) | (payload[6] << 8) | payload[7];
   */
211 212

  /* subbuffer skipping the 8 header bytes */
Wim Taymans's avatar
Wim Taymans committed
213
  subbuf = gst_rtp_buffer_get_payload_subbuffer (&rtp, 8, -1);
214 215
  gst_adapter_push (rtpgstdepay->adapter, subbuf);

Wim Taymans's avatar
Wim Taymans committed
216
  if (gst_rtp_buffer_get_marker (&rtp)) {
217 218 219 220 221 222 223 224 225 226
    guint avail;
    GstCaps *outcaps;

    /* take the buffer */
    avail = gst_adapter_available (rtpgstdepay->adapter);
    outbuf = gst_adapter_take_buffer (rtpgstdepay->adapter, avail);

    CV = (payload[0] >> 4) & 0x7;

    if (payload[0] & 0x80) {
Wim Taymans's avatar
Wim Taymans committed
227 228
      guint b, csize, left, offset;
      gsize size;
229 230 231 232
      guint8 *data;
      GstBuffer *subbuf;

      /* C bit, we have inline caps */
Wim Taymans's avatar
Wim Taymans committed
233
      data = gst_buffer_map (outbuf, &size, NULL, GST_MAP_READ);
234 235 236

      /* start reading the length, we need this to skip to the data later */
      csize = offset = 0;
Wim Taymans's avatar
Wim Taymans committed
237
      left = size;
238
      do {
Wim Taymans's avatar
Wim Taymans committed
239 240
        if (offset >= left) {
          gst_buffer_unmap (outbuf, data, size);
241
          goto too_small;
Wim Taymans's avatar
Wim Taymans committed
242
        }
243 244 245 246
        b = data[offset++];
        csize = (csize << 7) | (b & 0x7f);
      } while (b & 0x80);

Wim Taymans's avatar
Wim Taymans committed
247 248
      if (left < csize) {
        gst_buffer_unmap (outbuf, data, size);
249
        goto too_small;
Wim Taymans's avatar
Wim Taymans committed
250
      }
251 252 253

      /* parse and store in cache */
      outcaps = gst_caps_from_string ((gchar *) & data[offset]);
Wim Taymans's avatar
Wim Taymans committed
254
      store_cache (rtpgstdepay, CV, outcaps);
255 256 257

      /* skip caps */
      offset += csize;
Wim Taymans's avatar
Wim Taymans committed
258
      left -= csize;
259 260 261 262 263 264

      GST_DEBUG_OBJECT (rtpgstdepay,
          "inline caps %u, length %u, %" GST_PTR_FORMAT, CV, csize, outcaps);

      /* create real data buffer when needed */
      if (size)
Wim Taymans's avatar
Wim Taymans committed
265 266
        subbuf =
            gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, offset, left);
267 268 269
      else
        subbuf = NULL;

Wim Taymans's avatar
Wim Taymans committed
270
      gst_buffer_unmap (outbuf, data, size);
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
      gst_buffer_unref (outbuf);
      outbuf = subbuf;
    }

    /* see what caps we need */
    if (CV != rtpgstdepay->current_CV) {
      /* we need to switch caps, check if we have the caps */
      if ((outcaps = rtpgstdepay->CV_cache[CV]) == NULL)
        goto missing_caps;

      GST_DEBUG_OBJECT (rtpgstdepay,
          "need caps switch from %u to %u, %" GST_PTR_FORMAT,
          rtpgstdepay->current_CV, CV, outcaps);

      /* and set caps */
      if (gst_pad_set_caps (depayload->srcpad, outcaps))
        rtpgstdepay->current_CV = CV;
    }

    if (outbuf) {
      if (payload[0] & 0x8)
        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT);
      if (payload[0] & 0x4)
        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA1);
      if (payload[0] & 0x2)
        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA2);
      if (payload[0] & 0x1)
        GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MEDIA3);
    }
  }
  return outbuf;

  /* ERRORS */
empty_packet:
  {
    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
        ("Empty Payload."), (NULL));
Wim Taymans's avatar
Wim Taymans committed
308
    gst_rtp_buffer_unmap (&rtp);
309 310 311 312 313 314 315 316
    return NULL;
  }
too_small:
  {
    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
        ("Buffer too small."), (NULL));
    if (outbuf)
      gst_buffer_unref (outbuf);
Wim Taymans's avatar
Wim Taymans committed
317
    gst_rtp_buffer_unmap (&rtp);
318 319 320 321 322 323 324 325
    return NULL;
  }
missing_caps:
  {
    GST_ELEMENT_WARNING (rtpgstdepay, STREAM, DECODE,
        ("Missing caps %u.", CV), (NULL));
    if (outbuf)
      gst_buffer_unref (outbuf);
Wim Taymans's avatar
Wim Taymans committed
326
    gst_rtp_buffer_unmap (&rtp);
327 328 329 330
    return NULL;
  }
}

Wim Taymans's avatar
Wim Taymans committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
static GstStateChangeReturn
gst_rtp_gst_depay_change_state (GstElement * element, GstStateChange transition)
{
  GstRtpGSTDepay *rtpgstdepay;
  GstStateChangeReturn ret;

  rtpgstdepay = GST_RTP_GST_DEPAY (element);

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      gst_rtp_gst_depay_reset (rtpgstdepay);
      break;
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      gst_rtp_gst_depay_reset (rtpgstdepay);
      break;
    default:
      break;
  }
  return ret;
}


360 361 362 363 364 365
gboolean
gst_rtp_gst_depay_plugin_init (GstPlugin * plugin)
{
  return gst_element_register (plugin, "rtpgstdepay",
      GST_RANK_MARGINAL, GST_TYPE_RTP_GST_DEPAY);
}