All plugins updated for element state changes.
[platform/upstream/gstreamer.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 #define DEFAULT_PT      96
42 #define DEFAULT_SSRC    0
43
44 enum
45 {
46   PROP_0,
47   PROP_MTU,
48   PROP_PT,
49   PROP_SSRC
50 };
51
52 static GstStaticPadTemplate gst_rtph263penc_sink_template =
53 GST_STATIC_PAD_TEMPLATE ("sink",
54     GST_PAD_SINK,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("video/x-h263")
57     );
58
59 static GstStaticPadTemplate gst_rtph263penc_src_template =
60 GST_STATIC_PAD_TEMPLATE ("src",
61     GST_PAD_SRC,
62     GST_PAD_ALWAYS,
63     GST_STATIC_CAPS ("application/x-rtp")
64     );
65
66
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);
70
71 static GstFlowReturn gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer);
72
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);
77
78 static GstStateChangeReturn gst_rtph263penc_change_state (GstElement *
79     element, GstStateChange transition);
80
81 static GstElementClass *parent_class = NULL;
82
83 static GType
84 gst_rtph263penc_get_type (void)
85 {
86   static GType rtph263penc_type = 0;
87
88   if (!rtph263penc_type) {
89     static const GTypeInfo rtph263penc_info = {
90       sizeof (GstRtpH263PEncClass),
91       (GBaseInitFunc) gst_rtph263penc_base_init,
92       NULL,
93       (GClassInitFunc) gst_rtph263penc_class_init,
94       NULL,
95       NULL,
96       sizeof (GstRtpH263PEnc),
97       0,
98       (GInstanceInitFunc) gst_rtph263penc_init,
99     };
100
101     rtph263penc_type =
102         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpH263PEnc",
103         &rtph263penc_info, 0);
104   }
105   return rtph263penc_type;
106 }
107
108 static void
109 gst_rtph263penc_base_init (GstRtpH263PEncClass * klass)
110 {
111   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
112
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));
117
118   gst_element_class_set_details (element_class, &gst_rtp_h263penc_details);
119 }
120
121 static void
122 gst_rtph263penc_class_init (GstRtpH263PEncClass * klass)
123 {
124   GObjectClass *gobject_class;
125   GstElementClass *gstelement_class;
126
127   gobject_class = (GObjectClass *) klass;
128   gstelement_class = (GstElementClass *) klass;
129
130   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
131
132   gobject_class->set_property = gst_rtph263penc_set_property;
133   gobject_class->get_property = gst_rtph263penc_get_property;
134
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));
147
148   gstelement_class->change_state = gst_rtph263penc_change_state;
149 }
150
151 static void
152 gst_rtph263penc_init (GstRtpH263PEnc * rtph263penc)
153 {
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);
158
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);
164
165   rtph263penc->adapter = gst_adapter_new ();
166   rtph263penc->mtu = DEFAULT_MTU;
167 }
168
169 static GstFlowReturn
170 gst_rtph263penc_flush (GstRtpH263PEnc * rtph263penc)
171 {
172   guint avail;
173   GstBuffer *outbuf;
174   GstFlowReturn ret;
175   gboolean fragmented;
176
177   avail = gst_adapter_available (rtph263penc->adapter);
178   if (avail == 0)
179     return GST_FLOW_OK;
180
181   fragmented = FALSE;
182
183   while (avail > 0) {
184     guint towrite;
185     guint8 *payload;
186     guint8 *data;
187     guint payload_len;
188     gint header_len;
189
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
195      * header */
196     header_len = (fragmented ? 2 : 0);
197     payload_len = header_len + towrite;
198
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);
207
208     payload = gst_rtpbuffer_get_payload (outbuf);
209
210     data = (guint8 *) gst_adapter_peek (rtph263penc->adapter, towrite);
211     memcpy (&payload[header_len], data, towrite);
212
213     /*  0                   1
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      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218      */
219     payload[0] = fragmented ? 0x00 : 0x04;
220     payload[1] = 0;
221
222     GST_BUFFER_TIMESTAMP (outbuf) = rtph263penc->first_ts;
223     gst_adapter_flush (rtph263penc->adapter, towrite);
224
225     ret = gst_pad_push (rtph263penc->srcpad, outbuf);
226
227     avail -= towrite;
228     fragmented = TRUE;
229   }
230
231   return ret;
232 }
233
234 static GstFlowReturn
235 gst_rtph263penc_chain (GstPad * pad, GstBuffer * buffer)
236 {
237   GstRtpH263PEnc *rtph263penc;
238   GstFlowReturn ret;
239   guint size;
240
241   rtph263penc = GST_RTP_H263P_ENC (GST_OBJECT_PARENT (pad));
242
243   size = GST_BUFFER_SIZE (buffer);
244   rtph263penc->first_ts = GST_BUFFER_TIMESTAMP (buffer);
245
246   /* we always encode and flush a full picture */
247   gst_adapter_push (rtph263penc->adapter, buffer);
248   ret = gst_rtph263penc_flush (rtph263penc);
249
250   return ret;
251 }
252
253 static void
254 gst_rtph263penc_set_property (GObject * object, guint prop_id,
255     const GValue * value, GParamSpec * pspec)
256 {
257   GstRtpH263PEnc *rtph263penc;
258
259   rtph263penc = GST_RTP_H263P_ENC (object);
260
261   switch (prop_id) {
262     case PROP_MTU:
263       rtph263penc->mtu = g_value_get_uint (value);
264       break;
265     case PROP_PT:
266       rtph263penc->pt = g_value_get_uint (value);
267       break;
268     case PROP_SSRC:
269       rtph263penc->ssrc = g_value_get_uint (value);
270       break;
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274   }
275 }
276
277 static void
278 gst_rtph263penc_get_property (GObject * object, guint prop_id, GValue * value,
279     GParamSpec * pspec)
280 {
281   GstRtpH263PEnc *rtph263penc;
282
283   rtph263penc = GST_RTP_H263P_ENC (object);
284
285   switch (prop_id) {
286     case PROP_MTU:
287       g_value_set_uint (value, rtph263penc->mtu);
288       break;
289     case PROP_PT:
290       g_value_set_uint (value, rtph263penc->pt);
291       break;
292     case PROP_SSRC:
293       g_value_set_uint (value, rtph263penc->ssrc);
294       break;
295     default:
296       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297       break;
298   }
299 }
300
301 static GstStateChangeReturn
302 gst_rtph263penc_change_state (GstElement * element, GstStateChange transition)
303 {
304   GstRtpH263PEnc *rtph263penc;
305   GstStateChangeReturn ret;
306
307   rtph263penc = GST_RTP_H263P_ENC (element);
308
309   switch (transition) {
310     case GST_STATE_CHANGE_NULL_TO_READY:
311       break;
312     default:
313       break;
314   }
315
316   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
317
318   switch (transition) {
319     case GST_STATE_CHANGE_READY_TO_NULL:
320       break;
321     default:
322       break;
323   }
324   return ret;
325 }
326
327 gboolean
328 gst_rtph263penc_plugin_init (GstPlugin * plugin)
329 {
330   return gst_element_register (plugin, "rtph263penc",
331       GST_RANK_NONE, GST_TYPE_RTP_H263P_ENC);
332 }