gst/rtp/gstrtpmp2tdepay.c: Fix compilation.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp2tdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpmp2tdepay.h"
28
29 /* elementfactory information */
30 static const GstElementDetails gst_rtp_mp2tdepay_details =
31 GST_ELEMENT_DETAILS ("RTP packet depayloader",
32     "Codec/Depayloader/Network",
33     "Extracts MPEG2 TS from RTP packets (RFC 2250)",
34     "Wim Taymans <wim@fluendo.com>\n"
35     "Thijs Vermeir <thijs.vermeir@barco.com>");
36
37 /* RtpMP2TDepay signals and args */
38 enum
39 {
40   /* FILL ME */
41   LAST_SIGNAL
42 };
43
44 #define DEFAULT_SKIP_FIRST_BYTES        0
45
46 enum
47 {
48   PROP_0,
49   PROP_SKIP_FIRST_BYTES
50 };
51
52 static GstStaticPadTemplate gst_rtp_mp2t_depay_src_template =
53 GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/mpegts,"
57         "packetsize=(int)188," "systemstream=(boolean)true")
58     );
59
60 static GstStaticPadTemplate gst_rtp_mp2t_depay_sink_template =
61     GST_STATIC_PAD_TEMPLATE ("sink",
62     GST_PAD_SINK,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp, "
65         "media = (string) \"video\", "
66         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
67         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP2T-ES\";"
68         /* All optional parameters
69          *
70          * "profile-level-id=[1,MAX]"
71          * "config=" 
72          */
73         "application/x-rtp, "
74         "media = (string) \"video\", "
75         "payload = (int) " GST_RTP_PAYLOAD_MP2T_STRING ", "
76         "clock-rate = (int) [1, MAX ]")
77     );
78
79 GST_BOILERPLATE (GstRtpMP2TDepay, gst_rtp_mp2t_depay, GstBaseRTPDepayload,
80     GST_TYPE_BASE_RTP_DEPAYLOAD);
81
82 static gboolean gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload,
83     GstCaps * caps);
84 static GstBuffer *gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload,
85     GstBuffer * buf);
86
87 static void gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89 static void gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec);
91
92 static GstStateChangeReturn gst_rtp_mp2t_depay_change_state (GstElement *
93     element, GstStateChange transition);
94
95
96 static void
97 gst_rtp_mp2t_depay_base_init (gpointer klass)
98 {
99   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
100
101   gst_element_class_add_pad_template (element_class,
102       gst_static_pad_template_get (&gst_rtp_mp2t_depay_src_template));
103   gst_element_class_add_pad_template (element_class,
104       gst_static_pad_template_get (&gst_rtp_mp2t_depay_sink_template));
105
106   gst_element_class_set_details (element_class, &gst_rtp_mp2tdepay_details);
107 }
108
109 static void
110 gst_rtp_mp2t_depay_class_init (GstRtpMP2TDepayClass * klass)
111 {
112   GObjectClass *gobject_class;
113   GstElementClass *gstelement_class;
114   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
115
116   gobject_class = (GObjectClass *) klass;
117   gstelement_class = (GstElementClass *) klass;
118
119   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
120
121   parent_class = g_type_class_peek_parent (klass);
122
123   gstbasertpdepayload_class->process = gst_rtp_mp2t_depay_process;
124   gstbasertpdepayload_class->set_caps = gst_rtp_mp2t_depay_setcaps;
125
126   gobject_class->set_property = gst_rtp_mp2t_depay_set_property;
127   gobject_class->get_property = gst_rtp_mp2t_depay_get_property;
128
129   g_object_class_install_property (gobject_class, PROP_SKIP_FIRST_BYTES,
130       g_param_spec_uint ("skip-first-bytes",
131           "Skip first bytes",
132           "The amount of bytes that need to be skipped at the beginning of the payload",
133           0, G_MAXUINT, 0, G_PARAM_READWRITE));
134
135   gstelement_class->change_state = gst_rtp_mp2t_depay_change_state;
136 }
137
138 static void
139 gst_rtp_mp2t_depay_init (GstRtpMP2TDepay * depayload,
140     GstRtpMP2TDepayClass * klass)
141 {
142   GstRtpMP2TDepay *rtpmp2tdepay;
143
144   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
145
146   rtpmp2tdepay->skip_first_bytes = DEFAULT_SKIP_FIRST_BYTES;
147 }
148
149 static gboolean
150 gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
151 {
152
153   GstStructure *structure;
154   GstRtpMP2TDepay *rtpmp2tdepay;
155   gint clock_rate = 90000;      /* default */
156
157   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
158
159   structure = gst_caps_get_structure (caps, 0);
160
161   if (gst_structure_has_field (structure, "clock-rate")) {
162     gst_structure_get_int (structure, "clock-rate", &clock_rate);
163   }
164
165   depayload->clock_rate = clock_rate;
166
167   return TRUE;
168 }
169
170 static GstBuffer *
171 gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
172 {
173   GstRtpMP2TDepay *rtpmp2tdepay;
174   GstBuffer *outbuf;
175   gint payload_len;
176   guint32 timestamp;
177
178   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
179
180   if (G_UNLIKELY (!gst_rtp_buffer_validate (buf)))
181     goto bad_packet;
182
183   payload_len = gst_rtp_buffer_get_payload_len (buf);
184
185   if (G_UNLIKELY (payload_len <= rtpmp2tdepay->skip_first_bytes))
186     goto empty_packet;
187
188   timestamp = gst_rtp_buffer_get_timestamp (buf);
189
190   outbuf =
191       gst_rtp_buffer_get_payload_subbuffer (buf, rtpmp2tdepay->skip_first_bytes,
192       -1);
193
194   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
195   GST_BUFFER_TIMESTAMP (outbuf) =
196       gst_util_uint64_scale_int (timestamp, GST_SECOND, depayload->clock_rate);
197
198   GST_DEBUG ("gst_rtp_mp2t_depay_chain: pushing buffer of size %d",
199       GST_BUFFER_SIZE (outbuf));
200
201   return outbuf;
202
203   /* ERRORS */
204 bad_packet:
205   {
206     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
207         (NULL), ("Packet did not validate"));
208     return NULL;
209   }
210 empty_packet:
211   {
212     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
213         (NULL), ("Packet was empty"));
214     return NULL;
215   }
216 }
217
218 static void
219 gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
220     const GValue * value, GParamSpec * pspec)
221 {
222   GstRtpMP2TDepay *rtpmp2tdepay;
223
224   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
225
226   switch (prop_id) {
227     case PROP_SKIP_FIRST_BYTES:
228       rtpmp2tdepay->skip_first_bytes = g_value_get_uint (value);
229       break;
230     default:
231       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232       break;
233   }
234 }
235
236 static void
237 gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
238     GValue * value, GParamSpec * pspec)
239 {
240   GstRtpMP2TDepay *rtpmp2tdepay;
241
242   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
243
244   switch (prop_id) {
245     case PROP_SKIP_FIRST_BYTES:
246       g_value_set_uint (value, rtpmp2tdepay->skip_first_bytes);
247       break;
248     default:
249       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250       break;
251   }
252 }
253
254 static GstStateChangeReturn
255 gst_rtp_mp2t_depay_change_state (GstElement * element,
256     GstStateChange transition)
257 {
258   GstRtpMP2TDepay *rtpmp2tdepay;
259   GstStateChangeReturn ret;
260
261   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (element);
262
263   switch (transition) {
264     case GST_STATE_CHANGE_NULL_TO_READY:
265       break;
266     case GST_STATE_CHANGE_READY_TO_PAUSED:
267       break;
268     default:
269       break;
270   }
271
272   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
273
274   switch (transition) {
275     case GST_STATE_CHANGE_READY_TO_NULL:
276       break;
277     default:
278       break;
279   }
280   return ret;
281 }
282
283 gboolean
284 gst_rtp_mp2t_depay_plugin_init (GstPlugin * plugin)
285 {
286   return gst_element_register (plugin, "rtpmp2tdepay",
287       GST_RANK_MARGINAL, GST_TYPE_RTP_MP2T_DEPAY);
288 }