gstrtpmp4vdepay.c 7.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GStreamer
 * Copyright (C) <2005> Wim Taymans <wim@fluendo.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 
 */

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

#include <gst/rtp/gstrtpbuffer.h>

#include <string.h>
22
#include "gstrtpmp4vdepay.h"
23 24

/* elementfactory information */
25
static GstElementDetails gst_rtp_mp4vdepay_details = {
26
  "RTP packet parser",
27
  "Codec/Depayr/Network",
28 29 30 31
  "Extracts MPEG4 video from RTP packets (RFC 3016)",
  "Wim Taymans <wim@fluendo.com>"
};

32
/* RtpMP4VDepay signals and args */
33 34 35 36 37 38 39 40 41 42 43 44
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  ARG_0,
  ARG_FREQUENCY
};

45
static GstStaticPadTemplate gst_rtp_mp4v_depay_src_template =
46 47 48 49 50 51 52
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/mpeg,"
        "mpegversion=(int) 4," "systemstream=(boolean)false")
    );

53
static GstStaticPadTemplate gst_rtp_mp4v_depay_sink_template =
54 55 56
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
57 58
    GST_STATIC_CAPS ("application/x-rtp, "
        "media = (string) \"video\", "
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59
        "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
60 61 62 63 64 65 66 67
        /* All optional parameters
         *
         * "profile-level-id=[1,MAX]"
         * "config=" 
         */
    )
    );

68 69
GST_BOILERPLATE (GstRtpMP4VDepay, gst_rtp_mp4v_depay, GstBaseRTPDepayload,
    GST_TYPE_BASE_RTP_DEPAYLOAD);
70

71 72 73 74
static gboolean gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload,
    GstCaps * caps);
static GstBuffer *gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload,
    GstBuffer * buf);
75

76
static void gst_rtp_mp4v_depay_set_property (GObject * object, guint prop_id,
77
    const GValue * value, GParamSpec * pspec);
78
static void gst_rtp_mp4v_depay_get_property (GObject * object, guint prop_id,
79 80
    GValue * value, GParamSpec * pspec);

81 82
static GstStateChangeReturn gst_rtp_mp4v_depay_change_state (GstElement *
    element, GstStateChange transition);
83 84 85


static void
86
gst_rtp_mp4v_depay_base_init (gpointer klass)
87 88 89 90
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
91
      gst_static_pad_template_get (&gst_rtp_mp4v_depay_src_template));
92
  gst_element_class_add_pad_template (element_class,
93
      gst_static_pad_template_get (&gst_rtp_mp4v_depay_sink_template));
94

95
  gst_element_class_set_details (element_class, &gst_rtp_mp4vdepay_details);
96 97 98
}

static void
99
gst_rtp_mp4v_depay_class_init (GstRtpMP4VDepayClass * klass)
100 101 102
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;
103
  GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
104 105 106 107

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

108 109 110 111 112 113
  gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;

  parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_DEPAYLOAD);

  gstbasertpdepayload_class->process = gst_rtp_mp4v_depay_process;
  gstbasertpdepayload_class->set_caps = gst_rtp_mp4v_depay_setcaps;
114

115 116
  gobject_class->set_property = gst_rtp_mp4v_depay_set_property;
  gobject_class->get_property = gst_rtp_mp4v_depay_get_property;
117

118
  gstelement_class->change_state = gst_rtp_mp4v_depay_change_state;
119 120 121
}

static void
122 123
gst_rtp_mp4v_depay_init (GstRtpMP4VDepay * rtpmp4vdepay,
    GstRtpMP4VDepayClass * klass)
124 125 126 127
{
}

static gboolean
128
gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
129
{
130

131
  GstStructure *structure;
132
  GstRtpMP4VDepay *rtpmp4vdepay;
133
  GstCaps *srccaps;
134
  const gchar *str;
135

136
  rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
137 138 139

  structure = gst_caps_get_structure (caps, 0);

140 141 142 143 144 145 146
  gint clock_rate = 90000;      // default

  if (gst_structure_has_field (structure, "clock-rate")) {
    gst_structure_get_int (structure, "clock-rate", &clock_rate);
  }

  depayload->clock_rate = clock_rate;
147 148 149 150 151

  srccaps = gst_caps_new_simple ("video/mpeg",
      "mpegversion", G_TYPE_INT, 4,
      "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);

152 153 154 155 156 157 158 159 160 161 162
  if ((str = gst_structure_get_string (structure, "config"))) {
    GValue v = { 0 };

    g_value_init (&v, GST_TYPE_BUFFER);
    if (gst_value_deserialize (&v, str)) {
      GstBuffer *buffer;

      buffer = gst_value_get_buffer (&v);
      gst_buffer_ref (buffer);
      g_value_unset (&v);

163
      gst_buffer_set_caps (buffer, srccaps);
164

165 166 167
      gst_caps_unref (srccaps);

      gst_pad_push (depayload->srcpad, buffer);
168 169 170 171 172
    } else {
      g_warning ("cannot convert config to buffer");
    }
  }

173 174 175
  return TRUE;
}

176 177
static GstBuffer *
gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
178
{
179
  GstRtpMP4VDepay *rtpmp4vdepay;
180 181
  GstBuffer *outbuf;

182
  rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
183

184
  if (!gst_rtp_buffer_validate (buf))
185 186 187 188 189 190 191
    goto bad_packet;

  {
    gint payload_len;
    guint8 *payload;
    guint32 timestamp;

192 193
    payload_len = gst_rtp_buffer_get_payload_len (buf);
    payload = gst_rtp_buffer_get_payload (buf);
194

195
    timestamp = gst_rtp_buffer_get_timestamp (buf);
196 197 198 199

    outbuf = gst_buffer_new_and_alloc (payload_len);
    memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);

200
    gst_adapter_push (rtpmp4vdepay->adapter, outbuf);
201 202

    /* if this was the last packet of the VOP, create and push a buffer */
203
    if (gst_rtp_buffer_get_marker (buf)) {
204 205
      guint avail;

206
      avail = gst_adapter_available (rtpmp4vdepay->adapter);
207 208

      outbuf = gst_buffer_new_and_alloc (avail);
209

210
      GST_BUFFER_MALLOCDATA (outbuf) =
211
          gst_adapter_take (rtpmp4vdepay->adapter, avail);
212
      GST_BUFFER_DATA (outbuf) = GST_BUFFER_MALLOCDATA (outbuf);
213
      gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
214
      GST_BUFFER_TIMESTAMP (outbuf) =
215
          timestamp * GST_SECOND / depayload->clock_rate;
216

217
      GST_DEBUG ("gst_rtp_mp4v_depay_chain: pushing buffer of size %d",
218 219
          GST_BUFFER_SIZE (outbuf));

220
      return outbuf;
221
    } else {
222
      return NULL;
223 224 225
    }
  }

226 227
  return NULL;

228 229
bad_packet:
  {
230 231
    GST_ELEMENT_WARNING (rtpmp4vdepay, STREAM, DECODE,
        ("Packet did not validate"), (NULL));
232

233
    return NULL;
234 235 236 237
  }
}

static void
238
gst_rtp_mp4v_depay_set_property (GObject * object, guint prop_id,
239 240
    const GValue * value, GParamSpec * pspec)
{
241
  GstRtpMP4VDepay *rtpmp4vdepay;
242

243
  rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
244 245 246 247 248 249 250 251 252

  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
253 254
gst_rtp_mp4v_depay_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
255
{
256
  GstRtpMP4VDepay *rtpmp4vdepay;
257

258
  rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
259 260 261 262 263 264 265 266 267

  switch (prop_id) {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstStateChangeReturn
268 269
gst_rtp_mp4v_depay_change_state (GstElement * element,
    GstStateChange transition)
270
{
271
  GstRtpMP4VDepay *rtpmp4vdepay;
272 273
  GstStateChangeReturn ret;

274
  rtpmp4vdepay = GST_RTP_MP4V_DEPAY (element);
275 276 277

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
278
      rtpmp4vdepay->adapter = gst_adapter_new ();
279 280
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
281
      gst_adapter_clear (rtpmp4vdepay->adapter);
282 283 284 285 286 287 288 289 290
      break;
    default:
      break;
  }

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

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:
291 292
      g_object_unref (rtpmp4vdepay->adapter);
      rtpmp4vdepay->adapter = NULL;
293 294 295 296 297 298 299 300
      break;
    default:
      break;
  }
  return ret;
}

gboolean
301
gst_rtp_mp4v_depay_plugin_init (GstPlugin * plugin)
302
{
303 304
  return gst_element_register (plugin, "rtpmp4vdepay",
      GST_RANK_NONE, GST_TYPE_RTP_MP4V_DEPAY);
305
}