gstrtpL16depay.c 9.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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
12 13 14 15 16 17
 * 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.
18 19
 */

20 21 22
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
23
#include <string.h>
24
#include "gstrtpL16depay.h"
25 26 27
#include "gstrtp-common.h"

/* elementfactory information */
28
static const GstElementDetails gst_rtp_L16depay_details =
29
GST_ELEMENT_DETAILS ("RTP packet depayloader",
Wim Taymans's avatar
Wim Taymans committed
30
    "Codec/Depayloader/Network",
Wim Taymans's avatar
Wim Taymans committed
31 32
    "Extracts raw audio from RTP packets",
    "Zeeshan Ali <zak147@yahoo.com>");
33

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

enum
{
  ARG_0,
  ARG_FREQUENCY,
45
  ARG_PAYLOAD_TYPE
46 47
};

48
static GstStaticPadTemplate gst_rtp_L16depay_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
49
GST_STATIC_PAD_TEMPLATE ("src",
David Schleef's avatar
David Schleef committed
50 51
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
52
    GST_STATIC_CAPS ("audio/x-raw-int, "
53 54 55 56 57
        "endianness = (int) BYTE_ORDER, "
        "signed = (boolean) true, "
        "width = (int) 16, "
        "depth = (int) 16, "
        "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
58
    );
David Schleef's avatar
David Schleef committed
59

60
static GstStaticPadTemplate gst_rtp_L16depay_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
61
GST_STATIC_PAD_TEMPLATE ("sink",
David Schleef's avatar
David Schleef committed
62 63 64
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
65
    );
66

67 68 69
static void gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass);
static void gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass);
static void gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay);
70

71
static void gst_rtp_L16depay_chain (GstPad * pad, GstData * _data);
72

73
static void gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74
    const GValue * value, GParamSpec * pspec);
75
static void gst_rtp_L16depay_get_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
76
    GValue * value, GParamSpec * pspec);
77
static GstStateChangeReturn gst_rtp_L16depay_change_state (GstElement *
78
    element);
79 80 81

static GstElementClass *parent_class = NULL;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
82
static GType
83
gst_rtp_L16depay_get_type (void)
84
{
85
  static GType rtpL16depay_type = 0;
86

87 88 89 90
  if (!rtpL16depay_type) {
    static const GTypeInfo rtpL16depay_info = {
      sizeof (GstRtpL16DepayClass),
      (GBaseInitFunc) gst_rtp_L16depay_base_init,
91
      NULL,
92
      (GClassInitFunc) gst_rtp_L16depay_class_init,
93 94
      NULL,
      NULL,
95
      sizeof (GstRtpL16Depay),
96
      0,
97
      (GInstanceInitFunc) gst_rtp_L16depay_init,
98 99
    };

100 101 102
    rtpL16depay_type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Depay",
        &rtpL16depay_info, 0);
103
  }
104
  return rtpL16depay_type;
105 106
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
107
static void
108
gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
109 110 111 112
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
113
      gst_static_pad_template_get (&gst_rtp_L16depay_src_template));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
114
  gst_element_class_add_pad_template (element_class,
115 116
      gst_static_pad_template_get (&gst_rtp_L16depay_sink_template));
  gst_element_class_set_details (element_class, &gst_rtp_L16depay_details);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
117 118
}

119
static void
120
gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass)
121 122 123 124 125 126 127
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

128
  parent_class = g_type_class_peek_parent (klass);
129

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
130 131
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PAYLOAD_TYPE,
      g_param_spec_int ("payload_type", "payload_type", "payload type",
132
          G_MININT, G_MAXINT, PAYLOAD_L16_STEREO, G_PARAM_READABLE));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
133 134
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
      g_param_spec_int ("frequency", "frequency", "frequency",
135
          G_MININT, G_MAXINT, 44100, G_PARAM_READWRITE));
136

137 138
  gobject_class->set_property = gst_rtp_L16depay_set_property;
  gobject_class->get_property = gst_rtp_L16depay_get_property;
139

140
  gstelement_class->change_state = gst_rtp_L16depay_change_state;
141 142 143
}

static void
144
gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay)
145
{
146
  rtpL16depay->srcpad =
147
      gst_pad_new_from_static_template (&gst_rtp_L16depay_src_template, "src");
148
  rtpL16depay->sinkpad =
149 150
      gst_pad_new_from_static_template (&gst_rtp_L16depay_sink_template,
      "sink");
151 152 153
  gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->srcpad);
  gst_element_add_pad (GST_ELEMENT (rtpL16depay), rtpL16depay->sinkpad);
  gst_pad_set_chain_function (rtpL16depay->sinkpad, gst_rtp_L16depay_chain);
154

155 156
  rtpL16depay->frequency = 44100;
  rtpL16depay->channels = 2;
157

158
  rtpL16depay->payload_type = PAYLOAD_L16_STEREO;
159 160 161
}

void
162
gst_rtp_L16depay_ntohs (GstBuffer * buf)
163
{
Zeeshan Ali's avatar
Zeeshan Ali committed
164
  gint16 *i, *len;
165 166

  /* FIXME: is this code correct or even sane at all? */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
167
  i = (gint16 *) GST_BUFFER_DATA (buf);
Zeeshan Ali's avatar
Zeeshan Ali committed
168
  len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
169

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
170 171
  for (; i < len; i++) {
    *i = g_ntohs (*i);
172 173 174 175
  }
}

void
176
gst_rtp_L16_caps_nego (GstRtpL16Depay * rtpL16depay)
177 178 179
{
  GstCaps *caps;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
180
  caps =
181
      gst_caps_copy (gst_static_caps_get (&gst_rtp_L16depay_src_template.
182
          static_caps));
David Schleef's avatar
David Schleef committed
183 184

  gst_caps_set_simple (caps,
185 186
      "rate", G_TYPE_INT, rtpL16depay->frequency,
      "channel", G_TYPE_INT, rtpL16depay->channels, NULL);
187

188
  gst_pad_try_set_caps (rtpL16depay->srcpad, caps);
189 190 191
}

void
192
gst_rtp_L16depay_payloadtype_change (GstRtpL16Depay * rtpL16depay,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
193
    rtp_payload_t pt)
194
{
195
  rtpL16depay->payload_type = pt;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
196

197
  switch (pt) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
198
    case PAYLOAD_L16_MONO:
199
      rtpL16depay->channels = 1;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
200 201
      break;
    case PAYLOAD_L16_STEREO:
202
      rtpL16depay->channels = 2;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
203 204 205
      break;
    default:
      g_warning ("unknown payload_t %d\n", pt);
206 207
  }

208
  gst_rtp_L16_caps_nego (rtpL16depay);
209 210 211
}

static void
212
gst_rtp_L16depay_chain (GstPad * pad, GstData * _data)
213
{
214
  GstBuffer *buf = GST_BUFFER (_data);
215
  GstRtpL16Depay *rtpL16depay;
216 217 218 219 220 221 222 223
  GstBuffer *outbuf;
  Rtp_Packet packet;
  rtp_payload_t pt;

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));
  g_return_if_fail (buf != NULL);

224
  rtpL16depay = GST_RTP_L16_DEPAY (GST_OBJECT_PARENT (pad));
225

226 227
  g_return_if_fail (rtpL16depay != NULL);
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (rtpL16depay));
228 229 230

  if (GST_IS_EVENT (buf)) {
    GstEvent *event = GST_EVENT (buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
231

232
    gst_pad_event_default (pad, event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
233

234 235 236
    return;
  }

237 238
  if (GST_PAD_CAPS (rtpL16depay->srcpad) == NULL) {
    gst_rtp_L16_caps_nego (rtpL16depay);
239 240
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
241 242
  packet =
      rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
243 244 245

  pt = rtp_packet_get_payload_type (packet);

246 247
  if (pt != rtpL16depay->payload_type) {
    gst_rtp_L16depay_payloadtype_change (rtpL16depay, pt);
248 249 250 251 252
  }

  outbuf = gst_buffer_new ();
  GST_BUFFER_SIZE (outbuf) = rtp_packet_get_payload_len (packet);
  GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
253 254 255 256 257
  GST_BUFFER_TIMESTAMP (outbuf) =
      g_ntohl (rtp_packet_get_timestamp (packet)) * GST_SECOND;

  memcpy (GST_BUFFER_DATA (outbuf), rtp_packet_get_payload (packet),
      GST_BUFFER_SIZE (outbuf));
258

259
  GST_DEBUG ("gst_rtp_L16depay_chain: pushing buffer of size %d",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
260
      GST_BUFFER_SIZE (outbuf));
261 262 263

  /* FIXME: According to RFC 1890, this is required, right? */
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
264
  gst_rtp_L16depay_ntohs (outbuf);
265 266
#endif

267
  gst_pad_push (rtpL16depay->srcpad, GST_DATA (outbuf));
268 269 270 271 272 273

  rtp_packet_free (packet);
  gst_buffer_unref (buf);
}

static void
274
gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
275
    const GValue * value, GParamSpec * pspec)
276
{
277
  GstRtpL16Depay *rtpL16depay;
278

279 280
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
  rtpL16depay = GST_RTP_L16_DEPAY (object);
281 282 283

  switch (prop_id) {
    case ARG_PAYLOAD_TYPE:
284
      gst_rtp_L16depay_payloadtype_change (rtpL16depay,
285
          g_value_get_int (value));
286 287
      break;
    case ARG_FREQUENCY:
288
      rtpL16depay->frequency = g_value_get_int (value);
289 290 291 292 293 294 295
      break;
    default:
      break;
  }
}

static void
296
gst_rtp_L16depay_get_property (GObject * object, guint prop_id, GValue * value,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
297
    GParamSpec * pspec)
298
{
299
  GstRtpL16Depay *rtpL16depay;
300

301 302
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
  rtpL16depay = GST_RTP_L16_DEPAY (object);
303 304 305

  switch (prop_id) {
    case ARG_PAYLOAD_TYPE:
306
      g_value_set_int (value, rtpL16depay->payload_type);
307 308
      break;
    case ARG_FREQUENCY:
309
      g_value_set_int (value, rtpL16depay->frequency);
310 311 312 313 314 315 316
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

317
static GstStateChangeReturn
318
gst_rtp_L16depay_change_state (GstElement * element, GstStateChange transition)
319
{
320
  GstRtpL16Depay *rtpL16depay;
Wim Taymans's avatar
Wim Taymans committed
321
  GstStateChangeReturn ret;
322

323
  rtpL16depay = GST_RTP_L16_DEPAY (element);
324

325
  GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
326

Wim Taymans's avatar
Wim Taymans committed
327

328 329
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
330
      break;
Wim Taymans's avatar
Wim Taymans committed
331 332 333 334 335 336 337
    default:
      break;
  }
  /* if we haven't failed already, give the parent class a chance to ;-) */
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  switch (transition) {
338
    case GST_STATE_CHANGE_READY_TO_NULL:
339 340 341 342 343
      break;
    default:
      break;
  }

Wim Taymans's avatar
Wim Taymans committed
344
  return ret;
345 346 347
}

gboolean
348
gst_rtp_L16depay_plugin_init (GstPlugin * plugin)
349
{
350
  return gst_element_register (plugin, "rtpL16depay",
Wim Taymans's avatar
Wim Taymans committed
351
      GST_RANK_MARGINAL, GST_TYPE_RTP_L16_DEPAY);
352
}