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