rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vdepay.c
1 /* GStreamer
2  * Copyright (C) <2005> 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., 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 <gst/rtp/gstrtpbuffer.h>
25
26 #include <string.h>
27 #include "gstrtpmp4vdepay.h"
28
29 GST_DEBUG_CATEGORY_STATIC (rtpmp4vdepay_debug);
30 #define GST_CAT_DEFAULT (rtpmp4vdepay_debug)
31
32 static GstStaticPadTemplate gst_rtp_mp4v_depay_src_template =
33 GST_STATIC_PAD_TEMPLATE ("src",
34     GST_PAD_SRC,
35     GST_PAD_ALWAYS,
36     GST_STATIC_CAPS ("video/mpeg,"
37         "mpegversion=(int) 4," "systemstream=(boolean)false")
38     );
39
40 static GstStaticPadTemplate gst_rtp_mp4v_depay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("application/x-rtp, "
45         "media = (string) \"video\", "
46         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
47         /* All optional parameters
48          *
49          * "profile-level-id=[1,MAX]"
50          * "config=" 
51          */
52     )
53     );
54
55 #define gst_rtp_mp4v_depay_parent_class parent_class
56 G_DEFINE_TYPE (GstRtpMP4VDepay, gst_rtp_mp4v_depay,
57     GST_TYPE_RTP_BASE_DEPAYLOAD);
58
59 static void gst_rtp_mp4v_depay_finalize (GObject * object);
60
61 static gboolean gst_rtp_mp4v_depay_setcaps (GstRTPBaseDepayload * depayload,
62     GstCaps * caps);
63 static GstBuffer *gst_rtp_mp4v_depay_process (GstRTPBaseDepayload * depayload,
64     GstBuffer * buf);
65
66 static GstStateChangeReturn gst_rtp_mp4v_depay_change_state (GstElement *
67     element, GstStateChange transition);
68
69 static void
70 gst_rtp_mp4v_depay_class_init (GstRtpMP4VDepayClass * klass)
71 {
72   GObjectClass *gobject_class;
73   GstElementClass *gstelement_class;
74   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
75
76   gobject_class = (GObjectClass *) klass;
77   gstelement_class = (GstElementClass *) klass;
78   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
79
80   gobject_class->finalize = gst_rtp_mp4v_depay_finalize;
81
82   gstelement_class->change_state = gst_rtp_mp4v_depay_change_state;
83
84   gstrtpbasedepayload_class->process = gst_rtp_mp4v_depay_process;
85   gstrtpbasedepayload_class->set_caps = gst_rtp_mp4v_depay_setcaps;
86
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_mp4v_depay_src_template));
89   gst_element_class_add_pad_template (gstelement_class,
90       gst_static_pad_template_get (&gst_rtp_mp4v_depay_sink_template));
91
92   gst_element_class_set_static_metadata (gstelement_class,
93       "RTP MPEG4 video depayloader", "Codec/Depayloader/Network/RTP",
94       "Extracts MPEG4 video from RTP packets (RFC 3016)",
95       "Wim Taymans <wim.taymans@gmail.com>");
96
97   GST_DEBUG_CATEGORY_INIT (rtpmp4vdepay_debug, "rtpmp4vdepay", 0,
98       "MPEG4 video RTP Depayloader");
99 }
100
101 static void
102 gst_rtp_mp4v_depay_init (GstRtpMP4VDepay * rtpmp4vdepay)
103 {
104   rtpmp4vdepay->adapter = gst_adapter_new ();
105 }
106
107 static void
108 gst_rtp_mp4v_depay_finalize (GObject * object)
109 {
110   GstRtpMP4VDepay *rtpmp4vdepay;
111
112   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (object);
113
114   g_object_unref (rtpmp4vdepay->adapter);
115   rtpmp4vdepay->adapter = NULL;
116
117   G_OBJECT_CLASS (parent_class)->finalize (object);
118 }
119
120 static gboolean
121 gst_rtp_mp4v_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
122 {
123   GstStructure *structure;
124   GstCaps *srccaps;
125   const gchar *str;
126   gint clock_rate;
127   gboolean res;
128
129   structure = gst_caps_get_structure (caps, 0);
130
131   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
132     clock_rate = 90000;         /* default */
133   depayload->clock_rate = clock_rate;
134
135   srccaps = gst_caps_new_simple ("video/mpeg",
136       "mpegversion", G_TYPE_INT, 4,
137       "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
138
139   if ((str = gst_structure_get_string (structure, "config"))) {
140     GValue v = { 0 };
141
142     g_value_init (&v, GST_TYPE_BUFFER);
143     if (gst_value_deserialize (&v, str)) {
144       GstBuffer *buffer;
145
146       buffer = gst_value_get_buffer (&v);
147       gst_caps_set_simple (srccaps,
148           "codec_data", GST_TYPE_BUFFER, buffer, NULL);
149       /* caps takes ref */
150       g_value_unset (&v);
151     } else {
152       g_warning ("cannot convert config to buffer");
153     }
154   }
155   res = gst_pad_set_caps (depayload->srcpad, srccaps);
156   gst_caps_unref (srccaps);
157
158   return res;
159 }
160
161 static GstBuffer *
162 gst_rtp_mp4v_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
163 {
164   GstRtpMP4VDepay *rtpmp4vdepay;
165   GstBuffer *pbuf, *outbuf = NULL;
166   GstRTPBuffer rtp = { NULL };
167   gboolean marker;
168
169   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (depayload);
170
171   /* flush remaining data on discont */
172   if (GST_BUFFER_IS_DISCONT (buf))
173     gst_adapter_clear (rtpmp4vdepay->adapter);
174
175   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
176   pbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
177   marker = gst_rtp_buffer_get_marker (&rtp);
178   gst_rtp_buffer_unmap (&rtp);
179
180   gst_adapter_push (rtpmp4vdepay->adapter, pbuf);
181
182   /* if this was the last packet of the VOP, create and push a buffer */
183   if (marker) {
184     guint avail;
185
186     avail = gst_adapter_available (rtpmp4vdepay->adapter);
187     outbuf = gst_adapter_take_buffer (rtpmp4vdepay->adapter, avail);
188
189     GST_DEBUG ("gst_rtp_mp4v_depay_chain: pushing buffer of size %"
190         G_GSIZE_FORMAT, gst_buffer_get_size (outbuf));
191   }
192   return outbuf;
193 }
194
195 static GstStateChangeReturn
196 gst_rtp_mp4v_depay_change_state (GstElement * element,
197     GstStateChange transition)
198 {
199   GstRtpMP4VDepay *rtpmp4vdepay;
200   GstStateChangeReturn ret;
201
202   rtpmp4vdepay = GST_RTP_MP4V_DEPAY (element);
203
204   switch (transition) {
205     case GST_STATE_CHANGE_READY_TO_PAUSED:
206       gst_adapter_clear (rtpmp4vdepay->adapter);
207       break;
208     default:
209       break;
210   }
211
212   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
213
214   switch (transition) {
215     default:
216       break;
217   }
218   return ret;
219 }
220
221 gboolean
222 gst_rtp_mp4v_depay_plugin_init (GstPlugin * plugin)
223 {
224   return gst_element_register (plugin, "rtpmp4vdepay",
225       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_DEPAY);
226 }