• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
5  *
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.
10  *
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.
15  *
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.
20  */
21 /**
22  * SECTION:element-irtspparse
23  * @title: irtspparse
24  * @short_description: Interleaved RTSP parser
25  * @see_also: #GstPcapParse
26  *
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).
30  *
31  * ## Example launch line
32  * |[
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.
37  *
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 
44 #include <string.h>
45 
46 #include "gstirtspparse.h"
47 #include <gst/base/gstbytereader.h>
48 
49 GST_DEBUG_CATEGORY_STATIC (irtsp_parse_debug);
50 #define GST_CAT_DEFAULT irtsp_parse_debug
51 
52 enum
53 {
54   PROP_0,
55   PROP_CHANNEL_ID
56 };
57 
58 
59 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
60     GST_PAD_SRC,
61     GST_PAD_ALWAYS,
62     GST_STATIC_CAPS ("application/x-rtp ; application/x-rtcp"));
63 
64 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
65     GST_PAD_SINK,
66     GST_PAD_ALWAYS,
67     GST_STATIC_CAPS_ANY);
68 
69 static void gst_irtsp_parse_finalize (GObject * object);
70 
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);
75 
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);
80 
81 #define parent_class gst_irtsp_parse_parent_class
82 G_DEFINE_TYPE (GstIRTSPParse, gst_irtsp_parse, GST_TYPE_BASE_PARSE);
83 GST_ELEMENT_REGISTER_DEFINE (irtspparse, "irtspparse", GST_RANK_NONE,
84     GST_TYPE_IRTSP_PARSE);
85 
86 static void
gst_irtsp_parse_class_init(GstIRTSPParseClass * klass)87 gst_irtsp_parse_class_init (GstIRTSPParseClass * klass)
88 {
89   GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
90   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
91   GObjectClass *object_class = G_OBJECT_CLASS (klass);
92 
93   GST_DEBUG_CATEGORY_INIT (irtsp_parse_debug, "irtspparse", 0,
94       "Interleaved RTSP stream parser");
95 
96   object_class->finalize = gst_irtsp_parse_finalize;
97 
98   object_class->set_property = gst_irtsp_parse_set_property;
99   object_class->get_property = gst_irtsp_parse_get_property;
100 
101   g_object_class_install_property (object_class, PROP_CHANNEL_ID,
102       g_param_spec_int ("channel-id", "channel-id",
103           "Channel Identifier", 0, 255,
104           0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 
106   parse_class->start = GST_DEBUG_FUNCPTR (gst_irtsp_parse_start);
107   parse_class->stop = GST_DEBUG_FUNCPTR (gst_irtsp_parse_stop);
108   parse_class->handle_frame = GST_DEBUG_FUNCPTR (gst_irtsp_parse_handle_frame);
109 
110   gst_element_class_add_static_pad_template (element_class, &sink_template);
111   gst_element_class_add_static_pad_template (element_class, &src_template);
112 
113   gst_element_class_set_static_metadata (element_class, "IRTSPParse",
114       "Raw/Parser",
115       "Parses a raw interleaved RTSP stream",
116       "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
117 }
118 
119 static void
gst_irtsp_parse_reset(GstIRTSPParse * IRTSPParse)120 gst_irtsp_parse_reset (GstIRTSPParse * IRTSPParse)
121 {
122   IRTSPParse->state = IRTSP_SEARCH_FRAME;
123   IRTSPParse->current_offset = 0;
124   IRTSPParse->discont = FALSE;
125 }
126 
127 static void
gst_irtsp_parse_init(GstIRTSPParse * IRTSPParse)128 gst_irtsp_parse_init (GstIRTSPParse * IRTSPParse)
129 {
130   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (IRTSPParse), 1);
131   gst_irtsp_parse_reset (IRTSPParse);
132 }
133 
134 static void
gst_irtsp_parse_finalize(GObject * object)135 gst_irtsp_parse_finalize (GObject * object)
136 {
137   G_OBJECT_CLASS (parent_class)->finalize (object);
138 }
139 
140 static gboolean
gst_irtsp_parse_start(GstBaseParse * parse)141 gst_irtsp_parse_start (GstBaseParse * parse)
142 {
143   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
144 
145   GST_DEBUG_OBJECT (parse, "starting");
146 
147   gst_irtsp_parse_reset (IRTSPParse);
148 
149   return TRUE;
150 }
151 
152 static gboolean
gst_irtsp_parse_stop(GstBaseParse * parse)153 gst_irtsp_parse_stop (GstBaseParse * parse)
154 {
155   GST_DEBUG_OBJECT (parse, "stopping");
156 
157   return TRUE;
158 }
159 
160 static void
gst_irtsp_set_caps_once(GstBaseParse * parse)161 gst_irtsp_set_caps_once (GstBaseParse * parse)
162 {
163   if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) {
164     GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtp");
165     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
166     gst_caps_unref (caps);
167   }
168 }
169 
170 static GstFlowReturn
gst_irtsp_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)171 gst_irtsp_parse_handle_frame (GstBaseParse * parse,
172     GstBaseParseFrame * frame, gint * skipsize)
173 {
174   static const guint frame_header_size = sizeof (guint8) * 4;
175   static const guint8 frame_header_magic = 0x24;
176   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
177   GstBuffer *buf = frame->buffer;
178   GstMapInfo map;
179   const guint8 *frame_start;
180   guint8 current_channel_id;
181   const guint8 *data;
182   guint data_size;
183   guint flushed_size;
184 
185   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (frame->buffer,
186               GST_BUFFER_FLAG_DISCONT))) {
187     IRTSPParse->discont = TRUE;
188   }
189 
190   gst_buffer_map (buf, &map, GST_MAP_READ);
191 
192 start:
193   g_assert (map.size >= IRTSPParse->current_offset);
194   data = &map.data[IRTSPParse->current_offset];
195   data_size = map.size - IRTSPParse->current_offset;
196 
197   switch (IRTSPParse->state) {
198     case IRTSP_SEARCH_FRAME:
199       /* Use the first occurrence of 0x24 as a start of interleaved frames.
200        * This 'trick' allows us to parse a dump that doesn't contain RTSP
201        * handshake. It's up to user to provide the data where the first 0x24
202        * is an RTSP frame */
203       frame_start = memchr (data, frame_header_magic, data_size);
204       if (frame_start) {
205         IRTSPParse->state = IRTSP_PARSE_FRAME;
206         IRTSPParse->current_offset += frame_start - data;
207         goto start;
208       } else {
209         IRTSPParse->current_offset += data_size;
210       }
211       break;
212     case IRTSP_PARSE_FRAME:
213       if (data_size > 0 && data[0] != frame_header_magic) {
214         IRTSPParse->state = IRTSP_SEARCH_FRAME;
215         goto start;
216       }
217 
218       if (data_size >= frame_header_size) {
219         IRTSPParse->current_offset += frame_header_size;
220         current_channel_id = data[1];
221         IRTSPParse->frame_size = GST_READ_UINT16_BE (&data[2]);
222         if (current_channel_id != IRTSPParse->target_channel_id) {
223           IRTSPParse->state = IRTSP_SKIP_FRAME;
224         } else {
225           IRTSPParse->state = IRTSP_FLUSH_FRAME;
226         }
227         goto start;
228       }
229       break;
230     case IRTSP_SKIP_FRAME:
231       if (data_size >= IRTSPParse->frame_size) {
232         IRTSPParse->current_offset += IRTSPParse->frame_size;
233         IRTSPParse->state = IRTSP_PARSE_FRAME;
234         goto start;
235       }
236       break;
237     case IRTSP_FLUSH_FRAME:
238       if (data_size >= IRTSPParse->frame_size) {
239         gst_irtsp_set_caps_once (parse);
240         gst_buffer_unmap (buf, &map);
241 
242         frame->out_buffer = gst_buffer_copy (frame->buffer);
243         gst_buffer_resize (frame->out_buffer, IRTSPParse->current_offset,
244             IRTSPParse->frame_size);
245 
246         if (G_UNLIKELY (IRTSPParse->discont)) {
247           GST_BUFFER_FLAG_SET (frame->out_buffer, GST_BUFFER_FLAG_DISCONT);
248           IRTSPParse->discont = FALSE;
249         }
250 
251         flushed_size = IRTSPParse->current_offset + IRTSPParse->frame_size;
252         IRTSPParse->current_offset = 0;
253         IRTSPParse->state = IRTSP_PARSE_FRAME;
254 
255         return gst_base_parse_finish_frame (parse, frame, flushed_size);
256       }
257 
258       break;
259     default:
260       g_assert_not_reached ();
261       break;
262   }
263 
264   gst_buffer_unmap (buf, &map);
265   return GST_FLOW_OK;
266 }
267 
268 static void
gst_irtsp_parse_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)269 gst_irtsp_parse_set_property (GObject * object,
270     guint prop_id, const GValue * value, GParamSpec * pspec)
271 {
272   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
273 
274   switch (prop_id) {
275     case PROP_CHANNEL_ID:
276       IRTSPParse->target_channel_id = g_value_get_int (value);
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281   }
282 }
283 
284 static void
gst_irtsp_parse_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)285 gst_irtsp_parse_get_property (GObject * object,
286     guint prop_id, GValue * value, GParamSpec * pspec)
287 {
288   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
289 
290   switch (prop_id) {
291     case PROP_CHANNEL_ID:
292       g_value_set_int (value, IRTSPParse->target_channel_id);
293       break;
294     default:
295       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296       break;
297   }
298 }
299