good: use new gst_element_class_add_static_pad_template()
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp2tdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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 #include "gstrtputils.h"
29
30 /* RtpMP2TDepay signals and args */
31 enum
32 {
33   /* FILL ME */
34   LAST_SIGNAL
35 };
36
37 #define DEFAULT_SKIP_FIRST_BYTES        0
38
39 enum
40 {
41   PROP_0,
42   PROP_SKIP_FIRST_BYTES
43 };
44
45 static GstStaticPadTemplate gst_rtp_mp2t_depay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("video/mpegts,"
50         "packetsize=(int)188," "systemstream=(boolean)true")
51     );
52
53 static GstStaticPadTemplate gst_rtp_mp2t_depay_sink_template =
54     GST_STATIC_PAD_TEMPLATE ("sink",
55     GST_PAD_SINK,
56     GST_PAD_ALWAYS,
57     GST_STATIC_CAPS ("application/x-rtp, "
58         "media = (string) \"video\", "
59         "clock-rate = (int) [1, MAX ], "
60         "encoding-name = (string) { MP2T, MP2T-ES } ;"
61         /* All optional parameters
62          *
63          * "profile-level-id=[1,MAX]"
64          * "config=" 
65          */
66         "application/x-rtp, "
67         "media = (string) \"video\", "
68         "payload = (int) " GST_RTP_PAYLOAD_MP2T_STRING ", "
69         "clock-rate = (int) [1, MAX ]")
70     );
71
72 G_DEFINE_TYPE (GstRtpMP2TDepay, gst_rtp_mp2t_depay,
73     GST_TYPE_RTP_BASE_DEPAYLOAD);
74
75 static gboolean gst_rtp_mp2t_depay_setcaps (GstRTPBaseDepayload * depayload,
76     GstCaps * caps);
77 static GstBuffer *gst_rtp_mp2t_depay_process (GstRTPBaseDepayload * depayload,
78     GstRTPBuffer * rtp);
79
80 static void gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
81     const GValue * value, GParamSpec * pspec);
82 static void gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
83     GValue * value, GParamSpec * pspec);
84
85 static void
86 gst_rtp_mp2t_depay_class_init (GstRtpMP2TDepayClass * klass)
87 {
88   GObjectClass *gobject_class;
89   GstElementClass *gstelement_class;
90   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
91
92   gobject_class = (GObjectClass *) klass;
93   gstelement_class = (GstElementClass *) klass;
94   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
95
96   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mp2t_depay_process;
97   gstrtpbasedepayload_class->set_caps = gst_rtp_mp2t_depay_setcaps;
98
99   gobject_class->set_property = gst_rtp_mp2t_depay_set_property;
100   gobject_class->get_property = gst_rtp_mp2t_depay_get_property;
101
102   gst_element_class_add_static_pad_template (gstelement_class,
103       &gst_rtp_mp2t_depay_src_template);
104   gst_element_class_add_static_pad_template (gstelement_class,
105       &gst_rtp_mp2t_depay_sink_template);
106
107   gst_element_class_set_static_metadata (gstelement_class,
108       "RTP MPEG Transport Stream depayloader", "Codec/Depayloader/Network/RTP",
109       "Extracts MPEG2 TS from RTP packets (RFC 2250)",
110       "Wim Taymans <wim.taymans@gmail.com>, "
111       "Thijs Vermeir <thijs.vermeir@barco.com>");
112
113   g_object_class_install_property (gobject_class, PROP_SKIP_FIRST_BYTES,
114       g_param_spec_uint ("skip-first-bytes",
115           "Skip first bytes",
116           "The amount of bytes that need to be skipped at the beginning of the payload",
117           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118
119 }
120
121 static void
122 gst_rtp_mp2t_depay_init (GstRtpMP2TDepay * rtpmp2tdepay)
123 {
124   rtpmp2tdepay->skip_first_bytes = DEFAULT_SKIP_FIRST_BYTES;
125 }
126
127 static gboolean
128 gst_rtp_mp2t_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
129 {
130   GstCaps *srccaps;
131   GstStructure *structure;
132   gint clock_rate;
133   gboolean res;
134
135   structure = gst_caps_get_structure (caps, 0);
136   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
137     clock_rate = 90000;         /* default */
138   depayload->clock_rate = clock_rate;
139
140   srccaps = gst_caps_new_simple ("video/mpegts",
141       "packetsize", G_TYPE_INT, 188,
142       "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
143   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
144   gst_caps_unref (srccaps);
145
146   return res;
147 }
148
149 static GstBuffer *
150 gst_rtp_mp2t_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
151 {
152   GstRtpMP2TDepay *rtpmp2tdepay;
153   GstBuffer *outbuf;
154   gint payload_len, leftover;
155
156   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
157
158   payload_len = gst_rtp_buffer_get_payload_len (rtp);
159
160   if (G_UNLIKELY (payload_len <= rtpmp2tdepay->skip_first_bytes))
161     goto empty_packet;
162
163   payload_len -= rtpmp2tdepay->skip_first_bytes;
164
165   /* RFC 2250
166    *
167    * 2. Encapsulation of MPEG System and Transport Streams
168    *
169    * For MPEG2 Transport Streams the RTP payload will contain an integral
170    * number of MPEG transport packets.
171    */
172   leftover = payload_len % 188;
173   if (G_UNLIKELY (leftover)) {
174     GST_WARNING ("We don't have an integral number of buffers (leftover: %d)",
175         leftover);
176
177     payload_len -= leftover;
178   }
179
180   outbuf =
181       gst_rtp_buffer_get_payload_subbuffer (rtp,
182       rtpmp2tdepay->skip_first_bytes, payload_len);
183
184   if (outbuf) {
185     GST_DEBUG ("gst_rtp_mp2t_depay_chain: pushing buffer of size %"
186         G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
187
188     gst_rtp_drop_meta (GST_ELEMENT_CAST (depayload), outbuf, 0);
189   }
190
191   return outbuf;
192
193   /* ERRORS */
194 empty_packet:
195   {
196     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
197         (NULL), ("Packet was empty"));
198     return NULL;
199   }
200 }
201
202 static void
203 gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
204     const GValue * value, GParamSpec * pspec)
205 {
206   GstRtpMP2TDepay *rtpmp2tdepay;
207
208   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
209
210   switch (prop_id) {
211     case PROP_SKIP_FIRST_BYTES:
212       rtpmp2tdepay->skip_first_bytes = g_value_get_uint (value);
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220 static void
221 gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
222     GValue * value, GParamSpec * pspec)
223 {
224   GstRtpMP2TDepay *rtpmp2tdepay;
225
226   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
227
228   switch (prop_id) {
229     case PROP_SKIP_FIRST_BYTES:
230       g_value_set_uint (value, rtpmp2tdepay->skip_first_bytes);
231       break;
232     default:
233       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234       break;
235   }
236 }
237
238 gboolean
239 gst_rtp_mp2t_depay_plugin_init (GstPlugin * plugin)
240 {
241   return gst_element_register (plugin, "rtpmp2tdepay",
242       GST_RANK_SECONDARY, GST_TYPE_RTP_MP2T_DEPAY);
243 }