1 /* GStreamer Interleaved RTSP parser
2 * Copyright (C) 2011 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
3 * Copyright (C) 2011 Nokia Corporation. All rights reserved.
4 * Contact: Stefan Kost <stefan.kost@nokia.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 * SECTION:element-irtspparse
24 * @short_description: Interleaved RTSP parser
25 * @see_also: #GstPcapParse
27 * This is an interleaved RTSP parser that allows extracting specific
28 * so-called "channels" from received interleaved (TCP) RTSP data
29 * (typically extracted from some network capture).
31 * ## Example launch line
33 * gst-launch-1.0 filesrc location=h264crasher.pcap ! pcapparse ! irtspparse
34 * ! rtph264depay ! ffdec_h264 ! fakesink
35 * ]| Read from a pcap dump file using filesrc, extract the raw TCP packets,
36 * depayload and decode them.
46 #include "gstirtspparse.h"
47 #include <gst/base/gstbytereader.h>
49 GST_DEBUG_CATEGORY_STATIC (irtsp_parse_debug);
50 #define GST_CAT_DEFAULT irtsp_parse_debug
59 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-rtp ; application/x-rtcp"));
64 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
69 static void gst_irtsp_parse_finalize (GObject * object);
71 static gboolean gst_irtsp_parse_start (GstBaseParse * parse);
72 static gboolean gst_irtsp_parse_stop (GstBaseParse * parse);
73 static GstFlowReturn gst_irtsp_parse_handle_frame (GstBaseParse * parse,
74 GstBaseParseFrame * frame, gint * skipsize);
76 static void gst_irtsp_parse_set_property (GObject * object,
77 guint prop_id, const GValue * value, GParamSpec * pspec);
78 static void gst_irtsp_parse_get_property (GObject * object,
79 guint prop_id, GValue * value, GParamSpec * pspec);
81 #define parent_class gst_irtsp_parse_parent_class
82 G_DEFINE_TYPE (GstIRTSPParse, gst_irtsp_parse, GST_TYPE_BASE_PARSE);
85 gst_irtsp_parse_class_init (GstIRTSPParseClass * klass)
87 GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
88 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 GST_DEBUG_CATEGORY_INIT (irtsp_parse_debug, "irtspparse", 0,
92 "Interleaved RTSP stream parser");
94 object_class->finalize = gst_irtsp_parse_finalize;
96 object_class->set_property = gst_irtsp_parse_set_property;
97 object_class->get_property = gst_irtsp_parse_get_property;
99 g_object_class_install_property (object_class, PROP_CHANNEL_ID,
100 g_param_spec_int ("channel-id", "channel-id",
101 "Channel Identifier", 0, 255,
102 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 parse_class->start = GST_DEBUG_FUNCPTR (gst_irtsp_parse_start);
105 parse_class->stop = GST_DEBUG_FUNCPTR (gst_irtsp_parse_stop);
106 parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_irtsp_parse_handle_frame);
108 gst_element_class_add_static_pad_template (element_class, &sink_template);
109 gst_element_class_add_static_pad_template (element_class, &src_template);
111 gst_element_class_set_static_metadata (element_class, "IRTSPParse",
113 "Parses a raw interleaved RTSP stream",
114 "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
118 gst_irtsp_parse_reset (GstIRTSPParse * IRTSPParse)
120 IRTSPParse->state = IRTSP_SEARCH_FRAME;
121 IRTSPParse->current_offset = 0;
122 IRTSPParse->discont = FALSE;
126 gst_irtsp_parse_init (GstIRTSPParse * IRTSPParse)
128 gst_base_parse_set_min_frame_size (GST_BASE_PARSE (IRTSPParse), 1);
129 gst_irtsp_parse_reset (IRTSPParse);
133 gst_irtsp_parse_finalize (GObject * object)
135 G_OBJECT_CLASS (parent_class)->finalize (object);
139 gst_irtsp_parse_start (GstBaseParse * parse)
141 GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
143 GST_DEBUG_OBJECT (parse, "starting");
145 gst_irtsp_parse_reset (IRTSPParse);
151 gst_irtsp_parse_stop (GstBaseParse * parse)
153 GST_DEBUG_OBJECT (parse, "stopping");
159 gst_irtsp_set_caps_once (GstBaseParse * parse)
161 if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) {
162 GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtp");
163 gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
164 gst_caps_unref (caps);
169 gst_irtsp_parse_handle_frame (GstBaseParse * parse,
170 GstBaseParseFrame * frame, gint * skipsize)
172 static const guint frame_header_size = sizeof (guint8) * 4;
173 static const guint8 frame_header_magic = 0x24;
174 GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
175 GstBuffer *buf = frame->buffer;
177 const guint8 *frame_start;
178 guint8 current_channel_id;
183 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (frame->buffer,
184 GST_BUFFER_FLAG_DISCONT))) {
185 IRTSPParse->discont = TRUE;
188 gst_buffer_map (buf, &map, GST_MAP_READ);
191 g_assert (map.size >= IRTSPParse->current_offset);
192 data = &map.data[IRTSPParse->current_offset];
193 data_size = map.size - IRTSPParse->current_offset;
195 switch (IRTSPParse->state) {
196 case IRTSP_SEARCH_FRAME:
197 /* Use the first occurrence of 0x24 as a start of interleaved frames.
198 * This 'trick' allows us to parse a dump that doesn't contain RTSP
199 * handshake. It's up to user to provide the data where the first 0x24
200 * is an RTSP frame */
201 frame_start = memchr (data, frame_header_magic, data_size);
203 IRTSPParse->state = IRTSP_PARSE_FRAME;
204 IRTSPParse->current_offset += frame_start - data;
207 IRTSPParse->current_offset += data_size;
210 case IRTSP_PARSE_FRAME:
211 if (data_size > 0 && data[0] != frame_header_magic) {
212 IRTSPParse->state = IRTSP_SEARCH_FRAME;
216 if (data_size >= frame_header_size) {
217 IRTSPParse->current_offset += frame_header_size;
218 current_channel_id = data[1];
219 IRTSPParse->frame_size = GST_READ_UINT16_BE (&data[2]);
220 if (current_channel_id != IRTSPParse->target_channel_id) {
221 IRTSPParse->state = IRTSP_SKIP_FRAME;
223 IRTSPParse->state = IRTSP_FLUSH_FRAME;
228 case IRTSP_SKIP_FRAME:
229 if (data_size >= IRTSPParse->frame_size) {
230 IRTSPParse->current_offset += IRTSPParse->frame_size;
231 IRTSPParse->state = IRTSP_PARSE_FRAME;
235 case IRTSP_FLUSH_FRAME:
236 if (data_size >= IRTSPParse->frame_size) {
237 gst_irtsp_set_caps_once (parse);
238 gst_buffer_unmap (buf, &map);
240 frame->out_buffer = gst_buffer_copy (frame->buffer);
241 gst_buffer_resize (frame->out_buffer, IRTSPParse->current_offset,
242 IRTSPParse->frame_size);
244 if (G_UNLIKELY (IRTSPParse->discont)) {
245 GST_BUFFER_FLAG_SET (frame->out_buffer, GST_BUFFER_FLAG_DISCONT);
246 IRTSPParse->discont = FALSE;
249 flushed_size = IRTSPParse->current_offset + IRTSPParse->frame_size;
250 IRTSPParse->current_offset = 0;
251 IRTSPParse->state = IRTSP_PARSE_FRAME;
253 return gst_base_parse_finish_frame (parse, frame, flushed_size);
258 g_assert_not_reached ();
262 gst_buffer_unmap (buf, &map);
267 gst_irtsp_parse_set_property (GObject * object,
268 guint prop_id, const GValue * value, GParamSpec * pspec)
270 GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
273 case PROP_CHANNEL_ID:
274 IRTSPParse->target_channel_id = g_value_get_int (value);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gst_irtsp_parse_get_property (GObject * object,
284 guint prop_id, GValue * value, GParamSpec * pspec)
286 GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
289 case PROP_CHANNEL_ID:
290 g_value_set_int (value, IRTSPParse->target_channel_id);
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);