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