gstrtpL16depay.c 9.37 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
 * Library General Public License for more
13 14
 */

15 16 17
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
18
#include <string.h>
19
#include "gstrtpL16depay.h"
20 21 22
#include "gstrtp-common.h"

/* elementfactory information */
23
static const GstElementDetails gst_rtp_L16depay_details =
Wim Taymans's avatar
Wim Taymans committed
24 25 26 27
GST_ELEMENT_DETAILS ("RTP packet parser",
    "Codec/Depayr/Network",
    "Extracts raw audio from RTP packets",
    "Zeeshan Ali <zak147@yahoo.com>");
28

29
/* RtpL16Depay signals and args */
30 31 32 33 34 35 36 37 38 39
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

enum
{
  ARG_0,
  ARG_FREQUENCY,
40
  ARG_PAYLOAD_TYPE
41 42
};

43
static GstStaticPadTemplate gst_rtp_L16depay_src_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44
GST_STATIC_PAD_TEMPLATE ("src",
David Schleef's avatar
David Schleef committed
45 46
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
47
    GST_STATIC_CAPS ("audio/x-raw-int, "
48 49 50 51 52
        "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
53
    );
David Schleef's avatar
David Schleef committed
54

55
static GstStaticPadTemplate gst_rtp_L16depay_sink_template =
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56
GST_STATIC_PAD_TEMPLATE ("sink",
David Schleef's avatar
David Schleef committed
57 58 59
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
60
    );
61

62 63 64
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);
65

66
static void gst_rtp_L16depay_chain (GstPad * pad, GstData * _data);
67

68
static void gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69
    const GValue * value, GParamSpec * pspec);
70
static void gst_rtp_L16depay_get_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71
    GValue * value, GParamSpec * pspec);
72
static GstStateChangeReturn gst_rtp_L16depay_change_state (GstElement *
73
    element);
74 75 76

static GstElementClass *parent_class = NULL;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
77
static GType
78
gst_rtp_L16depay_get_type (void)
79
{
80
  static GType rtpL16depay_type = 0;
81

82 83 84 85
  if (!rtpL16depay_type) {
    static const GTypeInfo rtpL16depay_info = {
      sizeof (GstRtpL16DepayClass),
      (GBaseInitFunc) gst_rtp_L16depay_base_init,
86
      NULL,
87
      (GClassInitFunc) gst_rtp_L16depay_class_init,
88 89
      NULL,
      NULL,
90
      sizeof (GstRtpL16Depay),
91
      0,
92
      (GInstanceInitFunc) gst_rtp_L16depay_init,
93 94
    };

95 96 97
    rtpL16depay_type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Depay",
        &rtpL16depay_info, 0);
98
  }
99
  return rtpL16depay_type;
100 101
}

Ronald S. Bultje's avatar
Ronald S. Bultje committed
102
static void
103
gst_rtp_L16depay_base_init (GstRtpL16DepayClass * klass)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
104 105 106 107
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gst_element_class_add_pad_template (element_class,
108
      gst_static_pad_template_get (&gst_rtp_L16depay_src_template));
Ronald S. Bultje's avatar
Ronald S. Bultje committed
109
  gst_element_class_add_pad_template (element_class,
110 111
      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
112 113
}

114
static void
115
gst_rtp_L16depay_class_init (GstRtpL16DepayClass * klass)
116 117 118 119 120 121 122
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

123
  parent_class = g_type_class_peek_parent (klass);
124

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
125 126
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PAYLOAD_TYPE,
      g_param_spec_int ("payload_type", "payload_type", "payload type",
127
          G_MININT, G_MAXINT, PAYLOAD_L16_STEREO, G_PARAM_READABLE));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
128 129
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_FREQUENCY,
      g_param_spec_int ("frequency", "frequency", "frequency",
130
          G_MININT, G_MAXINT, 44100, G_PARAM_READWRITE));
131

132 133
  gobject_class->set_property = gst_rtp_L16depay_set_property;
  gobject_class->get_property = gst_rtp_L16depay_get_property;
134

135
  gstelement_class->change_state = gst_rtp_L16depay_change_state;
136 137 138
}

static void
139
gst_rtp_L16depay_init (GstRtpL16Depay * rtpL16depay)
140
{
141
  rtpL16depay->srcpad =
142
      gst_pad_new_from_static_template (&gst_rtp_L16depay_src_template, "src");
143
  rtpL16depay->sinkpad =
144 145
      gst_pad_new_from_static_template (&gst_rtp_L16depay_sink_template,
      "sink");
146 147 148
  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);
149

150 151
  rtpL16depay->frequency = 44100;
  rtpL16depay->channels = 2;
152

153
  rtpL16depay->payload_type = PAYLOAD_L16_STEREO;
154 155 156
}

void
157
gst_rtp_L16depay_ntohs (GstBuffer * buf)
158
{
Zeeshan Ali's avatar
Zeeshan Ali committed
159
  gint16 *i, *len;
160 161

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

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
165 166
  for (; i < len; i++) {
    *i = g_ntohs (*i);
167 168 169 170
  }
}

void
171
gst_rtp_L16_caps_nego (GstRtpL16Depay * rtpL16depay)
172 173 174
{
  GstCaps *caps;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
175
  caps =
176
      gst_caps_copy (gst_static_caps_get (&gst_rtp_L16depay_src_template.
177
          static_caps));
David Schleef's avatar
David Schleef committed
178 179

  gst_caps_set_simple (caps,
180 181
      "rate", G_TYPE_INT, rtpL16depay->frequency,
      "channel", G_TYPE_INT, rtpL16depay->channels, NULL);
182

183
  gst_pad_try_set_caps (rtpL16depay->srcpad, caps);
184 185 186
}

void
187
gst_rtp_L16depay_payloadtype_change (GstRtpL16Depay * rtpL16depay,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
188
    rtp_payload_t pt)
189
{
190
  rtpL16depay->payload_type = pt;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
191

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

203
  gst_rtp_L16_caps_nego (rtpL16depay);
204 205 206
}

static void
207
gst_rtp_L16depay_chain (GstPad * pad, GstData * _data)
208
{
209
  GstBuffer *buf = GST_BUFFER (_data);
210
  GstRtpL16Depay *rtpL16depay;
211 212 213 214 215 216 217 218
  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);

219
  rtpL16depay = GST_RTP_L16_DEPAY (GST_OBJECT_PARENT (pad));
220

221 222
  g_return_if_fail (rtpL16depay != NULL);
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (rtpL16depay));
223 224 225

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

227
    gst_pad_event_default (pad, event);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
228

229 230 231
    return;
  }

232 233
  if (GST_PAD_CAPS (rtpL16depay->srcpad) == NULL) {
    gst_rtp_L16_caps_nego (rtpL16depay);
234 235
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236 237
  packet =
      rtp_packet_new_copy_data (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
238 239 240

  pt = rtp_packet_get_payload_type (packet);

241 242
  if (pt != rtpL16depay->payload_type) {
    gst_rtp_L16depay_payloadtype_change (rtpL16depay, pt);
243 244 245 246 247
  }

  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
248 249 250 251 252
  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));
253

254
  GST_DEBUG ("gst_rtp_L16depay_chain: pushing buffer of size %d",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
255
      GST_BUFFER_SIZE (outbuf));
256 257 258

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

262
  gst_pad_push (rtpL16depay->srcpad, GST_DATA (outbuf));
263 264 265 266 267 268

  rtp_packet_free (packet);
  gst_buffer_unref (buf);
}

static void
269
gst_rtp_L16depay_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
270
    const GValue * value, GParamSpec * pspec)
271
{
272
  GstRtpL16Depay *rtpL16depay;
273

274 275
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
  rtpL16depay = GST_RTP_L16_DEPAY (object);
276 277 278

  switch (prop_id) {
    case ARG_PAYLOAD_TYPE:
279
      gst_rtp_L16depay_payloadtype_change (rtpL16depay,
280
          g_value_get_int (value));
281 282
      break;
    case ARG_FREQUENCY:
283
      rtpL16depay->frequency = g_value_get_int (value);
284 285 286 287 288 289 290
      break;
    default:
      break;
  }
}

static void
291
gst_rtp_L16depay_get_property (GObject * object, guint prop_id, GValue * value,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
292
    GParamSpec * pspec)
293
{
294
  GstRtpL16Depay *rtpL16depay;
295

296 297
  g_return_if_fail (GST_IS_RTP_L16_DEPAY (object));
  rtpL16depay = GST_RTP_L16_DEPAY (object);
298 299 300

  switch (prop_id) {
    case ARG_PAYLOAD_TYPE:
301
      g_value_set_int (value, rtpL16depay->payload_type);
302 303
      break;
    case ARG_FREQUENCY:
304
      g_value_set_int (value, rtpL16depay->frequency);
305 306 307 308 309 310 311
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

312
static GstStateChangeReturn
313
gst_rtp_L16depay_change_state (GstElement * element, GstStateChange transition)
314
{
315
  GstRtpL16Depay *rtpL16depay;
316

317
  g_return_val_if_fail (GST_IS_RTP_L16_DEPAY (element),
318
      GST_STATE_CHANGE_FAILURE);
319

320
  rtpL16depay = GST_RTP_L16_DEPAY (element);
321

322
  GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
323

324 325
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
326
      break;
327
    case GST_STATE_CHANGE_READY_TO_NULL:
328 329 330 331 332 333 334
      break;
    default:
      break;
  }

  /* if we haven't failed already, give the parent class a chance to ;-) */
  if (GST_ELEMENT_CLASS (parent_class)->change_state)
335
    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
336

337
  return GST_STATE_CHANGE_SUCCESS;
338 339 340
}

gboolean
341
gst_rtp_L16depay_plugin_init (GstPlugin * plugin)
342
{
343 344
  return gst_element_register (plugin, "rtpL16depay",
      GST_RANK_NONE, GST_TYPE_RTP_L16_DEPAY);
345
}