1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
*
* gstmultipartdemux.c: multipart stream demuxer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include <string.h>
GST_DEBUG_CATEGORY_STATIC (gst_multipart_demux_debug);
#define GST_CAT_DEFAULT gst_multipart_demux_debug
#define GST_TYPE_MULTIPART_DEMUX (gst_multipart_demux_get_type())
#define GST_MULTIPART_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTIPART_DEMUX, GstMultipartDemux))
#define GST_MULTIPART_DEMUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MULTIPART_DEMUX, GstMultipartDemux))
#define GST_IS_MULTIPART_DEMUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTIPART_DEMUX))
#define GST_IS_MULTIPART_DEMUX_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MULTIPART_DEMUX))
#define MAX_LINE_LEN 500
typedef struct _GstMultipartDemux GstMultipartDemux;
typedef struct _GstMultipartDemuxClass GstMultipartDemuxClass;
static gchar *toFind = "--ThisRandomString\nContent-type: ";
static gint toFindLen;
/* all information needed for one multipart stream */
typedef struct
{
GstPad *pad; /* reference for this pad is held by element we belong to */
gchar *mime;
guint64 offset; /* end offset of last buffer */
guint64 known_offset; /* last known offset */
guint flags;
}
GstMultipartPad;
struct _GstMultipartDemux
{
GstElement element;
/* pad */
GstPad *sinkpad;
GSList *srcpads;
gint numpads;
gchar *parsing_mime;
gchar *buffer;
gint maxlen;
gint bufsize;
gint scanpos;
gint lastpos;
};
struct _GstMultipartDemuxClass
{
GstElementClass parent_class;
};
/* elementfactory information */
static GstElementDetails gst_multipart_demux_details =
GST_ELEMENT_DETAILS ("multipart demuxer",
"Codec/Demuxer",
"demux multipart streams",
"Wim Taymans <wim@fluendo.com>");
/* signals and args */
enum
{
/* FILL ME */
LAST_SIGNAL
};
enum
{
ARG_0,
/* FILL ME */
};
static GstStaticPadTemplate multipart_demux_src_template_factory =
GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
static GstStaticPadTemplate multipart_demux_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("multipart/x-mixed-replace")
);
static void gst_multipart_demux_finalize (GObject * object);
static void gst_multipart_demux_chain (GstPad * pad, GstData * buffer);
static GstElementStateReturn gst_multipart_demux_change_state (GstElement *
element);
GST_BOILERPLATE (GstMultipartDemux, gst_multipart_demux, GstElement,
GST_TYPE_ELEMENT)
static void gst_multipart_demux_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_set_details (element_class, &gst_multipart_demux_details);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&multipart_demux_sink_template_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&multipart_demux_src_template_factory));
toFindLen = strlen (toFind);
}
static void
gst_multipart_demux_class_init (GstMultipartDemuxClass * klass)
{
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gstelement_class->change_state = gst_multipart_demux_change_state;
gobject_class->finalize = gst_multipart_demux_finalize;
}
static void
gst_multipart_demux_init (GstMultipartDemux * multipart,
GstMultipartDemuxClass * g_class)
{
/* create the sink pad */
multipart->sinkpad =
gst_pad_new_from_template (gst_static_pad_template_get
(&multipart_demux_sink_template_factory), "sink");
gst_element_add_pad (GST_ELEMENT (multipart), multipart->sinkpad);
gst_pad_set_chain_function (multipart->sinkpad,
GST_DEBUG_FUNCPTR (gst_multipart_demux_chain));
GST_FLAG_SET (multipart, GST_ELEMENT_EVENT_AWARE);
multipart->maxlen = 4096;
multipart->parsing_mime = NULL;
multipart->numpads = 0;
multipart->scanpos = 0;
multipart->lastpos = 0;
}
static void
gst_multipart_demux_finalize (GObject * object)
{
GstMultipartDemux *multipart;
multipart = GST_MULTIPART_DEMUX (object);
}
static void
gst_multipart_demux_handle_event (GstPad * pad, GstEvent * event)
{
//GstMultipartDemux *multipart = GST_MULTIPART_DEMUX (gst_pad_get_parent (pad));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_DISCONTINUOUS:
case GST_EVENT_EOS:
default:
gst_pad_event_default (pad, event);
break;
}
return;
}
static GstMultipartPad *
gst_multipart_find_pad_by_mime (GstMultipartDemux * demux, gchar * mime)
{
GSList *walk;
walk = demux->srcpads;
while (walk) {
GstMultipartPad *pad = (GstMultipartPad *) walk->data;
if (!strcmp (pad->mime, mime)) {
return pad;
}
walk = walk->next;
}
// pad not found, create it
{
GstPad *pad;
GstMultipartPad *mppad;
gchar *name;
GstCaps *caps;
mppad = g_new0 (GstMultipartPad, 1);
name = g_strdup_printf ("src_%d", demux->numpads);
pad = gst_pad_new_from_template (gst_static_pad_template_get
(&multipart_demux_src_template_factory), name);
g_free (name);
caps = gst_caps_from_string (mime);
gst_pad_use_explicit_caps (pad);
gst_pad_set_explicit_caps (pad, caps);
mppad->pad = pad;
mppad->mime = g_strdup (mime);
demux->srcpads = g_slist_prepend (demux->srcpads, mppad);
demux->numpads++;
gst_element_add_pad (GST_ELEMENT (demux), pad);
return mppad;
}
}
static void
gst_multipart_demux_chain (GstPad * pad, GstData * buffer)
{
GstMultipartDemux *multipart;
gint size;
gchar *data;
gint matchpos;
/* handle events */
if (GST_IS_EVENT (buffer)) {
gst_multipart_demux_handle_event (pad, GST_EVENT (buffer));
return;
}
multipart = GST_MULTIPART_DEMUX (gst_pad_get_parent (pad));
data = GST_BUFFER_DATA (buffer);
size = GST_BUFFER_SIZE (buffer);
// first make sure our buffer is long enough
if (multipart->bufsize + size > multipart->maxlen) {
gint newsize = (multipart->bufsize + size) * 2;
multipart->buffer = g_realloc (multipart->buffer, newsize);
multipart->maxlen = newsize;
}
// copy bytes into the buffer
memcpy (multipart->buffer + multipart->bufsize, data, size);
multipart->bufsize += size;
// find \n
while (multipart->scanpos < multipart->bufsize) {
if (multipart->buffer[multipart->scanpos] == '\n') {
break;
}
multipart->scanpos++;
}
// then scan for the boundary
for (matchpos = 0;
multipart->scanpos + toFindLen + MAX_LINE_LEN - matchpos <
multipart->bufsize; multipart->scanpos++) {
if (multipart->buffer[multipart->scanpos] == toFind[matchpos]) {
matchpos++;
if (matchpos == toFindLen) {
int datalen;
int i, start;
gchar *mime_type;
multipart->scanpos++;
start = multipart->scanpos;
// find \n
for (i = 0; i < MAX_LINE_LEN; i++) {
if (multipart->buffer[multipart->scanpos] == '\n')
break;
multipart->scanpos++;
matchpos++;
}
mime_type =
g_strndup (multipart->buffer + start, multipart->scanpos - start);
multipart->scanpos += 2;
matchpos += 3;
datalen = multipart->scanpos - matchpos;
if (datalen > 0 && multipart->parsing_mime) {
GstBuffer *outbuf;
GstMultipartPad *srcpad;
srcpad =
gst_multipart_find_pad_by_mime (multipart,
multipart->parsing_mime);
if (srcpad != NULL) {
outbuf = gst_buffer_new_and_alloc (datalen);
memcpy (GST_BUFFER_DATA (outbuf), multipart->buffer, datalen);
GST_BUFFER_TIMESTAMP (outbuf) = 0;
gst_pad_push (srcpad->pad, GST_DATA (outbuf));
}
}
// move rest downward
multipart->bufsize -= multipart->scanpos;
memmove (multipart->buffer, multipart->buffer + multipart->scanpos,
multipart->bufsize);
g_free (multipart->parsing_mime);
multipart->parsing_mime = mime_type;
multipart->scanpos = 0;
}
} else {
matchpos = 0;
}
}
gst_buffer_unref (buffer);
}
static GstElementStateReturn
gst_multipart_demux_change_state (GstElement * element)
{
GstMultipartDemux *multipart;
multipart = GST_MULTIPART_DEMUX (element);
switch (GST_STATE_TRANSITION (element)) {
case GST_STATE_NULL_TO_READY:
break;
case GST_STATE_READY_TO_PAUSED:
multipart->buffer = g_malloc (multipart->maxlen);
break;
case GST_STATE_PAUSED_TO_PLAYING:
break;
case GST_STATE_PLAYING_TO_PAUSED:
break;
case GST_STATE_PAUSED_TO_READY:
g_free (multipart->parsing_mime);
multipart->parsing_mime = NULL;
g_free (multipart->buffer);
multipart->buffer = NULL;
break;
case GST_STATE_READY_TO_NULL:
break;
default:
break;
}
return parent_class->change_state (element);
}
gboolean
gst_multipart_demux_plugin_init (GstPlugin * plugin, GstPluginClass * g_class)
{
GST_DEBUG_CATEGORY_INIT (gst_multipart_demux_debug,
"multipartdemux", 0, "multipart demuxer");
return gst_element_register (plugin, "multipartdemux", GST_RANK_PRIMARY,
GST_TYPE_MULTIPART_DEMUX);
}
|