2 * Copyright (C) 2013 Sebastian Dröge <sebastian@centricular.com>
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.
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.
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.
21 * SECTION:element-rtpstreamdepay
23 * Implements stream depayloading of RTP and RTCP packets for connection-oriented
24 * transport protocols according to RFC4571.
26 * <title>Example launch line</title>
28 * gst-launch-1.0 audiotestsrc ! "audio/x-raw,rate=48000" ! vorbisenc ! rtpvorbispay config-interval=1 ! rtpstreampay ! tcpserversink port=5678
29 * gst-launch-1.0 tcpclientsrc port=5678 host=127.0.0.1 do-timestamp=true ! "application/x-rtp-stream,media=audio,clock-rate=48000,encoding-name=VORBIS" ! rtpstreamdepay ! rtpvorbisdepay ! decodebin ! audioconvert ! audioresample ! autoaudiosink
38 #include "gstrtpstreamdepay.h"
40 GST_DEBUG_CATEGORY (gst_rtp_stream_depay_debug);
41 #define GST_CAT_DEFAULT gst_rtp_stream_depay_debug
43 static GstStaticPadTemplate src_template =
44 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
46 GST_STATIC_CAPS ("application/x-rtp; application/x-rtcp;"
47 "application/x-srtp; application/x-srtcp")
50 static GstStaticPadTemplate sink_template =
51 GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK,
53 GST_STATIC_CAPS ("application/x-rtp-stream; application/x-rtcp-stream;"
54 "application/x-srtp-stream; application/x-srtcp-stream")
57 #define parent_class gst_rtp_stream_depay_parent_class
58 G_DEFINE_TYPE (GstRtpStreamDepay, gst_rtp_stream_depay, GST_TYPE_BASE_PARSE);
60 static gboolean gst_rtp_stream_depay_set_sink_caps (GstBaseParse * parse,
62 static GstCaps *gst_rtp_stream_depay_get_sink_caps (GstBaseParse * parse,
64 static GstFlowReturn gst_rtp_stream_depay_handle_frame (GstBaseParse * parse,
65 GstBaseParseFrame * frame, gint * skipsize);
68 gst_rtp_stream_depay_class_init (GstRtpStreamDepayClass * klass)
70 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
71 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
73 GST_DEBUG_CATEGORY_INIT (gst_rtp_stream_depay_debug, "rtpstreamdepay", 0,
74 "RTP stream depayloader");
76 gst_element_class_add_pad_template (gstelement_class,
77 gst_static_pad_template_get (&src_template));
78 gst_element_class_add_pad_template (gstelement_class,
79 gst_static_pad_template_get (&sink_template));
81 gst_element_class_set_static_metadata (gstelement_class,
82 "RTP Stream Depayloading", "Codec/Depayloader/Network",
83 "Depayloads RTP/RTCP packets for streaming protocols according to RFC4571",
84 "Sebastian Dröge <sebastian@centricular.com>");
86 parse_class->set_sink_caps =
87 GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_set_sink_caps);
88 parse_class->get_sink_caps =
89 GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_get_sink_caps);
90 parse_class->handle_frame =
91 GST_DEBUG_FUNCPTR (gst_rtp_stream_depay_handle_frame);
95 gst_rtp_stream_depay_init (GstRtpStreamDepay * self)
97 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (self), 2);
101 gst_rtp_stream_depay_set_sink_caps (GstBaseParse * parse, GstCaps * caps)
104 GstStructure *structure;
107 othercaps = gst_caps_copy (caps);
108 structure = gst_caps_get_structure (othercaps, 0);
110 if (gst_structure_has_name (structure, "application/x-rtp-stream"))
111 gst_structure_set_name (structure, "application/x-rtp");
112 else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
113 gst_structure_set_name (structure, "application/x-rtcp");
114 else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
115 gst_structure_set_name (structure, "application/x-srtp");
117 gst_structure_set_name (structure, "application/x-srtcp");
119 ret = gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), othercaps);
120 gst_caps_unref (othercaps);
126 gst_rtp_stream_depay_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
128 GstCaps *peerfilter = NULL, *peercaps, *templ;
130 GstStructure *structure;
134 peerfilter = gst_caps_copy (filter);
135 n = gst_caps_get_size (peerfilter);
136 for (i = 0; i < n; i++) {
137 structure = gst_caps_get_structure (peerfilter, i);
139 if (gst_structure_has_name (structure, "application/x-rtp-stream"))
140 gst_structure_set_name (structure, "application/x-rtp");
141 else if (gst_structure_has_name (structure, "application/x-rtcp-stream"))
142 gst_structure_set_name (structure, "application/x-rtcp");
143 else if (gst_structure_has_name (structure, "application/x-srtp-stream"))
144 gst_structure_set_name (structure, "application/x-srtp");
146 gst_structure_set_name (structure, "application/x-srtcp");
150 templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
152 gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), peerfilter);
155 /* Rename structure names */
156 peercaps = gst_caps_make_writable (peercaps);
157 n = gst_caps_get_size (peercaps);
158 for (i = 0; i < n; i++) {
159 structure = gst_caps_get_structure (peercaps, i);
161 if (gst_structure_has_name (structure, "application/x-rtp"))
162 gst_structure_set_name (structure, "application/x-rtp-stream");
163 else if (gst_structure_has_name (structure, "application/x-rtcp"))
164 gst_structure_set_name (structure, "application/x-rtcp-stream");
165 else if (gst_structure_has_name (structure, "application/x-srtp"))
166 gst_structure_set_name (structure, "application/x-srtp-stream");
168 gst_structure_set_name (structure, "application/x-srtcp-stream");
171 res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
172 gst_caps_unref (peercaps);
178 GstCaps *intersection;
181 gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
182 gst_caps_unref (res);
185 gst_caps_unref (peerfilter);
192 gst_rtp_stream_depay_handle_frame (GstBaseParse * parse,
193 GstBaseParseFrame * frame, gint * skipsize)
198 if (gst_buffer_extract (frame->buffer, 0, &size, 2) != 2)
199 return GST_FLOW_ERROR;
201 size = GUINT16_FROM_BE (size);
202 buf_size = gst_buffer_get_size (frame->buffer);
205 if (size + 2 > buf_size)
209 gst_buffer_copy_region (frame->buffer, GST_BUFFER_COPY_ALL, 2, size);
211 return gst_base_parse_finish_frame (parse, frame, size + 2);
215 gst_rtp_stream_depay_plugin_init (GstPlugin * plugin)
217 return gst_element_register (plugin, "rtpstreamdepay",
218 GST_RANK_NONE, GST_TYPE_RTP_STREAM_DEPAY);