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