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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/* ASF parser plugin for GStreamer
* Copyright (C) 2009 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
*
* 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 <string.h>
#include "gstasfparse.h"
/* FIXME add this include
* #include <gst/gst-i18n-plugin.h> */
GST_DEBUG_CATEGORY_STATIC (asfparse_debug);
#define GST_CAT_DEFAULT asfparse_debug
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-ms-asf, parsed = (boolean) true")
);
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/x-ms-asf, parsed = (boolean) false")
);
static GstStateChangeReturn gst_asf_parse_change_state (GstElement * element,
GstStateChange transition);
static void gst_asf_parse_loop (GstPad * pad);
GST_BOILERPLATE (GstAsfParse, gst_asf_parse, GstElement, GST_TYPE_ELEMENT);
static void
gst_asf_parse_reset (GstAsfParse * asfparse)
{
gst_adapter_clear (asfparse->adapter);
gst_asf_file_info_reset (asfparse->asfinfo);
asfparse->parse_state = ASF_PARSING_HEADERS;
asfparse->headers_size = 0;
asfparse->data_size = 0;
asfparse->parsed_packets = 0;
asfparse->offset = 0;
}
static gboolean
gst_asf_parse_sink_activate (GstPad * pad)
{
if (gst_pad_check_pull_range (pad)) {
return gst_pad_activate_pull (pad, TRUE);
} else {
return gst_pad_activate_push (pad, TRUE);
}
}
static gboolean
gst_asf_parse_sink_activate_pull (GstPad * pad, gboolean active)
{
if (active) {
return gst_pad_start_task (pad, (GstTaskFunction) gst_asf_parse_loop, pad);
} else {
return gst_pad_stop_task (pad);
}
}
static GstFlowReturn
gst_asf_parse_push (GstAsfParse * asfparse, GstBuffer * buf)
{
gst_buffer_set_caps (buf, asfparse->outcaps);
return gst_pad_push (asfparse->srcpad, buf);
}
static GstFlowReturn
gst_asf_parse_parse_data_object (GstAsfParse * asfparse, GstBuffer * buffer)
{
GstByteReader *reader;
GstFlowReturn ret = GST_FLOW_OK;
guint64 packet_count = 0;
GST_DEBUG_OBJECT (asfparse, "Parsing data object");
reader = gst_byte_reader_new_from_buffer (buffer);
/* skip to packet count */
if (!gst_byte_reader_skip (reader, 40))
goto error;
if (!gst_byte_reader_get_uint64_le (reader, &packet_count))
goto error;
if (asfparse->asfinfo->packets_count != packet_count) {
GST_WARNING_OBJECT (asfparse, "File properties object and data object have "
"different packets count, %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT,
asfparse->asfinfo->packets_count, packet_count);
} else {
GST_DEBUG_OBJECT (asfparse, "Total packets: %" G_GUINT64_FORMAT,
packet_count);
}
gst_byte_reader_free (reader);
return gst_asf_parse_push (asfparse, buffer);
error:
ret = GST_FLOW_ERROR;
GST_ERROR_OBJECT (asfparse, "Error while parsing data object headers");
gst_byte_reader_free (reader);
return ret;
}
static GstFlowReturn
gst_asf_parse_parse_packet (GstAsfParse * asfparse, GstBuffer * buffer)
{
GstAsfPacketInfo *packetinfo = asfparse->packetinfo;
if (!gst_asf_parse_packet (buffer, packetinfo, FALSE,
asfparse->asfinfo->packet_size))
goto error;
GST_DEBUG_OBJECT (asfparse, "Received packet of length %" G_GUINT32_FORMAT
", padding %" G_GUINT32_FORMAT ", send time %" G_GUINT32_FORMAT
", duration %" G_GUINT16_FORMAT " and %s keyframe(s)",
packetinfo->packet_size, packetinfo->padding,
packetinfo->send_time, packetinfo->duration,
(packetinfo->has_keyframe) ? "with" : "without");
/* set gstbuffer fields */
if (!packetinfo->has_keyframe) {
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
}
GST_BUFFER_TIMESTAMP (buffer) = ((GstClockTime) packetinfo->send_time)
* GST_MSECOND;
GST_BUFFER_DURATION (buffer) = ((GstClockTime) packetinfo->duration)
* GST_MSECOND;
return gst_asf_parse_push (asfparse, buffer);
error:
GST_ERROR_OBJECT (asfparse, "Error while parsing data packet");
return GST_FLOW_ERROR;
}
static GstFlowReturn
gst_asf_parse_pull_headers (GstAsfParse * asfparse)
{
GstBuffer *guid_and_size = NULL;
GstBuffer *headers = NULL;
guint64 size;
GstFlowReturn ret;
if ((ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset,
ASF_GUID_OBJSIZE_SIZE, &guid_and_size)) != GST_FLOW_OK) {
GST_ERROR_OBJECT (asfparse, "Failed to pull data from headers");
goto leave;
}
asfparse->offset += ASF_GUID_OBJSIZE_SIZE;
size = gst_asf_match_and_peek_obj_size (GST_BUFFER_DATA (guid_and_size),
&(guids[ASF_HEADER_OBJECT_INDEX]));
if (size == 0) {
GST_ERROR_OBJECT (asfparse, "ASF starting identifier missing");
goto leave;
}
if ((ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset,
size - ASF_GUID_OBJSIZE_SIZE, &headers)) != GST_FLOW_OK) {
GST_ERROR_OBJECT (asfparse, "Failed to pull data from headers");
goto leave;
}
headers = gst_buffer_join (guid_and_size, headers);
guid_and_size = NULL;
asfparse->offset += size - ASF_GUID_OBJSIZE_SIZE;
if (!gst_asf_parse_headers (headers, asfparse->asfinfo)) {
goto leave;
}
return gst_asf_parse_push (asfparse, headers);
leave:
if (headers)
gst_buffer_unref (headers);
if (guid_and_size)
gst_buffer_unref (guid_and_size);
return ret;
}
static GstFlowReturn
gst_asf_parse_pull_data_header (GstAsfParse * asfparse)
{
GstBuffer *buf = NULL;
GstFlowReturn ret;
if ((ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset,
ASF_DATA_OBJECT_SIZE, &buf)) != GST_FLOW_OK) {
GST_ERROR_OBJECT (asfparse, "Failed to pull data header");
return ret;
}
asfparse->offset += ASF_DATA_OBJECT_SIZE;
asfparse->data_size = gst_asf_match_and_peek_obj_size (GST_BUFFER_DATA (buf),
&(guids[ASF_DATA_OBJECT_INDEX]));
if (asfparse->data_size == 0) {
GST_ERROR_OBJECT (asfparse, "Unexpected object, was expecting data object");
gst_buffer_unref (buf);
return GST_FLOW_ERROR;
}
return gst_asf_parse_parse_data_object (asfparse, buf);
}
static GstFlowReturn
gst_asf_parse_pull_packets (GstAsfParse * asfparse)
{
GstFlowReturn ret;
while (asfparse->asfinfo->broadcast ||
asfparse->parsed_packets < asfparse->asfinfo->packets_count) {
GstBuffer *packet = NULL;
GST_DEBUG_OBJECT (asfparse, "Parsing packet %" G_GUINT64_FORMAT,
asfparse->parsed_packets);
/* get the packet */
ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset,
asfparse->asfinfo->packet_size, &packet);
if (ret != GST_FLOW_OK)
return ret;
asfparse->parsed_packets++;
asfparse->offset += asfparse->asfinfo->packet_size;
/* parse the packet */
ret = gst_asf_parse_parse_packet (asfparse, packet);
if (ret != GST_FLOW_OK)
return ret;
}
return GST_FLOW_OK;
}
static GstFlowReturn
gst_asf_parse_pull_indexes (GstAsfParse * asfparse)
{
GstBuffer *guid_and_size = NULL;
GstBuffer *buf = NULL;
guint64 obj_size;
GstFlowReturn ret = GST_FLOW_OK;
while (1) {
ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset,
ASF_GUID_OBJSIZE_SIZE, &guid_and_size);
if (ret != GST_FLOW_OK)
break;
/* we can peek at the object size */
obj_size =
gst_asf_match_and_peek_obj_size (GST_BUFFER_DATA (guid_and_size), NULL);
if (obj_size == 0) {
GST_ERROR_OBJECT (asfparse, "Incomplete object found");
gst_buffer_unref (guid_and_size);
ret = GST_FLOW_ERROR;
break;
}
asfparse->offset += ASF_GUID_OBJSIZE_SIZE;
/* pull the rest of the object */
ret = gst_pad_pull_range (asfparse->sinkpad, asfparse->offset, obj_size,
&buf);
if (ret != GST_FLOW_OK) {
gst_buffer_unref (guid_and_size);
break;
}
asfparse->offset += obj_size - ASF_GUID_OBJSIZE_SIZE;
buf = gst_buffer_join (guid_and_size, buf);
ret = gst_asf_parse_push (asfparse, buf);
if (ret != GST_FLOW_OK)
break;
}
return ret;
}
static void
gst_asf_parse_loop (GstPad * pad)
{
GstFlowReturn ret = GST_FLOW_OK;
GstAsfParse *asfparse = GST_ASF_PARSE_CAST (GST_OBJECT_PARENT (pad));
GST_LOG_OBJECT (asfparse, "Processing data in loop function");
switch (asfparse->parse_state) {
case ASF_PARSING_HEADERS:
GST_INFO_OBJECT (asfparse, "Starting to parse headers");
ret = gst_asf_parse_pull_headers (asfparse);
if (ret != GST_FLOW_OK)
goto pause;
asfparse->parse_state = ASF_PARSING_DATA;
case ASF_PARSING_DATA:
GST_INFO_OBJECT (asfparse, "Parsing data object headers");
ret = gst_asf_parse_pull_data_header (asfparse);
if (ret != GST_FLOW_OK)
goto pause;
asfparse->parse_state = ASF_PARSING_PACKETS;
case ASF_PARSING_PACKETS:
GST_INFO_OBJECT (asfparse, "Starting packet parsing");
GST_INFO_OBJECT (asfparse, "Broadcast mode %s",
asfparse->asfinfo->broadcast ? "on" : "off");
ret = gst_asf_parse_pull_packets (asfparse);
if (ret != GST_FLOW_OK)
goto pause;
/* test if all packets have been processed */
if (!asfparse->asfinfo->broadcast &&
asfparse->parsed_packets == asfparse->asfinfo->packets_count) {
GST_INFO_OBJECT (asfparse,
"All %" G_GUINT64_FORMAT " packets processed",
asfparse->parsed_packets);
asfparse->parse_state = ASF_PARSING_INDEXES;
}
case ASF_PARSING_INDEXES:
/* we currently don't care about indexes, so just push them forward */
GST_INFO_OBJECT (asfparse, "Starting indexes parsing");
ret = gst_asf_parse_pull_indexes (asfparse);
if (ret != GST_FLOW_OK)
goto pause;
default:
break;
}
pause:
{
const gchar *reason = gst_flow_get_name (ret);
GST_INFO_OBJECT (asfparse, "Pausing sinkpad task");
gst_pad_pause_task (pad);
if (ret == GST_FLOW_UNEXPECTED) {
gst_pad_push_event (asfparse->srcpad, gst_event_eos_new ());
} else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
GST_ELEMENT_ERROR (asfparse, STREAM, FAILED,
(NULL), ("streaming task paused, reason %s (%d)", reason, ret));
gst_pad_push_event (asfparse->srcpad, gst_event_eos_new ());
}
}
}
static GstFlowReturn
gst_asf_parse_chain (GstPad * pad, GstBuffer * buffer)
{
GstAsfParse *asfparse;
GstFlowReturn ret = GST_FLOW_OK;
asfparse = GST_ASF_PARSE (GST_PAD_PARENT (pad));
gst_adapter_push (asfparse->adapter, buffer);
switch (asfparse->parse_state) {
case ASF_PARSING_HEADERS:
if (asfparse->headers_size == 0 &&
gst_adapter_available (asfparse->adapter) >= ASF_GUID_OBJSIZE_SIZE) {
/* we can peek at the object size */
asfparse->headers_size =
gst_asf_match_and_peek_obj_size (gst_adapter_peek
(asfparse->adapter, ASF_GUID_OBJSIZE_SIZE),
&(guids[ASF_HEADER_OBJECT_INDEX]));
if (asfparse->headers_size == 0) {
/* something is wrong, this probably ain't an ASF stream */
GST_ERROR_OBJECT (asfparse, "ASF starting identifier missing");
ret = GST_FLOW_ERROR;
goto end;
}
}
if (gst_adapter_available (asfparse->adapter) >= asfparse->headers_size) {
GstBuffer *headers = gst_adapter_take_buffer (asfparse->adapter,
asfparse->headers_size);
if (gst_asf_parse_headers (headers, asfparse->asfinfo)) {
ret = gst_asf_parse_push (asfparse, headers);
asfparse->parse_state = ASF_PARSING_DATA;
} else {
ret = GST_FLOW_ERROR;
GST_ERROR_OBJECT (asfparse, "Failed to parse headers");
}
}
break;
case ASF_PARSING_DATA:
if (asfparse->data_size == 0 &&
gst_adapter_available (asfparse->adapter) >= ASF_GUID_OBJSIZE_SIZE) {
/* we can peek at the object size */
asfparse->data_size =
gst_asf_match_and_peek_obj_size (gst_adapter_peek
(asfparse->adapter, ASF_GUID_OBJSIZE_SIZE),
&(guids[ASF_DATA_OBJECT_INDEX]));
if (asfparse->data_size == 0) {
/* something is wrong */
GST_ERROR_OBJECT (asfparse, "Unexpected object after headers, was "
"expecting a data object");
ret = GST_FLOW_ERROR;
goto end;
}
}
/* if we have received the full data object headers */
if (gst_adapter_available (asfparse->adapter) >= ASF_DATA_OBJECT_SIZE) {
ret = gst_asf_parse_parse_data_object (asfparse,
gst_adapter_take_buffer (asfparse->adapter, ASF_DATA_OBJECT_SIZE));
if (ret != GST_FLOW_OK) {
goto end;
}
asfparse->parse_state = ASF_PARSING_PACKETS;
}
break;
case ASF_PARSING_PACKETS:
g_assert (asfparse->asfinfo->packet_size);
while ((asfparse->asfinfo->broadcast ||
asfparse->parsed_packets < asfparse->asfinfo->packets_count) &&
gst_adapter_available (asfparse->adapter) >=
asfparse->asfinfo->packet_size) {
GstBuffer *packet = gst_adapter_take_buffer (asfparse->adapter,
asfparse->asfinfo->packet_size);
asfparse->parsed_packets++;
ret = gst_asf_parse_parse_packet (asfparse, packet);
if (ret != GST_FLOW_OK)
goto end;
}
if (!asfparse->asfinfo->broadcast &&
asfparse->parsed_packets >= asfparse->asfinfo->packets_count) {
GST_INFO_OBJECT (asfparse, "Finished parsing packets");
asfparse->parse_state = ASF_PARSING_INDEXES;
}
break;
case ASF_PARSING_INDEXES:
/* we currently don't care about any of those objects */
if (gst_adapter_available (asfparse->adapter) >= ASF_GUID_OBJSIZE_SIZE) {
guint64 obj_size;
/* we can peek at the object size */
obj_size = gst_asf_match_and_peek_obj_size (gst_adapter_peek
(asfparse->adapter, ASF_GUID_OBJSIZE_SIZE), NULL);
if (gst_adapter_available (asfparse->adapter) >= obj_size) {
GST_DEBUG_OBJECT (asfparse, "Skiping object");
ret = gst_asf_parse_push (asfparse,
gst_adapter_take_buffer (asfparse->adapter, obj_size));
if (ret != GST_FLOW_OK) {
goto end;
}
}
}
break;
default:
break;
}
end:
return ret;
}
static void
gst_asf_parse_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_factory));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_factory));
gst_element_class_set_details_simple (element_class, "ASF parser",
"Parser", "Parses ASF", "Thiago Santos <thiagoss@embedded.ufcg.edu.br>");
GST_DEBUG_CATEGORY_INIT (asfparse_debug, "asfparse", 0,
"Parser for ASF streams");
}
static void
gst_asf_parse_finalize (GObject * object)
{
GstAsfParse *asfparse = GST_ASF_PARSE (object);
gst_adapter_clear (asfparse->adapter);
g_object_unref (G_OBJECT (asfparse->adapter));
gst_caps_unref (asfparse->outcaps);
gst_asf_file_info_free (asfparse->asfinfo);
g_free (asfparse->packetinfo);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gst_asf_parse_class_init (GstAsfParseClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = gst_asf_parse_finalize;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_asf_parse_change_state);
}
static void
gst_asf_parse_init (GstAsfParse * asfparse, GstAsfParseClass * klass)
{
asfparse->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
gst_pad_set_chain_function (asfparse->sinkpad, gst_asf_parse_chain);
gst_pad_set_activate_function (asfparse->sinkpad,
gst_asf_parse_sink_activate);
gst_pad_set_activatepull_function (asfparse->sinkpad,
gst_asf_parse_sink_activate_pull);
gst_element_add_pad (GST_ELEMENT (asfparse), asfparse->sinkpad);
asfparse->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
gst_pad_use_fixed_caps (asfparse->srcpad);
gst_element_add_pad (GST_ELEMENT (asfparse), asfparse->srcpad);
asfparse->adapter = gst_adapter_new ();
asfparse->outcaps = gst_caps_new_simple ("video/x-ms-asf", NULL);
asfparse->asfinfo = gst_asf_file_info_new ();
asfparse->packetinfo = g_new0 (GstAsfPacketInfo, 1);
gst_asf_parse_reset (asfparse);
}
static GstStateChangeReturn
gst_asf_parse_change_state (GstElement * element, GstStateChange transition)
{
GstAsfParse *asfparse;
GstStateChangeReturn ret;
asfparse = GST_ASF_PARSE (element);
switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_asf_parse_reset (asfparse);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
goto done;
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
done:
return ret;
}
gboolean
gst_asf_parse_plugin_init (GstPlugin * plugin)
{
return gst_element_register (plugin, "asfparse",
GST_RANK_NONE, GST_TYPE_ASF_PARSE);
}
|