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
21 #include <gst/rtp/gstrtpbuffer.h>
23 #include "gstrtph263penc.h"
25 /* elementfactory information */
26 static GstElementDetails gst_rtp_h263penc_details = {
28 "Codec/Parser/Network",
29 "Extracts H263+ video from RTP packets",
30 "Wim Taymans <wim@fluendo.com>"
33 /* RtpH263PEnc signals and args */
40 #define DEFAULT_MTU 1024
42 #define DEFAULT_SSRC 0
52 static GstStaticPadTemplate gst_rtph263penc_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
56 GST_STATIC_CAPS ("video/x-h263")
59 static GstStaticPadTemplate gst_rtph263penc_src_template =
60 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_STATIC_CAPS ("application/x-rtp")
67 static void gst_rtph263penc_class_init (GstRtpH263PEncClass * klass);
68 static void gst_rtph263penc_base_init (GstRtpH263PEncClass * klass);
69 static void gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc);
71 static GstFlowReturn gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer);
73 static void gst_rtph263penc_set_property (GObject * object, guint prop_id,
74 const GValue * value, GParamSpec * pspec);
75 static void gst_rtph263penc_get_property (GObject * object, guint prop_id,
76 GValue * value, GParamSpec * pspec);
78 static GstStateChangeReturn gst_rtph263penc_change_state (GstElement *
79 element, GstStateChange transition);
81 static GstElementClass *parent_class = NULL;
84 gst_rtph263penc_get_type (void)
86 static GType rtph263penc_type = 0;
88 if (!rtph263penc_type) {
89 static const GTypeInfo rtph263penc_info = {
90 sizeof (GstRtpH263PEncClass),
91 (GBaseInitFunc) gst_rtph263penc_base_init,
93 (GClassInitFunc) gst_rtph263penc_class_init,
96 sizeof (GstRtpH263PEnc),
98 (GInstanceInitFunc) gst_rtph263penc_init,
102 g_type_register_static (GST_TYPE_ELEMENT, "GstRtpH263PEnc",
103 &rtph263penc_info, 0);
105 return rtph263penc_type;
109 gst_rtph263penc_base_init (GstRtpH263PEncClass * klass)
111 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
113 gst_element_class_add_pad_template (element_class,
114 gst_static_pad_template_get (&gst_rtph263penc_src_template));
115 gst_element_class_add_pad_template (element_class,
116 gst_static_pad_template_get (&gst_rtph263penc_sink_template));
118 gst_element_class_set_details (element_class, &gst_rtp_h263penc_details);
122 gst_rtph263penc_class_init (GstRtpH263PEncClass * klass)
124 GObjectClass *gobject_class;
125 GstElementClass *gstelement_class;
127 gobject_class = (GObjectClass *) klass;
128 gstelement_class = (GstElementClass *) klass;
130 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
132 gobject_class->set_property = gst_rtph263penc_set_property;
133 gobject_class->get_property = gst_rtph263penc_get_property;
135 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
136 g_param_spec_uint ("mtu", "MTU",
137 "Maximum size of one packet",
138 28, G_MAXUINT, DEFAULT_MTU, G_PARAM_READWRITE));
139 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_PT,
140 g_param_spec_uint ("pt", "payload type",
141 "The payload type of the packets",
142 0, 0x80, DEFAULT_PT, G_PARAM_READWRITE));
143 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
144 g_param_spec_uint ("ssrc", "SSRC",
145 "The SSRC of the packets",
146 0, G_MAXUINT, DEFAULT_SSRC, G_PARAM_READWRITE));
148 gstelement_class->change_state = gst_rtph263penc_change_state;
152 gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc)
154 rtph263penc->srcpad =
155 gst_pad_new_from_template (gst_static_pad_template_get
156 (&gst_rtph263penc_src_template), "src");
157 gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->srcpad);
159 rtph263penc->sinkpad =
160 gst_pad_new_from_template (gst_static_pad_template_get
161 (&gst_rtph263penc_sink_template), "sink");
162 gst_pad_set_chain_function (rtph263penc->sinkpad, gst_rtph263penc_chain);
163 gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->sinkpad);
165 rtph263penc->adapter = gst_adapter_new ();
166 rtph263penc->mtu = DEFAULT_MTU;
170 gst_rtph263penc_flush (GstRtpH263PEnc * rtph263penc)
177 avail = gst_adapter_available (rtph263penc->adapter);
190 /* FIXME, do better mtu packing, header len etc should be
191 * included in this calculation. */
192 towrite = MIN (avail, rtph263penc->mtu);
193 /* for fragmented frames we need 2 bytes header, for other
194 * frames we must reuse the first 2 bytes of the data as the
196 header_len = (fragmented ? 2 : 0);
197 payload_len = header_len + towrite;
199 outbuf = gst_rtpbuffer_new_allocate (payload_len, 0, 0);
200 gst_rtpbuffer_set_timestamp (outbuf,
201 rtph263penc->first_ts * 90000 / GST_SECOND);
202 gst_rtpbuffer_set_payload_type (outbuf, rtph263penc->pt);
203 gst_rtpbuffer_set_ssrc (outbuf, rtph263penc->ssrc);
204 gst_rtpbuffer_set_seq (outbuf, rtph263penc->seqnum++);
205 /* last fragment gets the marker bit set */
206 gst_rtpbuffer_set_marker (outbuf, avail > towrite ? 0 : 1);
208 payload = gst_rtpbuffer_get_payload (outbuf);
210 data = (guint8 *) gst_adapter_peek (rtph263penc->adapter, towrite);
211 memcpy (&payload[header_len], data, towrite);
214 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
215 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216 * | RR |P|V| PLEN |PEBIT|
217 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 payload[0] = fragmented ? 0x00 : 0x04;
222 GST_BUFFER_TIMESTAMP (outbuf) = rtph263penc->first_ts;
223 gst_adapter_flush (rtph263penc->adapter, towrite);
225 ret = gst_pad_push (rtph263penc->srcpad, outbuf);
235 gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer)
237 GstRtpH263PEnc *rtph263penc;
241 rtph263penc = GST_RTP_H263P_ENC (GST_OBJECT_PARENT (pad));
243 size = GST_BUFFER_SIZE (buffer);
244 rtph263penc->first_ts = GST_BUFFER_TIMESTAMP (buffer);
246 /* we always encode and flush a full picture */
247 gst_adapter_push (rtph263penc->adapter, buffer);
248 ret = gst_rtph263penc_flush (rtph263penc);
254 gst_rtph263penc_set_property (GObject * object, guint prop_id,
255 const GValue * value, GParamSpec * pspec)
257 GstRtpH263PEnc *rtph263penc;
259 rtph263penc = GST_RTP_H263P_ENC (object);
263 rtph263penc->mtu = g_value_get_uint (value);
266 rtph263penc->pt = g_value_get_uint (value);
269 rtph263penc->ssrc = g_value_get_uint (value);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_rtph263penc_get_property (GObject * object, guint prop_id, GValue * value,
281 GstRtpH263PEnc *rtph263penc;
283 rtph263penc = GST_RTP_H263P_ENC (object);
287 g_value_set_uint (value, rtph263penc->mtu);
290 g_value_set_uint (value, rtph263penc->pt);
293 g_value_set_uint (value, rtph263penc->ssrc);
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 static GstStateChangeReturn
302 gst_rtph263penc_change_state (GstElement * element, GstStateChange transition)
304 GstRtpH263PEnc *rtph263penc;
305 GstStateChangeReturn ret;
307 rtph263penc = GST_RTP_H263P_ENC (element);
309 switch (transition) {
310 case GST_STATE_CHANGE_NULL_TO_READY:
316 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
318 switch (transition) {
319 case GST_STATE_CHANGE_READY_TO_NULL:
328 gst_rtph263penc_plugin_init (GstPlugin * plugin)
330 return gst_element_register (plugin, "rtph263penc",
331 GST_RANK_NONE, GST_TYPE_RTP_H263P_ENC);