documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / gst / pcapparse / gstirtspparse.c
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
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
118 gst_irtsp_parse_reset (GstIRTSPParse * IRTSPParse)
119 {
120   IRTSPParse->state = IRTSP_SEARCH_FRAME;
121   IRTSPParse->current_offset = 0;
122   IRTSPParse->discont = FALSE;
123 }
124
125 static void
126 gst_irtsp_parse_init (GstIRTSPParse * IRTSPParse)
127 {
128   gst_base_parse_set_min_frame_size (GST_BASE_PARSE (IRTSPParse), 1);
129   gst_irtsp_parse_reset (IRTSPParse);
130 }
131
132 static void
133 gst_irtsp_parse_finalize (GObject * object)
134 {
135   G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137
138 static gboolean
139 gst_irtsp_parse_start (GstBaseParse * parse)
140 {
141   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (parse);
142
143   GST_DEBUG_OBJECT (parse, "starting");
144
145   gst_irtsp_parse_reset (IRTSPParse);
146
147   return TRUE;
148 }
149
150 static gboolean
151 gst_irtsp_parse_stop (GstBaseParse * parse)
152 {
153   GST_DEBUG_OBJECT (parse, "stopping");
154
155   return TRUE;
156 }
157
158 static void
159 gst_irtsp_set_caps_once (GstBaseParse * parse)
160 {
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);
165   }
166 }
167
168 static GstFlowReturn
169 gst_irtsp_parse_handle_frame (GstBaseParse * parse,
170     GstBaseParseFrame * frame, gint * skipsize)
171 {
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;
176   GstMapInfo map;
177   const guint8 *frame_start;
178   guint8 current_channel_id;
179   const guint8 *data;
180   guint data_size;
181   guint flushed_size;
182
183   if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (frame->buffer,
184               GST_BUFFER_FLAG_DISCONT))) {
185     IRTSPParse->discont = TRUE;
186   }
187
188   gst_buffer_map (buf, &map, GST_MAP_READ);
189
190 start:
191   g_assert (map.size >= IRTSPParse->current_offset);
192   data = &map.data[IRTSPParse->current_offset];
193   data_size = map.size - IRTSPParse->current_offset;
194
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);
202       if (frame_start) {
203         IRTSPParse->state = IRTSP_PARSE_FRAME;
204         IRTSPParse->current_offset += frame_start - data;
205         goto start;
206       } else {
207         IRTSPParse->current_offset += data_size;
208       }
209       break;
210     case IRTSP_PARSE_FRAME:
211       if (data_size > 0 && data[0] != frame_header_magic) {
212         IRTSPParse->state = IRTSP_SEARCH_FRAME;
213         goto start;
214       }
215
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;
222         } else {
223           IRTSPParse->state = IRTSP_FLUSH_FRAME;
224         }
225         goto start;
226       }
227       break;
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;
232         goto start;
233       }
234       break;
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);
239
240         frame->out_buffer = gst_buffer_copy (frame->buffer);
241         gst_buffer_resize (frame->out_buffer, IRTSPParse->current_offset,
242             IRTSPParse->frame_size);
243
244         if (G_UNLIKELY (IRTSPParse->discont)) {
245           GST_BUFFER_FLAG_SET (frame->out_buffer, GST_BUFFER_FLAG_DISCONT);
246           IRTSPParse->discont = FALSE;
247         }
248
249         flushed_size = IRTSPParse->current_offset + IRTSPParse->frame_size;
250         IRTSPParse->current_offset = 0;
251         IRTSPParse->state = IRTSP_PARSE_FRAME;
252
253         return gst_base_parse_finish_frame (parse, frame, flushed_size);
254       }
255
256       break;
257     default:
258       g_assert_not_reached ();
259       break;
260   }
261
262   gst_buffer_unmap (buf, &map);
263   return GST_FLOW_OK;
264 }
265
266 static void
267 gst_irtsp_parse_set_property (GObject * object,
268     guint prop_id, const GValue * value, GParamSpec * pspec)
269 {
270   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
271
272   switch (prop_id) {
273     case PROP_CHANNEL_ID:
274       IRTSPParse->target_channel_id = g_value_get_int (value);
275       break;
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279   }
280 }
281
282 static void
283 gst_irtsp_parse_get_property (GObject * object,
284     guint prop_id, GValue * value, GParamSpec * pspec)
285 {
286   GstIRTSPParse *IRTSPParse = GST_IRTSP_PARSE (object);
287
288   switch (prop_id) {
289     case PROP_CHANNEL_ID:
290       g_value_set_int (value, IRTSPParse->target_channel_id);
291       break;
292     default:
293       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294       break;
295   }
296 }