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