rtpvp8: port some more to new memory API
[platform/upstream/gstreamer.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., 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 /* 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         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
59         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP2T\";"
60         /* All optional parameters
61          *
62          * "profile-level-id=[1,MAX]"
63          * "config=" 
64          */
65         "application/x-rtp, "
66         "media = (string) \"video\", "
67         "payload = (int) " GST_RTP_PAYLOAD_MP2T_STRING ", "
68         "clock-rate = (int) [1, MAX ]")
69     );
70
71 G_DEFINE_TYPE (GstRtpMP2TDepay, gst_rtp_mp2t_depay,
72     GST_TYPE_RTP_BASE_DEPAYLOAD);
73
74 static gboolean gst_rtp_mp2t_depay_setcaps (GstRTPBaseDepayload * depayload,
75     GstCaps * caps);
76 static GstBuffer *gst_rtp_mp2t_depay_process (GstRTPBaseDepayload * depayload,
77     GstBuffer * buf);
78
79 static void gst_rtp_mp2t_depay_set_property (GObject * object, guint prop_id,
80     const GValue * value, GParamSpec * pspec);
81 static void gst_rtp_mp2t_depay_get_property (GObject * object, guint prop_id,
82     GValue * value, GParamSpec * pspec);
83
84 static void
85 gst_rtp_mp2t_depay_class_init (GstRtpMP2TDepayClass * klass)
86 {
87   GObjectClass *gobject_class;
88   GstElementClass *gstelement_class;
89   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
90
91   gobject_class = (GObjectClass *) klass;
92   gstelement_class = (GstElementClass *) klass;
93   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
94
95   gstrtpbasedepayload_class->process = gst_rtp_mp2t_depay_process;
96   gstrtpbasedepayload_class->set_caps = gst_rtp_mp2t_depay_setcaps;
97
98   gobject_class->set_property = gst_rtp_mp2t_depay_set_property;
99   gobject_class->get_property = gst_rtp_mp2t_depay_get_property;
100
101   gst_element_class_add_pad_template (gstelement_class,
102       gst_static_pad_template_get (&gst_rtp_mp2t_depay_src_template));
103   gst_element_class_add_pad_template (gstelement_class,
104       gst_static_pad_template_get (&gst_rtp_mp2t_depay_sink_template));
105
106   gst_element_class_set_static_metadata (gstelement_class,
107       "RTP MPEG Transport Stream depayloader", "Codec/Depayloader/Network/RTP",
108       "Extracts MPEG2 TS from RTP packets (RFC 2250)",
109       "Wim Taymans <wim.taymans@gmail.com>, "
110       "Thijs Vermeir <thijs.vermeir@barco.com>");
111
112   g_object_class_install_property (gobject_class, PROP_SKIP_FIRST_BYTES,
113       g_param_spec_uint ("skip-first-bytes",
114           "Skip first bytes",
115           "The amount of bytes that need to be skipped at the beginning of the payload",
116           0, G_MAXUINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117
118 }
119
120 static void
121 gst_rtp_mp2t_depay_init (GstRtpMP2TDepay * rtpmp2tdepay)
122 {
123   rtpmp2tdepay->skip_first_bytes = DEFAULT_SKIP_FIRST_BYTES;
124 }
125
126 static gboolean
127 gst_rtp_mp2t_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
128 {
129   GstCaps *srccaps;
130   GstStructure *structure;
131   gint clock_rate;
132   gboolean res;
133
134   structure = gst_caps_get_structure (caps, 0);
135   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
136     clock_rate = 90000;         /* default */
137   depayload->clock_rate = clock_rate;
138
139   srccaps = gst_caps_new_simple ("video/mpegts",
140       "packetsize", G_TYPE_INT, 188,
141       "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
142   res = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
143   gst_caps_unref (srccaps);
144
145   return res;
146 }
147
148 static GstBuffer *
149 gst_rtp_mp2t_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
150 {
151   GstRtpMP2TDepay *rtpmp2tdepay;
152   GstBuffer *outbuf;
153   gint payload_len, leftover;
154   GstRTPBuffer rtp = { NULL };
155
156   rtpmp2tdepay = GST_RTP_MP2T_DEPAY (depayload);
157
158   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
159   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
160
161   if (G_UNLIKELY (payload_len <= rtpmp2tdepay->skip_first_bytes))
162     goto empty_packet;
163
164   payload_len -= rtpmp2tdepay->skip_first_bytes;
165
166   /* RFC 2250
167    *
168    * 2. Encapsulation of MPEG System and Transport Streams
169    *
170    * For MPEG2 Transport Streams the RTP payload will contain an integral
171    * number of MPEG transport packets.
172    */
173   leftover = payload_len % 188;
174   if (G_UNLIKELY (leftover)) {
175     GST_WARNING ("We don't have an integral number of buffers (leftover: %d)",
176         leftover);
177
178     payload_len -= leftover;
179   }
180
181   outbuf =
182       gst_rtp_buffer_get_payload_subbuffer (&rtp,
183       rtpmp2tdepay->skip_first_bytes, payload_len);
184
185   gst_rtp_buffer_unmap (&rtp);
186   if (outbuf)
187     GST_DEBUG ("gst_rtp_mp2t_depay_chain: pushing buffer of size %"
188         G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
189
190   return outbuf;
191
192   /* ERRORS */
193 empty_packet:
194   {
195     GST_ELEMENT_WARNING (rtpmp2tdepay, STREAM, DECODE,
196         (NULL), ("Packet was empty"));
197     gst_rtp_buffer_unmap (&rtp);
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 }