Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmpvpay.c
1 /* GStreamer
2  * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.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 details.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpmpvpay.h"
29
30 static const GstElementDetails gst_rtp_mpv_pay_details =
31 GST_ELEMENT_DETAILS ("RTP MPEG2 ES video payloader",
32     "Codec/Payloader/Network",
33     "Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
34     "Thijs Vermeir <thijsvermeir@gmail.com>");
35
36 static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("video/mpeg, "
41         "mpegversion = (int) 2, systemstream = (boolean) FALSE")
42     );
43
44 static GstStaticPadTemplate gst_rtp_mpv_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46     GST_PAD_SRC,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"video\", "
50         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
51         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
52     );
53
54 static gboolean gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload,
55     GstCaps * caps);
56 static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload *
57     payload, GstBuffer * buffer);
58 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay,
59     GstClockTime timestamp, GstClockTime duration);
60 static void gst_rtp_mpv_pay_finalize (GObject * object);
61
62 GST_BOILERPLATE (GstRTPMPVPay, gst_rtp_mpv_pay, GstBaseRTPPayload,
63     GST_TYPE_BASE_RTP_PAYLOAD);
64
65 static void
66 gst_rtp_mpv_pay_base_init (gpointer klass)
67 {
68   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
69
70   gst_element_class_add_pad_template (element_class,
71       gst_static_pad_template_get (&gst_rtp_mpv_pay_sink_template));
72   gst_element_class_add_pad_template (element_class,
73       gst_static_pad_template_get (&gst_rtp_mpv_pay_src_template));
74   gst_element_class_set_details (element_class, &gst_rtp_mpv_pay_details);
75 }
76
77 static void
78 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
79 {
80   GObjectClass *gobject_class;
81   GstBaseRTPPayloadClass *gstbasertppayload_class;
82
83   gobject_class = (GObjectClass *) klass;
84   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
85
86   gobject_class->finalize = gst_rtp_mpv_pay_finalize;
87
88   gstbasertppayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
89   gstbasertppayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
90 }
91
92 static void
93 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay, GstRTPMPVPayClass * klass)
94 {
95   GST_BASE_RTP_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
96   GST_BASE_RTP_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
97
98   rtpmpvpay->adapter = gst_adapter_new ();
99 }
100
101 static void
102 gst_rtp_mpv_pay_finalize (GObject * object)
103 {
104   GstRTPMPVPay *rtpmpvpay;
105
106   rtpmpvpay = GST_RTP_MPV_PAY (object);
107
108   g_object_unref (rtpmpvpay->adapter);
109   rtpmpvpay->adapter = NULL;
110
111   G_OBJECT_CLASS (parent_class)->finalize (object);
112 }
113
114 static gboolean
115 gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
116 {
117   const char *stname;
118   GstStructure *structure;
119   gboolean res;
120
121   structure = gst_caps_get_structure (caps, 0);
122
123   stname = gst_structure_get_name (structure);
124
125   gst_basertppayload_set_options (payload, "video", FALSE, "MPV", 90000);
126   res = gst_basertppayload_set_outcaps (payload, NULL);
127
128   return res;
129 }
130
131 static GstFlowReturn
132 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay, GstClockTime timestamp,
133     GstClockTime duration)
134 {
135   GstBuffer *outbuf;
136   GstFlowReturn ret;
137   guint avail;
138   guint8 *payload;
139   gint packet_size;
140   gint payload_size;
141
142   avail = gst_adapter_available (rtpmpvpay->adapter);
143   packet_size = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
144
145   /* check for the maximum size of the rtp buffer */
146   if (packet_size > GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay)) {
147     payload_size =
148         GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay) -
149         gst_rtp_buffer_calc_packet_len (4, 0, 0);
150   } else {
151     payload_size = avail;
152   }
153   outbuf = gst_rtp_buffer_new_allocate (4 + payload_size, 0, 0);
154   /* enable MPEG Video-specific header
155    *
156    *  0                   1                   2                   3
157    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
158    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159    * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
160    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
161    *                                  AN              FBV     FFV
162    */
163   payload = gst_rtp_buffer_get_payload (outbuf);
164   /* fill in the MPEG Video-specific header */
165   memset (payload, 0x0, 4);
166   /* copy stuff from adapter to payload */
167   gst_adapter_copy (rtpmpvpay->adapter, payload + 4, 0, payload_size);
168   GST_BUFFER_TIMESTAMP (outbuf) = rtpmpvpay->first_ts;
169   GST_BUFFER_DURATION (outbuf) = rtpmpvpay->duration;
170
171   GST_DEBUG_OBJECT (rtpmpvpay, "pushing buffer of size %d",
172       GST_BUFFER_SIZE (outbuf));
173   ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpvpay), outbuf);
174   gst_adapter_flush (rtpmpvpay->adapter, payload_size);
175
176   /* update the timestamp and duration */
177   rtpmpvpay->first_ts = timestamp;
178   rtpmpvpay->duration = duration;
179
180   /* check if there is enough data for another rtp buffer */
181   avail = gst_adapter_available (rtpmpvpay->adapter);
182   packet_size = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
183
184   if (packet_size >= GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay) && ret == GST_FLOW_OK) {
185     GST_DEBUG_OBJECT (rtpmpvpay, "Have enough data for another rtp packet");
186     ret = gst_rtp_mpv_pay_flush (rtpmpvpay, timestamp, duration);
187   }
188   return ret;
189 }
190
191 static GstFlowReturn
192 gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
193     GstBuffer * buffer)
194 {
195   GstRTPMPVPay *rtpmpvpay;
196   guint size, avail, packet_len;
197   guint8 *data;
198   GstClockTime timestamp, duration;
199   GstFlowReturn ret;
200
201   rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
202
203   size = GST_BUFFER_SIZE (buffer);
204   data = GST_BUFFER_DATA (buffer);
205   timestamp = GST_BUFFER_TIMESTAMP (buffer);
206   duration = GST_BUFFER_DURATION (buffer);
207
208   gst_adapter_push (rtpmpvpay->adapter, buffer);
209   avail = gst_adapter_available (rtpmpvpay->adapter);
210
211   /* Initialize new RTP payload */
212   if (avail == 0) {
213     rtpmpvpay->first_ts = timestamp;
214     rtpmpvpay->duration = duration;
215   }
216
217   /* get packet length of previous data and this new data,
218    * payload length includes a 4 byte MPEG video-specific header */
219   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
220
221   if (gst_basertppayload_is_filled (basepayload,
222           packet_len, rtpmpvpay->duration + duration)) {
223     ret = gst_rtp_mpv_pay_flush (rtpmpvpay, timestamp, duration);
224   } else {
225     if (GST_CLOCK_TIME_IS_VALID (duration))
226       rtpmpvpay->duration += duration;
227     ret = GST_FLOW_OK;
228   }
229   return ret;
230 }
231
232 gboolean
233 gst_rtp_mpv_pay_plugin_init (GstPlugin * plugin)
234 {
235   return gst_element_register (plugin, "rtpmpvpay",
236       GST_RANK_NONE, GST_TYPE_RTP_MPV_PAY);
237 }