Define GstElementDetails as const and also static (when defined as global)
[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 "gstrtph263ppay.h"
24
25 /* elementfactory information */
26 static const GstElementDetails gst_rtp_h263ppay_details =
27 GST_ELEMENT_DETAILS ("RTP packet parser",
28     "Codec/Payloader/Network",
29     "Payload-encodes H263+ video in RTP packets (RFC 2429)",
30     "Wim Taymans <wim@fluendo.com>");
31
32 static GstStaticPadTemplate gst_rtp_h263p_pay_sink_template =
33 GST_STATIC_PAD_TEMPLATE ("sink",
34     GST_PAD_SINK,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("video/x-h263")
37     );
38
39 static GstStaticPadTemplate gst_rtp_h263p_pay_src_template =
40 GST_STATIC_PAD_TEMPLATE ("src",
41     GST_PAD_SRC,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("application/x-rtp, "
44         "media = (string) \"video\", "
45         "payload = (int) [ 96, 127 ], "
46         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
47     );
48
49 static void gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass);
50 static void gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass);
51 static void gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay);
52 static void gst_rtp_h263p_pay_finalize (GObject * object);
53
54 static gboolean gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload,
55     GstCaps * caps);
56 static GstFlowReturn gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload *
57     payload, GstBuffer * buffer);
58
59 static GstBaseRTPPayloadClass *parent_class = NULL;
60
61 static GType
62 gst_rtp_h263p_pay_get_type (void)
63 {
64   static GType rtph263ppay_type = 0;
65
66   if (!rtph263ppay_type) {
67     static const GTypeInfo rtph263ppay_info = {
68       sizeof (GstRtpH263PPayClass),
69       (GBaseInitFunc) gst_rtp_h263p_pay_base_init,
70       NULL,
71       (GClassInitFunc) gst_rtp_h263p_pay_class_init,
72       NULL,
73       NULL,
74       sizeof (GstRtpH263PPay),
75       0,
76       (GInstanceInitFunc) gst_rtp_h263p_pay_init,
77     };
78
79     rtph263ppay_type =
80         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpH263PPay",
81         &rtph263ppay_info, 0);
82   }
83   return rtph263ppay_type;
84 }
85
86 static void
87 gst_rtp_h263p_pay_base_init (GstRtpH263PPayClass * klass)
88 {
89   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
90
91   gst_element_class_add_pad_template (element_class,
92       gst_static_pad_template_get (&gst_rtp_h263p_pay_src_template));
93   gst_element_class_add_pad_template (element_class,
94       gst_static_pad_template_get (&gst_rtp_h263p_pay_sink_template));
95
96   gst_element_class_set_details (element_class, &gst_rtp_h263ppay_details);
97 }
98
99 static void
100 gst_rtp_h263p_pay_class_init (GstRtpH263PPayClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104   GstBaseRTPPayloadClass *gstbasertppayload_class;
105
106   gobject_class = (GObjectClass *) klass;
107   gstelement_class = (GstElementClass *) klass;
108   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
109
110   parent_class = g_type_class_peek_parent (klass);
111
112   gobject_class->finalize = gst_rtp_h263p_pay_finalize;
113
114   gstbasertppayload_class->set_caps = gst_rtp_h263p_pay_setcaps;
115   gstbasertppayload_class->handle_buffer = gst_rtp_h263p_pay_handle_buffer;
116 }
117
118 static void
119 gst_rtp_h263p_pay_init (GstRtpH263PPay * rtph263ppay)
120 {
121   rtph263ppay->adapter = gst_adapter_new ();
122 }
123
124 static void
125 gst_rtp_h263p_pay_finalize (GObject * object)
126 {
127   GstRtpH263PPay *rtph263ppay;
128
129   rtph263ppay = GST_RTP_H263P_PAY (object);
130
131   g_object_unref (rtph263ppay->adapter);
132   rtph263ppay->adapter = NULL;
133
134   G_OBJECT_CLASS (parent_class)->finalize (object);
135 }
136
137 static gboolean
138 gst_rtp_h263p_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
139 {
140   gst_basertppayload_set_options (payload, "video", TRUE, "H263-1998", 90000);
141   gst_basertppayload_set_outcaps (payload, NULL);
142
143   return TRUE;
144 }
145
146
147 static GstFlowReturn
148 gst_rtp_h263p_pay_flush (GstRtpH263PPay * rtph263ppay)
149 {
150   guint avail;
151   GstBuffer *outbuf;
152   GstFlowReturn ret;
153   gboolean fragmented;
154
155   avail = gst_adapter_available (rtph263ppay->adapter);
156   if (avail == 0)
157     return GST_FLOW_OK;
158
159   fragmented = FALSE;
160
161   while (avail > 0) {
162     guint towrite;
163     guint8 *payload;
164     guint8 *data;
165     guint payload_len;
166     gint header_len;
167
168     /* FIXME, do better mtu packing, header len etc should be
169      * included in this calculation. */
170     towrite = MIN (avail, GST_BASE_RTP_PAYLOAD_MTU (rtph263ppay));
171     /* for fragmented frames we need 2 bytes header, for other
172      * frames we must reuse the first 2 bytes of the data as the
173      * header */
174     header_len = (fragmented ? 2 : 0);
175     payload_len = header_len + towrite;
176
177     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
178     /* last fragment gets the marker bit set */
179     gst_rtp_buffer_set_marker (outbuf, avail > towrite ? 0 : 1);
180
181     payload = gst_rtp_buffer_get_payload (outbuf);
182
183     data = (guint8 *) gst_adapter_peek (rtph263ppay->adapter, towrite);
184     memcpy (&payload[header_len], data, towrite);
185
186     /*  0                   1
187      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
188      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
189      * |   RR    |P|V|   PLEN    |PEBIT|
190      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
191      */
192     payload[0] = fragmented ? 0x00 : 0x04;
193     payload[1] = 0;
194
195     GST_BUFFER_TIMESTAMP (outbuf) = rtph263ppay->first_ts;
196     gst_adapter_flush (rtph263ppay->adapter, towrite);
197
198     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtph263ppay), outbuf);
199
200     avail -= towrite;
201     fragmented = TRUE;
202   }
203
204   return ret;
205 }
206
207 static GstFlowReturn
208 gst_rtp_h263p_pay_handle_buffer (GstBaseRTPPayload * payload,
209     GstBuffer * buffer)
210 {
211   GstRtpH263PPay *rtph263ppay;
212   GstFlowReturn ret;
213   guint size;
214
215   rtph263ppay = GST_RTP_H263P_PAY (payload);
216
217   size = GST_BUFFER_SIZE (buffer);
218   rtph263ppay->first_ts = GST_BUFFER_TIMESTAMP (buffer);
219
220   /* we always encode and flush a full picture */
221   gst_adapter_push (rtph263ppay->adapter, buffer);
222   ret = gst_rtp_h263p_pay_flush (rtph263ppay);
223
224   return ret;
225 }
226
227 gboolean
228 gst_rtp_h263p_pay_plugin_init (GstPlugin * plugin)
229 {
230   return gst_element_register (plugin, "rtph263ppay",
231       GST_RANK_NONE, GST_TYPE_RTP_H263P_PAY);
232 }