tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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 GST_DEBUG_CATEGORY_STATIC (rtpmpvpay_debug);
31 #define GST_CAT_DEFAULT (rtpmpvpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("video/mpeg, "
38         "mpegversion = (int) 2, systemstream = (boolean) FALSE")
39     );
40
41 static GstStaticPadTemplate gst_rtp_mpv_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"video\", "
47         "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
48         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
49     );
50
51 static GstStateChangeReturn gst_rtp_mpv_pay_change_state (GstElement * element,
52     GstStateChange transition);
53
54 static void gst_rtp_mpv_pay_finalize (GObject * object);
55
56 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay);
57 static gboolean gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload,
58     GstCaps * caps);
59 static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload *
60     payload, GstBuffer * buffer);
61 static gboolean gst_rtp_mpv_pay_handle_event (GstPad * pad, GstEvent * event);
62
63 GST_BOILERPLATE (GstRTPMPVPay, gst_rtp_mpv_pay, GstBaseRTPPayload,
64     GST_TYPE_BASE_RTP_PAYLOAD);
65
66 static void
67 gst_rtp_mpv_pay_base_init (gpointer klass)
68 {
69   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
70
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_mpv_pay_sink_template);
73   gst_element_class_add_static_pad_template (element_class,
74       &gst_rtp_mpv_pay_src_template);
75   gst_element_class_set_details_simple (element_class,
76       "RTP MPEG2 ES video payloader", "Codec/Payloader/Network/RTP",
77       "Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
78       "Thijs Vermeir <thijsvermeir@gmail.com>");
79 }
80
81 static void
82 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
83 {
84   GObjectClass *gobject_class;
85   GstElementClass *gstelement_class;
86   GstBaseRTPPayloadClass *gstbasertppayload_class;
87
88   gobject_class = (GObjectClass *) klass;
89   gstelement_class = (GstElementClass *) klass;
90   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
91
92   gobject_class->finalize = gst_rtp_mpv_pay_finalize;
93
94   gstelement_class->change_state = gst_rtp_mpv_pay_change_state;
95
96   gstbasertppayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
97   gstbasertppayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
98   gstbasertppayload_class->handle_event = gst_rtp_mpv_pay_handle_event;
99
100   GST_DEBUG_CATEGORY_INIT (rtpmpvpay_debug, "rtpmpvpay", 0,
101       "MPEG2 ES Video RTP Payloader");
102 }
103
104 static void
105 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay, GstRTPMPVPayClass * klass)
106 {
107   GST_BASE_RTP_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
108   GST_BASE_RTP_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
109
110   rtpmpvpay->adapter = gst_adapter_new ();
111 }
112
113 static void
114 gst_rtp_mpv_pay_finalize (GObject * object)
115 {
116   GstRTPMPVPay *rtpmpvpay;
117
118   rtpmpvpay = GST_RTP_MPV_PAY (object);
119
120   g_object_unref (rtpmpvpay->adapter);
121   rtpmpvpay->adapter = NULL;
122
123   G_OBJECT_CLASS (parent_class)->finalize (object);
124 }
125
126 static void
127 gst_rtp_mpv_pay_reset (GstRTPMPVPay * pay)
128 {
129   pay->first_ts = -1;
130   pay->duration = 0;
131   gst_adapter_clear (pay->adapter);
132   GST_DEBUG_OBJECT (pay, "reset depayloader");
133 }
134
135 static gboolean
136 gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
137 {
138   gst_basertppayload_set_options (payload, "video", FALSE, "MPV", 90000);
139   return gst_basertppayload_set_outcaps (payload, NULL);
140 }
141
142 static gboolean
143 gst_rtp_mpv_pay_handle_event (GstPad * pad, GstEvent * event)
144 {
145   GstRTPMPVPay *rtpmpvpay;
146
147   rtpmpvpay = GST_RTP_MPV_PAY (gst_pad_get_parent (pad));
148
149   switch (GST_EVENT_TYPE (event)) {
150     case GST_EVENT_EOS:
151       /* make sure we push the last packets in the adapter on EOS */
152       gst_rtp_mpv_pay_flush (rtpmpvpay);
153       break;
154     case GST_EVENT_FLUSH_STOP:
155       gst_rtp_mpv_pay_reset (rtpmpvpay);
156       break;
157     default:
158       break;
159   }
160
161   gst_object_unref (rtpmpvpay);
162
163   /* FALSE to let the parent handle the event as well */
164   return FALSE;
165 }
166
167 static GstFlowReturn
168 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay)
169 {
170   GstBuffer *outbuf;
171   GstFlowReturn ret;
172   guint avail;
173
174   guint8 *payload;
175
176   avail = gst_adapter_available (rtpmpvpay->adapter);
177
178   ret = GST_FLOW_OK;
179
180   while (avail > 0) {
181     guint towrite;
182     guint packet_len;
183     guint payload_len;
184
185     packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
186
187     towrite = MIN (packet_len, GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay));
188
189     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 4, 0);
190
191     outbuf = gst_rtp_buffer_new_allocate (payload_len, 4, 0);
192
193     payload = gst_rtp_buffer_get_payload (outbuf);
194     /* enable MPEG Video-specific header
195      *
196      *  0                   1                   2                   3
197      *  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
198      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
199      * |    MBZ  |T|         TR        | |N|S|B|E|  P  | | BFC | | FFC |
200      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201      *                                  AN              FBV     FFV
202      */
203
204     /* fill in the MPEG Video-specific header 
205      * data is set to 0x0 here
206      */
207     memset (payload, 0x0, 4);
208
209     gst_adapter_copy (rtpmpvpay->adapter, payload + 4, 0, payload_len);
210     gst_adapter_flush (rtpmpvpay->adapter, payload_len);
211
212     avail -= payload_len;
213
214     gst_rtp_buffer_set_marker (outbuf, avail == 0);
215
216     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpvpay->first_ts;
217
218     ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpvpay), outbuf);
219   }
220
221   return ret;
222 }
223
224 static GstFlowReturn
225 gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
226     GstBuffer * buffer)
227 {
228   GstRTPMPVPay *rtpmpvpay;
229   guint avail, packet_len;
230   GstClockTime timestamp, duration;
231   GstFlowReturn ret = GST_FLOW_OK;
232
233   rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
234
235   timestamp = GST_BUFFER_TIMESTAMP (buffer);
236   duration = GST_BUFFER_DURATION (buffer);
237
238   if (GST_BUFFER_IS_DISCONT (buffer)) {
239     GST_DEBUG_OBJECT (rtpmpvpay, "DISCONT");
240     gst_rtp_mpv_pay_reset (rtpmpvpay);
241   }
242
243   avail = gst_adapter_available (rtpmpvpay->adapter);
244
245   if (duration == -1)
246     duration = 0;
247
248   if (rtpmpvpay->first_ts == GST_CLOCK_TIME_NONE || avail == 0)
249     rtpmpvpay->first_ts = timestamp;
250
251   if (avail == 0) {
252     rtpmpvpay->duration = duration;
253   } else {
254     rtpmpvpay->duration += duration;
255   }
256
257   gst_adapter_push (rtpmpvpay->adapter, buffer);
258   avail = gst_adapter_available (rtpmpvpay->adapter);
259
260   /* get packet length of previous data and this new data,
261    * payload length includes a 4 byte MPEG video-specific header */
262   packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
263   GST_LOG_OBJECT (rtpmpvpay, "available %d, rtp packet length %d", avail,
264       packet_len);
265
266   if (gst_basertppayload_is_filled (basepayload,
267           packet_len, rtpmpvpay->duration)) {
268     ret = gst_rtp_mpv_pay_flush (rtpmpvpay);
269   } else {
270     rtpmpvpay->first_ts = timestamp;
271   }
272
273   return ret;
274 }
275
276 static GstStateChangeReturn
277 gst_rtp_mpv_pay_change_state (GstElement * element, GstStateChange transition)
278 {
279   GstRTPMPVPay *rtpmpvpay;
280   GstStateChangeReturn ret;
281
282   rtpmpvpay = GST_RTP_MPV_PAY (element);
283
284   switch (transition) {
285     case GST_STATE_CHANGE_READY_TO_PAUSED:
286       gst_rtp_mpv_pay_reset (rtpmpvpay);
287       break;
288     default:
289       break;
290   }
291
292   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
293
294   switch (transition) {
295     case GST_STATE_CHANGE_PAUSED_TO_READY:
296       gst_rtp_mpv_pay_reset (rtpmpvpay);
297       break;
298     default:
299       break;
300   }
301   return ret;
302 }
303
304
305 gboolean
306 gst_rtp_mpv_pay_plugin_init (GstPlugin * plugin)
307 {
308   return gst_element_register (plugin, "rtpmpvpay",
309       GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_PAY);
310 }