gst/rtp/: Added mpeg2 TS depayloader. Closing #347234.
[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 parser",
32     "Codec/Depay/Network",
33     "Extracts MPEG2 TS from RTP packets (RFC 2250)",
34     "Wim Taymans <wim@fluendo.com>");
35
36 /* RtpMP2TDepay signals and args */
37 enum
38 {
39   /* FILL ME */
40   LAST_SIGNAL
41 };
42
43 enum
44 {
45   ARG_0,
46   ARG_FREQUENCY
47 };
48
49 static GstStaticPadTemplate gst_rtp_mp2t_depay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("video/mpegts,"
54         "packetsize=(int)188," "systemstream=(boolean)true")
55     );
56
57 static GstStaticPadTemplate gst_rtp_mp2t_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp, "
62         "media = (string) \"video\", "
63         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP2T-ES\""
64         /* All optional parameters
65          *
66          * "profile-level-id=[1,MAX]"
67          * "config=" 
68          */
69     )
70     );
71
72 GST_BOILERPLATE (GstRtpMP2TDepay, gst_rtp_mp2t_depay, GstBaseRTPDepayload,
73     GST_TYPE_BASE_RTP_DEPAYLOAD);
74
75 static gboolean gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload,
76     GstCaps * caps);
77 static GstBuffer *gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload,
78     GstBuffer * buf);
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 GstStateChangeReturn gst_rtp_mp2t_depay_change_state (GstElement *
86     element, GstStateChange transition);
87
88
89 static void
90 gst_rtp_mp2t_depay_base_init (gpointer klass)
91 {
92   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
93
94   gst_element_class_add_pad_template (element_class,
95       gst_static_pad_template_get (&gst_rtp_mp2t_depay_src_template));
96   gst_element_class_add_pad_template (element_class,
97       gst_static_pad_template_get (&gst_rtp_mp2t_depay_sink_template));
98
99   gst_element_class_set_details (element_class, &gst_rtp_mp2tdepay_details);
100 }
101
102 static void
103 gst_rtp_mp2t_depay_class_init (GstRtpMP2TDepayClass * klass)
104 {
105   GObjectClass *gobject_class;
106   GstElementClass *gstelement_class;
107   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
108
109   gobject_class = (GObjectClass *) klass;
110   gstelement_class = (GstElementClass *) klass;
111
112   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
113
114   parent_class = g_type_class_peek_parent (klass);
115
116   gstbasertpdepayload_class->process = gst_rtp_mp2t_depay_process;
117   gstbasertpdepayload_class->set_caps = gst_rtp_mp2t_depay_setcaps;
118
119   gobject_class->set_property = gst_rtp_mp2t_depay_set_property;
120   gobject_class->get_property = gst_rtp_mp2t_depay_get_property;
121
122   gstelement_class->change_state = gst_rtp_mp2t_depay_change_state;
123 }
124
125 static void
126 gst_rtp_mp2t_depay_init (GstRtpMP2TDepay * rtpmp2tdepay,
127     GstRtpMP2TDepayClass * klass)
128 {
129 }
130
131 static gboolean
132 gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
133 {
134
135   GstStructure *structure;
136   GstRtpMP2TDepay *rtpmp2tdepay;
137   GstCaps *srccaps;
138   gint clock_rate = 90000;      /* default */
139
140   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
141
142   structure = gst_caps_get_structure (caps, 0);
143
144   if (gst_structure_has_field (structure, "clock-rate")) {
145     gst_structure_get_int (structure, "clock-rate", &clock_rate);
146   }
147
148   depayload->clock_rate = clock_rate;
149
150   srccaps = gst_caps_new_simple ("video/mpegts",
151       "systemstream", G_TYPE_BOOLEAN, TRUE,
152       "packetsize", G_TYPE_INT, 188, NULL);
153
154   return TRUE;
155 }
156
157 static GstBuffer *
158 gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
159 {
160   GstRtpMP2TDepay *rtpmp2tdepay;
161   GstBuffer *outbuf;
162   gint payload_len;
163   guint8 *payload;
164   guint32 timestamp;
165
166   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
167
168   if (!gst_rtp_buffer_validate (buf))
169     goto bad_packet;
170
171   payload_len = gst_rtp_buffer_get_payload_len (buf);
172   payload = gst_rtp_buffer_get_payload (buf);
173
174   timestamp = gst_rtp_buffer_get_timestamp (buf);
175
176   outbuf = gst_buffer_new_and_alloc (payload_len);
177   memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
178
179   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
180   GST_BUFFER_TIMESTAMP (outbuf) =
181       gst_util_uint64_scale_int (timestamp, GST_SECOND, depayload->clock_rate);
182
183   GST_DEBUG ("gst_rtp_mp2t_depay_chain: pushing buffer of size %d",
184       GST_BUFFER_SIZE (outbuf));
185
186   return outbuf;
187
188 bad_packet:
189   {
190     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
191         ("Packet did not validate"), (NULL));
192     return NULL;
193   }
194 }
195
196 static void
197 gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
198     const GValue * value, GParamSpec * pspec)
199 {
200   GstRtpMP2TDepay *rtpmp2tdepay;
201
202   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
203
204   switch (prop_id) {
205     default:
206       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207       break;
208   }
209 }
210
211 static void
212 gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
213     GValue * value, GParamSpec * pspec)
214 {
215   GstRtpMP2TDepay *rtpmp2tdepay;
216
217   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
218
219   switch (prop_id) {
220     default:
221       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222       break;
223   }
224 }
225
226 static GstStateChangeReturn
227 gst_rtp_mp2t_depay_change_state (GstElement * element,
228     GstStateChange transition)
229 {
230   GstRtpMP2TDepay *rtpmp2tdepay;
231   GstStateChangeReturn ret;
232
233   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (element);
234
235   switch (transition) {
236     case GST_STATE_CHANGE_NULL_TO_READY:
237       break;
238     case GST_STATE_CHANGE_READY_TO_PAUSED:
239       break;
240     default:
241       break;
242   }
243
244   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
245
246   switch (transition) {
247     case GST_STATE_CHANGE_READY_TO_NULL:
248       break;
249     default:
250       break;
251   }
252   return ret;
253 }
254
255 gboolean
256 gst_rtp_mp2t_depay_plugin_init (GstPlugin * plugin)
257 {
258   return gst_element_register (plugin, "rtpmp2tdepay",
259       GST_RANK_NONE, GST_TYPE_RTP_MP2T_DEPAY);
260 }