gst/rtp/: Fix caps with payload numbers.
[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>");
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         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
64         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP2T-ES\";"
65         /* All optional parameters
66          *
67          * "profile-level-id=[1,MAX]"
68          * "config=" 
69          */
70         "application/x-rtp, "
71         "media = (string) \"video\", "
72         "payload = (int) " GST_RTP_PAYLOAD_MP2T_STRING ", "
73         "clock-rate = (int) [1, MAX ]")
74     );
75
76 GST_BOILERPLATE (GstRtpMP2TDepay, gst_rtp_mp2t_depay, GstBaseRTPDepayload,
77     GST_TYPE_BASE_RTP_DEPAYLOAD);
78
79 static gboolean gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload,
80     GstCaps * caps);
81 static GstBuffer *gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload,
82     GstBuffer * buf);
83
84 static void gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
85     const GValue * value, GParamSpec * pspec);
86 static void gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
87     GValue * value, GParamSpec * pspec);
88
89 static GstStateChangeReturn gst_rtp_mp2t_depay_change_state (GstElement *
90     element, GstStateChange transition);
91
92
93 static void
94 gst_rtp_mp2t_depay_base_init (gpointer klass)
95 {
96   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
97
98   gst_element_class_add_pad_template (element_class,
99       gst_static_pad_template_get (&gst_rtp_mp2t_depay_src_template));
100   gst_element_class_add_pad_template (element_class,
101       gst_static_pad_template_get (&gst_rtp_mp2t_depay_sink_template));
102
103   gst_element_class_set_details (element_class, &gst_rtp_mp2tdepay_details);
104 }
105
106 static void
107 gst_rtp_mp2t_depay_class_init (GstRtpMP2TDepayClass * klass)
108 {
109   GObjectClass *gobject_class;
110   GstElementClass *gstelement_class;
111   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
112
113   gobject_class = (GObjectClass *) klass;
114   gstelement_class = (GstElementClass *) klass;
115
116   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
117
118   parent_class = g_type_class_peek_parent (klass);
119
120   gstbasertpdepayload_class->process = gst_rtp_mp2t_depay_process;
121   gstbasertpdepayload_class->set_caps = gst_rtp_mp2t_depay_setcaps;
122
123   gobject_class->set_property = gst_rtp_mp2t_depay_set_property;
124   gobject_class->get_property = gst_rtp_mp2t_depay_get_property;
125
126   gstelement_class->change_state = gst_rtp_mp2t_depay_change_state;
127 }
128
129 static void
130 gst_rtp_mp2t_depay_init (GstRtpMP2TDepay * rtpmp2tdepay,
131     GstRtpMP2TDepayClass * klass)
132 {
133 }
134
135 static gboolean
136 gst_rtp_mp2t_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
137 {
138
139   GstStructure *structure;
140   GstRtpMP2TDepay *rtpmp2tdepay;
141   GstCaps *srccaps;
142   gint clock_rate = 90000;      /* default */
143
144   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
145
146   structure = gst_caps_get_structure (caps, 0);
147
148   if (gst_structure_has_field (structure, "clock-rate")) {
149     gst_structure_get_int (structure, "clock-rate", &clock_rate);
150   }
151
152   depayload->clock_rate = clock_rate;
153
154   srccaps = gst_caps_new_simple ("video/mpegts",
155       "systemstream", G_TYPE_BOOLEAN, TRUE,
156       "packetsize", G_TYPE_INT, 188, NULL);
157
158   return TRUE;
159 }
160
161 static GstBuffer *
162 gst_rtp_mp2t_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
163 {
164   GstRtpMP2TDepay *rtpmp2tdepay;
165   GstBuffer *outbuf;
166   gint payload_len;
167   guint8 *payload;
168   guint32 timestamp;
169
170   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
171
172   if (!gst_rtp_buffer_validate (buf))
173     goto bad_packet;
174
175   payload_len = gst_rtp_buffer_get_payload_len (buf);
176   payload = gst_rtp_buffer_get_payload (buf);
177
178   timestamp = gst_rtp_buffer_get_timestamp (buf);
179
180   outbuf = gst_buffer_new_and_alloc (payload_len);
181   memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
182
183   gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
184   GST_BUFFER_TIMESTAMP (outbuf) =
185       gst_util_uint64_scale_int (timestamp, GST_SECOND, depayload->clock_rate);
186
187   GST_DEBUG ("gst_rtp_mp2t_depay_chain: pushing buffer of size %d",
188       GST_BUFFER_SIZE (outbuf));
189
190   return outbuf;
191
192 bad_packet:
193   {
194     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
195         ("Packet did not validate"), (NULL));
196     return NULL;
197   }
198 }
199
200 static void
201 gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
202     const GValue * value, GParamSpec * pspec)
203 {
204   GstRtpMP2TDepay *rtpmp2tdepay;
205
206   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
207
208   switch (prop_id) {
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211       break;
212   }
213 }
214
215 static void
216 gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
217     GValue * value, GParamSpec * pspec)
218 {
219   GstRtpMP2TDepay *rtpmp2tdepay;
220
221   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (object);
222
223   switch (prop_id) {
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228 }
229
230 static GstStateChangeReturn
231 gst_rtp_mp2t_depay_change_state (GstElement * element,
232     GstStateChange transition)
233 {
234   GstRtpMP2TDepay *rtpmp2tdepay;
235   GstStateChangeReturn ret;
236
237   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (element);
238
239   switch (transition) {
240     case GST_STATE_CHANGE_NULL_TO_READY:
241       break;
242     case GST_STATE_CHANGE_READY_TO_PAUSED:
243       break;
244     default:
245       break;
246   }
247
248   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
249
250   switch (transition) {
251     case GST_STATE_CHANGE_READY_TO_NULL:
252       break;
253     default:
254       break;
255   }
256   return ret;
257 }
258
259 gboolean
260 gst_rtp_mp2t_depay_plugin_init (GstPlugin * plugin)
261 {
262   return gst_element_register (plugin, "rtpmp2tdepay",
263       GST_RANK_MARGINAL, GST_TYPE_RTP_MP2T_DEPAY);
264 }