gst/rtp/: Remove old code that is now in gst-libs/gst/rtp/.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtph263ppay.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 
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #  include "config.h"
17 #endif
18
19 #include <string.h>
20
21 #include <gst/rtp/gstrtpbuffer.h>
22
23 #include "gstrtph263penc.h"
24
25 /* elementfactory information */
26 static GstElementDetails gst_rtp_h263penc_details = {
27   "RTP packet parser",
28   "Codec/Parser/Network",
29   "Extracts H263+ video from RTP packets",
30   "Wim Taymans <wim@fluendo.com>"
31 };
32
33 /* RtpH263PEnc signals and args */
34 enum
35 {
36   /* FILL ME */
37   LAST_SIGNAL
38 };
39
40 #define DEFAULT_MTU     1024
41
42 enum
43 {
44   PROP_0,
45   PROP_MTU
46 };
47
48 static GstStaticPadTemplate gst_rtph263penc_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("video/x-h263")
53     );
54
55 static GstStaticPadTemplate gst_rtph263penc_src_template =
56 GST_STATIC_PAD_TEMPLATE ("src",
57     GST_PAD_SRC,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp")
60     );
61
62
63 static void gst_rtph263penc_class_init (GstRtpH263PEncClass * klass);
64 static void gst_rtph263penc_base_init (GstRtpH263PEncClass * klass);
65 static void gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc);
66
67 static GstFlowReturn gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer);
68
69 static void gst_rtph263penc_set_property (GObject * object, guint prop_id,
70     const GValue * value, GParamSpec * pspec);
71 static void gst_rtph263penc_get_property (GObject * object, guint prop_id,
72     GValue * value, GParamSpec * pspec);
73
74 static GstElementStateReturn gst_rtph263penc_change_state (GstElement *
75     element);
76
77 static GstElementClass *parent_class = NULL;
78
79 static GType
80 gst_rtph263penc_get_type (void)
81 {
82   static GType rtph263penc_type = 0;
83
84   if (!rtph263penc_type) {
85     static const GTypeInfo rtph263penc_info = {
86       sizeof (GstRtpH263PEncClass),
87       (GBaseInitFunc) gst_rtph263penc_base_init,
88       NULL,
89       (GClassInitFunc) gst_rtph263penc_class_init,
90       NULL,
91       NULL,
92       sizeof (GstRtpH263PEnc),
93       0,
94       (GInstanceInitFunc) gst_rtph263penc_init,
95     };
96
97     rtph263penc_type =
98         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpH263PEnc",
99         &rtph263penc_info, 0);
100   }
101   return rtph263penc_type;
102 }
103
104 static void
105 gst_rtph263penc_base_init (GstRtpH263PEncClass * klass)
106 {
107   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
108
109   gst_element_class_add_pad_template (element_class,
110       gst_static_pad_template_get (&gst_rtph263penc_src_template));
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&gst_rtph263penc_sink_template));
113
114   gst_element_class_set_details (element_class, &gst_rtp_h263penc_details);
115 }
116
117 static void
118 gst_rtph263penc_class_init (GstRtpH263PEncClass * klass)
119 {
120   GObjectClass *gobject_class;
121   GstElementClass *gstelement_class;
122
123   gobject_class = (GObjectClass *) klass;
124   gstelement_class = (GstElementClass *) klass;
125
126   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
127
128   gobject_class->set_property = gst_rtph263penc_set_property;
129   gobject_class->get_property = gst_rtph263penc_get_property;
130
131   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_MTU,
132       g_param_spec_uint ("mtu", "MTU",
133           "Maximum size of one packet",
134           28, G_MAXUINT, DEFAULT_MTU, G_PARAM_READWRITE));
135
136   gstelement_class->change_state = gst_rtph263penc_change_state;
137 }
138
139 static void
140 gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc)
141 {
142   rtph263penc->srcpad =
143       gst_pad_new_from_template (gst_static_pad_template_get
144       (&gst_rtph263penc_src_template), "src");
145   gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->srcpad);
146
147   rtph263penc->sinkpad =
148       gst_pad_new_from_template (gst_static_pad_template_get
149       (&gst_rtph263penc_sink_template), "sink");
150   gst_pad_set_chain_function (rtph263penc->sinkpad, gst_rtph263penc_chain);
151   gst_element_add_pad (GST_ELEMENT (rtph263penc), rtph263penc->sinkpad);
152
153   rtph263penc->adapter = gst_adapter_new ();
154   rtph263penc->mtu = DEFAULT_MTU;
155 }
156
157 static GstFlowReturn
158 gst_rtph263penc_flush (GstRtpH263PEnc * rtph263penc)
159 {
160   guint avail;
161   GstBuffer *outbuf;
162   GstFlowReturn ret;
163   gboolean fragmented;
164
165   avail = gst_adapter_available (rtph263penc->adapter);
166   if (avail == 0)
167     return GST_FLOW_OK;
168
169   fragmented = FALSE;
170
171   while (avail > 0) {
172     guint towrite;
173     guint8 *payload;
174     guint8 *data;
175     guint payload_len;
176     gint header_len;
177
178     /* FIXME, do better mtu packing, header len etc should be
179      * included in this calculation. */
180     towrite = MIN (avail, rtph263penc->mtu);
181     /* for fragmented frames we need 2 bytes header, for other
182      * frames we must reuse the first 2 bytes of the data as the
183      * header */
184     header_len = (fragmented ? 2 : 0);
185     payload_len = header_len + towrite;
186
187     outbuf = gst_rtpbuffer_new_allocate (payload_len, 0, 0);
188     gst_rtpbuffer_set_padding (outbuf, 0);
189     gst_rtpbuffer_set_timestamp (outbuf,
190         rtph263penc->first_ts * 90000 / GST_SECOND);
191     gst_rtpbuffer_set_payload_type (outbuf, 0);
192     /* last fragment gets the marker bit set */
193     gst_rtpbuffer_set_marker (outbuf, avail > towrite ? 0 : 1);
194
195     payload = gst_rtpbuffer_get_payload (outbuf);
196
197     data = (guint8 *) gst_adapter_peek (rtph263penc->adapter, towrite);
198     memcpy (&payload[header_len], data, towrite);
199
200     payload[0] = fragmented ? 0x00 : 0x04;
201     payload[1] = 0;
202
203     GST_BUFFER_TIMESTAMP (outbuf) = rtph263penc->first_ts;
204     gst_adapter_flush (rtph263penc->adapter, towrite);
205
206     ret = gst_pad_push (rtph263penc->srcpad, outbuf);
207
208     avail -= towrite;
209     fragmented = TRUE;
210   }
211
212   return ret;
213 }
214
215 static GstFlowReturn
216 gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer)
217 {
218   GstRtpH263PEnc *rtph263penc;
219   GstFlowReturn ret;
220   guint size;
221
222   rtph263penc = GST_RTP_H263P_ENC (GST_OBJECT_PARENT (pad));
223
224   size = GST_BUFFER_SIZE (buffer);
225   rtph263penc->first_ts = GST_BUFFER_TIMESTAMP (buffer);
226
227   /* we always encode and flush a full picture */
228   gst_adapter_push (rtph263penc->adapter, buffer);
229   ret = gst_rtph263penc_flush (rtph263penc);
230
231   return ret;
232 }
233
234 static void
235 gst_rtph263penc_set_property (GObject * object, guint prop_id,
236     const GValue * value, GParamSpec * pspec)
237 {
238   GstRtpH263PEnc *rtph263penc;
239
240   rtph263penc = GST_RTP_H263P_ENC (object);
241
242   switch (prop_id) {
243     case PROP_MTU:
244       rtph263penc->mtu = g_value_get_uint (value);
245       break;
246     default:
247       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248       break;
249   }
250 }
251
252 static void
253 gst_rtph263penc_get_property (GObject * object, guint prop_id, GValue * value,
254     GParamSpec * pspec)
255 {
256   GstRtpH263PEnc *rtph263penc;
257
258   rtph263penc = GST_RTP_H263P_ENC (object);
259
260   switch (prop_id) {
261     case PROP_MTU:
262       g_value_set_uint (value, rtph263penc->mtu);
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267   }
268 }
269
270 static GstElementStateReturn
271 gst_rtph263penc_change_state (GstElement * element)
272 {
273   GstRtpH263PEnc *rtph263penc;
274   gint transition;
275   GstElementStateReturn ret;
276
277   rtph263penc = GST_RTP_H263P_ENC (element);
278   transition = GST_STATE_TRANSITION (element);
279
280   switch (transition) {
281     case GST_STATE_NULL_TO_READY:
282       break;
283     default:
284       break;
285   }
286
287   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element);
288
289   switch (transition) {
290     case GST_STATE_READY_TO_NULL:
291       break;
292     default:
293       break;
294   }
295   return ret;
296 }
297
298 gboolean
299 gst_rtph263penc_plugin_init (GstPlugin * plugin)
300 {
301   return gst_element_register (plugin, "rtph263penc",
302       GST_RANK_NONE, GST_TYPE_RTP_H263P_ENC);
303 }