2 * Copyright (C) <2005> Wim Taymans <wim@fluendo.com>
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.
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.
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.
24 #include <gst/rtp/gstrtpbuffer.h>
27 #include "gstrtpmp4vdepay.h"
29 /* elementfactory information */
30 static const GstElementDetails gst_rtp_mp4vdepay_details =
31 GST_ELEMENT_DETAILS ("RTP packet depayloader",
32 "Codec/Depayloader/Network",
33 "Extracts MPEG4 video from RTP packets (RFC 3016)",
34 "Wim Taymans <wim@fluendo.com>");
36 /* RtpMP4VDepay signals and args */
49 static GstStaticPadTemplate gst_rtp_mp4v_depay_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
53 GST_STATIC_CAPS ("video/mpeg,"
54 "mpegversion=(int) 4," "systemstream=(boolean)false")
57 static GstStaticPadTemplate gst_rtp_mp4v_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
61 GST_STATIC_CAPS ("application/x-rtp, "
62 "media = (string) \"video\", "
63 "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
64 /* All optional parameters
66 * "profile-level-id=[1,MAX]"
72 GST_BOILERPLATE (GstRtpMP4VDepay, gst_rtp_mp4v_depay, GstBaseRTPDepayload,
73 GST_TYPE_BASE_RTP_DEPAYLOAD);
75 static gboolean gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload,
77 static GstBuffer *gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload,
80 static void gst_rtp_mp4v_depay_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_rtp_mp4v_depay_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstStateChangeReturn gst_rtp_mp4v_depay_change_state (GstElement *
86 element, GstStateChange transition);
90 gst_rtp_mp4v_depay_base_init (gpointer klass)
92 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
94 gst_element_class_add_pad_template (element_class,
95 gst_static_pad_template_get (&gst_rtp_mp4v_depay_src_template));
96 gst_element_class_add_pad_template (element_class,
97 gst_static_pad_template_get (&gst_rtp_mp4v_depay_sink_template));
99 gst_element_class_set_details (element_class, &gst_rtp_mp4vdepay_details);
103 gst_rtp_mp4v_depay_class_init (GstRtpMP4VDepayClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
107 GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
112 gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
114 gstbasertpdepayload_class->process = gst_rtp_mp4v_depay_process;
115 gstbasertpdepayload_class->set_caps = gst_rtp_mp4v_depay_setcaps;
117 gobject_class->set_property = gst_rtp_mp4v_depay_set_property;
118 gobject_class->get_property = gst_rtp_mp4v_depay_get_property;
120 gstelement_class->change_state = gst_rtp_mp4v_depay_change_state;
124 gst_rtp_mp4v_depay_init (GstRtpMP4VDepay * rtpmp4vdepay,
125 GstRtpMP4VDepayClass * klass)
130 gst_rtp_mp4v_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
133 GstStructure *structure;
134 GstRtpMP4VDepay *rtpmp4vdepay;
137 gint clock_rate = 90000; /* default */
139 rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
141 structure = gst_caps_get_structure (caps, 0);
143 if (gst_structure_has_field (structure, "clock-rate")) {
144 gst_structure_get_int (structure, "clock-rate", &clock_rate);
147 depayload->clock_rate = clock_rate;
149 srccaps = gst_caps_new_simple ("video/mpeg",
150 "mpegversion", G_TYPE_INT, 4,
151 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
153 if ((str = gst_structure_get_string (structure, "config"))) {
156 g_value_init (&v, GST_TYPE_BUFFER);
157 if (gst_value_deserialize (&v, str)) {
160 buffer = gst_value_get_buffer (&v);
161 gst_buffer_ref (buffer);
164 gst_buffer_set_caps (buffer, srccaps);
166 gst_caps_unref (srccaps);
168 gst_pad_push (depayload->srcpad, buffer);
170 g_warning ("cannot convert config to buffer");
178 gst_rtp_mp4v_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
180 GstRtpMP4VDepay *rtpmp4vdepay;
183 rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
185 if (!gst_rtp_buffer_validate (buf))
193 payload_len = gst_rtp_buffer_get_payload_len (buf);
194 payload = gst_rtp_buffer_get_payload (buf);
196 timestamp = gst_rtp_buffer_get_timestamp (buf);
198 outbuf = gst_buffer_new_and_alloc (payload_len);
199 memcpy (GST_BUFFER_DATA (outbuf), payload, payload_len);
201 gst_adapter_push (rtpmp4vdepay->adapter, outbuf);
203 /* if this was the last packet of the VOP, create and push a buffer */
204 if (gst_rtp_buffer_get_marker (buf)) {
207 avail = gst_adapter_available (rtpmp4vdepay->adapter);
209 outbuf = gst_buffer_new ();
210 GST_BUFFER_SIZE (outbuf) = avail;
211 GST_BUFFER_MALLOCDATA (outbuf) =
212 gst_adapter_take (rtpmp4vdepay->adapter, avail);
213 GST_BUFFER_DATA (outbuf) = GST_BUFFER_MALLOCDATA (outbuf);
214 gst_buffer_set_caps (outbuf, GST_PAD_CAPS (depayload->srcpad));
215 GST_BUFFER_TIMESTAMP (outbuf) =
216 timestamp * GST_SECOND / depayload->clock_rate;
218 GST_DEBUG ("gst_rtp_mp4v_depay_chain: pushing buffer of size %d",
219 GST_BUFFER_SIZE (outbuf));
231 GST_ELEMENT_WARNING (rtpmp4vdepay, STREAM, DECODE,
232 ("Packet did not validate"), (NULL));
239 gst_rtp_mp4v_depay_set_property (GObject * object, guint prop_id,
240 const GValue * value, GParamSpec * pspec)
242 GstRtpMP4VDepay *rtpmp4vdepay;
244 rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_rtp_mp4v_depay_get_property (GObject * object, guint prop_id,
255 GValue * value, GParamSpec * pspec)
257 GstRtpMP4VDepay *rtpmp4vdepay;
259 rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 static GstStateChangeReturn
269 gst_rtp_mp4v_depay_change_state (GstElement * element,
270 GstStateChange transition)
272 GstRtpMP4VDepay *rtpmp4vdepay;
273 GstStateChangeReturn ret;
275 rtpmp4vdepay = GST_RTP_MP4V_DEPAY (element);
277 switch (transition) {
278 case GST_STATE_CHANGE_NULL_TO_READY:
279 rtpmp4vdepay->adapter = gst_adapter_new ();
281 case GST_STATE_CHANGE_READY_TO_PAUSED:
282 gst_adapter_clear (rtpmp4vdepay->adapter);
288 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
290 switch (transition) {
291 case GST_STATE_CHANGE_READY_TO_NULL:
292 g_object_unref (rtpmp4vdepay->adapter);
293 rtpmp4vdepay->adapter = NULL;
302 gst_rtp_mp4v_depay_plugin_init (GstPlugin * plugin)
304 return gst_element_register (plugin, "rtpmp4vdepay",
305 GST_RANK_MARGINAL, GST_TYPE_RTP_MP4V_DEPAY);