Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmp2tpay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@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 "gstrtpmp2tpay.h"
29
30 /* elementfactory information */
31 static const GstElementDetails gst_rtp_mp2t_pay_details =
32 GST_ELEMENT_DETAILS ("RTP MPEG2 Transport Stream payloader",
33     "Codec/Payloader/Network",
34     "Payload-encodes MPEG2 TS into RTP packets (RFC 2250)",
35     "Wim Taymans <wim.taymans@gmail.com>");
36
37 static GstStaticPadTemplate gst_rtp_mp2t_pay_sink_template =
38 GST_STATIC_PAD_TEMPLATE ("sink",
39     GST_PAD_SINK,
40     GST_PAD_ALWAYS,
41     GST_STATIC_CAPS ("video/mpegts,"
42         "packetsize=(int)188," "systemstream=(boolean)true")
43     );
44
45 static GstStaticPadTemplate gst_rtp_mp2t_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"video\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) 90000, " "encoding-name = (string) \"MP2T-ES\"")
53     );
54
55 static gboolean gst_rtp_mp2t_pay_setcaps (GstBaseRTPPayload * payload,
56     GstCaps * caps);
57 static GstFlowReturn gst_rtp_mp2t_pay_handle_buffer (GstBaseRTPPayload *
58     payload, GstBuffer * buffer);
59 static GstFlowReturn gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay);
60 static void gst_rtp_mp2t_pay_finalize (GObject * object);
61
62 GST_BOILERPLATE (GstRTPMP2TPay, gst_rtp_mp2t_pay, GstBaseRTPPayload,
63     GST_TYPE_BASE_RTP_PAYLOAD);
64
65 static void
66 gst_rtp_mp2t_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_mp2t_pay_sink_template));
72   gst_element_class_add_pad_template (element_class,
73       gst_static_pad_template_get (&gst_rtp_mp2t_pay_src_template));
74   gst_element_class_set_details (element_class, &gst_rtp_mp2t_pay_details);
75 }
76
77 static void
78 gst_rtp_mp2t_pay_class_init (GstRTPMP2TPayClass * 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_mp2t_pay_finalize;
87
88   gstbasertppayload_class->set_caps = gst_rtp_mp2t_pay_setcaps;
89   gstbasertppayload_class->handle_buffer = gst_rtp_mp2t_pay_handle_buffer;
90 }
91
92 static void
93 gst_rtp_mp2t_pay_init (GstRTPMP2TPay * rtpmp2tpay, GstRTPMP2TPayClass * klass)
94 {
95   GST_BASE_RTP_PAYLOAD (rtpmp2tpay)->clock_rate = 90000;
96   GST_BASE_RTP_PAYLOAD_PT (rtpmp2tpay) = GST_RTP_PAYLOAD_MP2T;
97
98   rtpmp2tpay->adapter = gst_adapter_new ();
99 }
100
101 static void
102 gst_rtp_mp2t_pay_finalize (GObject * object)
103 {
104   GstRTPMP2TPay *rtpmp2tpay;
105
106   rtpmp2tpay = GST_RTP_MP2T_PAY (object);
107
108   g_object_unref (rtpmp2tpay->adapter);
109   rtpmp2tpay->adapter = NULL;
110
111   G_OBJECT_CLASS (parent_class)->finalize (object);
112 }
113
114 static gboolean
115 gst_rtp_mp2t_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
116 {
117   const char *stname;
118   GstStructure *structure;
119
120   structure = gst_caps_get_structure (caps, 0);
121
122   stname = gst_structure_get_name (structure);
123
124   gst_basertppayload_set_options (payload, "video", TRUE, "MP2T-ES", 90000);
125   gst_basertppayload_set_outcaps (payload, NULL);
126
127   return TRUE;
128 }
129
130 static GstFlowReturn
131 gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay)
132 {
133   guint avail;
134   guint8 *payload;
135   GstFlowReturn ret;
136   GstBuffer *outbuf;
137
138   avail = gst_adapter_available (rtpmp2tpay->adapter);
139   outbuf = gst_rtp_buffer_new_allocate (avail, 0, 0);
140
141   /* get payload */
142   payload = gst_rtp_buffer_get_payload (outbuf);
143
144   /* copy stuff from adapter to payload */
145   gst_adapter_copy (rtpmp2tpay->adapter, payload, 0, avail);
146
147   GST_BUFFER_TIMESTAMP (outbuf) = rtpmp2tpay->first_ts;
148   GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration;
149
150   GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %d",
151       GST_BUFFER_SIZE (outbuf));
152
153   ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmp2tpay), outbuf);
154
155   /* flush the adapter content */
156   gst_adapter_flush (rtpmp2tpay->adapter, avail);
157
158   return ret;
159 }
160
161 static GstFlowReturn
162 gst_rtp_mp2t_pay_handle_buffer (GstBaseRTPPayload * basepayload,
163     GstBuffer * buffer)
164 {
165   GstRTPMP2TPay *rtpmp2tpay;
166   guint size, avail, packet_len;
167   guint8 *data;
168   GstClockTime timestamp, duration;
169   GstFlowReturn ret;
170
171   rtpmp2tpay = GST_RTP_MP2T_PAY (basepayload);
172
173   size = GST_BUFFER_SIZE (buffer);
174   data = GST_BUFFER_DATA (buffer);
175   timestamp = GST_BUFFER_TIMESTAMP (buffer);
176   duration = GST_BUFFER_DURATION (buffer);
177
178   ret = GST_FLOW_OK;
179   avail = gst_adapter_available (rtpmp2tpay->adapter);
180
181   /* Initialize new RTP payload */
182   if (avail == 0) {
183     rtpmp2tpay->first_ts = timestamp;
184     rtpmp2tpay->duration = duration;
185   }
186
187   /* get packet length of previous data and this new data, 
188    * payload length includes a 4 byte header */
189   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
190
191   /* if this buffer is going to overflow the packet, flush what we
192    * have. */
193   if (gst_basertppayload_is_filled (basepayload,
194           packet_len, rtpmp2tpay->duration + duration)) {
195     ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay);
196     rtpmp2tpay->first_ts = timestamp;
197     rtpmp2tpay->duration = duration;
198
199     /* keep filling the payload */
200   } else {
201     if (GST_CLOCK_TIME_IS_VALID (duration))
202       rtpmp2tpay->duration += duration;
203   }
204
205   /* copy buffer to adapter */
206   gst_adapter_push (rtpmp2tpay->adapter, buffer);
207
208   return ret;
209
210 }
211
212 gboolean
213 gst_rtp_mp2t_pay_plugin_init (GstPlugin * plugin)
214 {
215   return gst_element_register (plugin, "rtpmp2tpay",
216       GST_RANK_NONE, GST_TYPE_RTP_MP2T_PAY);
217 }