• 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 
84 static void
gst_irtsp_parse_class_init(GstIRTSPParseClass * klass)85 gst_irtsp_parse_class_init (GstIRTSPParseClass * klass)
86 {
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);
90 
91   GST_DEBUG_CATEGORY_INIT (irtsp_parse_debug, "irtspparse", 0,
92       "Interleaved RTSP stream parser");
93 
94   object_class->finalize = gst_irtsp_parse_finalize;
95 
96   object_class->set_property = gst_irtsp_parse_set_property;
97   object_class->get_property = gst_irtsp_parse_get_property;
98 
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));
103 
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);
107 
108   gst_element_class_add_static_pad_template (element_class, &sink_template);
109   gst_element_class_add_static_pad_template (element_class, &src_template);
110 
111   gst_element_class_set_static_metadata (element_class, "IRTSPParse",
112       "Raw/Parser",
113       "Parses a raw interleaved RTSP stream",
114       "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
115 }
116 
117 static void
gst_irtsp_parse_reset(GstIRTSPParse * IRTSPParse)118 gst_irtsp_parse_reset (GstIRTSPParse * IRTSPParse)
119 {
120 }
121 
122 static void
gst_irtsp_parse_init(GstIRTSPParse * IRTSPParse)123 gst_irtsp_parse_init (GstIRTSPParse * IRTSPParse)
124 {
125   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (IRTSPParse), 4);
126   gst_irtsp_parse_reset (IRTSPParse);
127 }
128 
129 static void
gst_irtsp_parse_finalize(GObject * object)130 gst_irtsp_parse_finalize (GObject * object)
131 {
132   G_OBJECT_CLASS (parent_class)->finalize (object);
133 }
134 
135 static gboolean
gst_irtsp_parse_start(GstBaseParse * parse)136 gst_irtsp_parse_start (GstBaseParse * parse)
137 {
138   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
139 
140   GST_DEBUG_OBJECT (parse, "starting");
141 
142   gst_irtsp_parse_reset (IRTSPParse);
143 
144   return TRUE;
145 }
146 
147 static gboolean
gst_irtsp_parse_stop(GstBaseParse * parse)148 gst_irtsp_parse_stop (GstBaseParse * parse)
149 {
150   GST_DEBUG_OBJECT (parse, "stopping");
151 
152   return TRUE;
153 }
154 
155 static GstFlowReturn
gst_irtsp_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)156 gst_irtsp_parse_handle_frame (GstBaseParse * parse,
157     GstBaseParseFrame * frame, gint * skipsize)
158 {
159   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
160   GstBuffer *buf = frame->buffer;
161   GstByteReader reader;
162   gint off;
163   GstMapInfo map;
164   guint framesize;
165 
166   gst_buffer_map (buf, &map, GST_MAP_READ);
167   if (G_UNLIKELY (map.size < 4))
168     goto exit;
169 
170   gst_byte_reader_init (&reader, map.data, map.size);
171 
172   off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffff0000,
173       0x24000000 + (IRTSPParse->channel_id << 16), 0, map.size);
174 
175   GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
176 
177   /* didn't find anything that looks like a sync word, skip */
178   if (off < 0) {
179     *skipsize = map.size - 3;
180     goto exit;
181   }
182 
183   /* possible frame header, but not at offset 0? skip bytes before sync */
184   if (off > 0) {
185     *skipsize = off;
186     goto exit;
187   }
188 
189   framesize = GST_READ_UINT16_BE (map.data + 2) + 4;
190   GST_LOG_OBJECT (parse, "got frame size %d", framesize);
191 
192   if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) {
193     GstCaps *caps;
194 
195     caps = gst_caps_new_empty_simple ("application/x-rtp");
196     gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
197     gst_caps_unref (caps);
198   }
199 
200   if (framesize <= map.size) {
201     gst_buffer_unmap (buf, &map);
202     /* HACK HACK skip header.
203      * could also ask baseparse to skip this,
204      * but that would give us a discontinuity for free
205      * which is a bit too much to have on all our packets */
206     frame->out_buffer = gst_buffer_copy (frame->buffer);
207     gst_buffer_resize (frame->out_buffer, 4, -1);
208     GST_BUFFER_FLAG_UNSET (frame->out_buffer, GST_BUFFER_FLAG_DISCONT);
209     return gst_base_parse_finish_frame (parse, frame, framesize);
210   }
211 
212 exit:
213   gst_buffer_unmap (buf, &map);
214   return GST_FLOW_OK;
215 }
216 
217 static void
gst_irtsp_parse_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)218 gst_irtsp_parse_set_property (GObject * object,
219     guint prop_id, const GValue * value, GParamSpec * pspec)
220 {
221   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
222 
223   switch (prop_id) {
224     case PROP_CHANNEL_ID:
225       IRTSPParse->channel_id = g_value_get_int (value);
226       break;
227     default:
228       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229       break;
230   }
231 }
232 
233 static void
gst_irtsp_parse_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)234 gst_irtsp_parse_get_property (GObject * object,
235     guint prop_id, GValue * value, GParamSpec * pspec)
236 {
237   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
238 
239   switch (prop_id) {
240     case PROP_CHANNEL_ID:
241       g_value_set_int (value, IRTSPParse->channel_id);
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246   }
247 }
248