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