File: producer_xml.c

package info (click to toggle)
mlt 0.8.0-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,100 kB
  • sloc: ansic: 51,947; cpp: 22,271; makefile: 1,401; sh: 1,267; asm: 311; ruby: 76; python: 50; perl: 34; java: 30; cs: 20; php: 18; tcl: 15
file content (1764 lines) | stat: -rw-r--r-- 57,383 bytes parent folder | download
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
/*
 * producer_xml.c -- a libxml2 parser of mlt service networks
 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
 * Author: Dan Dennedy <dan@dennedy.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 */

// TODO: destroy unreferenced producers (they are currently destroyed
//       when the returned producer is closed).

#include <framework/mlt.h>
#include <framework/mlt_log.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>

#include <libxml/parser.h>
#include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
#include <libxml/tree.h>

#define STACK_SIZE 1000
#define BRANCH_SIG_LEN 4000

#define _x (const xmlChar*)
#define _s (const char*)

#undef DEBUG
#ifdef DEBUG
extern xmlDocPtr xml_make_doc( mlt_service service );
#endif

enum service_type
{
	mlt_invalid_type,
	mlt_unknown_type,
	mlt_producer_type,
	mlt_playlist_type,
	mlt_entry_type,
	mlt_tractor_type,
	mlt_multitrack_type,
	mlt_filter_type,
	mlt_transition_type,
	mlt_consumer_type,
	mlt_field_type,
	mlt_services_type,
	mlt_dummy_filter_type,
	mlt_dummy_transition_type,
	mlt_dummy_producer_type,
	mlt_dummy_consumer_type
};

struct deserialise_context_s
{
	enum service_type stack_types[ STACK_SIZE ];
	mlt_service stack_service[ STACK_SIZE ];
	int stack_service_size;
	mlt_properties producer_map;
	mlt_properties destructors;
	char *property;
	int is_value;
	xmlDocPtr value_doc;
	xmlNodePtr stack_node[ STACK_SIZE ];
	int stack_node_size;
	xmlDocPtr entity_doc;
	int entity_is_replace;
	int depth;
	int branch[ STACK_SIZE ];
	const xmlChar *publicId;
	const xmlChar *systemId;
	mlt_properties params;
	mlt_profile profile;
	int pass;
	char *lc_numeric;
	mlt_consumer consumer;
	int multi_consumer;
	int consumer_count;
};
typedef struct deserialise_context_s *deserialise_context;

/** Trim the leading and trailing whitespace from a string in-place.
*/
static char* trim( char *s )
{
	int n;
	if ( s && ( n = strlen( s ) ) )
	{
		int i = 0;
		while ( i < n && isspace( s[i] ) ) i++;
		while ( --n && isspace( s[n] ) );
		n = n - i + 1;
		if ( n > 0 )
			memmove( s, s + i, n );
		s[ n ] = 0;
	}
	return s;
}

/** Convert the numerical current branch address to a dot-delimited string.
*/
static char *serialise_branch( deserialise_context context, char *s )
{
	int i;
	
	s[0] = 0;
	for ( i = 0; i < context->depth; i++ )
	{
		int len = strlen( s );
		snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
	}
	return s;
}

/** Push a service.
*/

static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
{
	int ret = context->stack_service_size >= STACK_SIZE - 1;
	if ( ret == 0 )
	{
		context->stack_service[ context->stack_service_size ] = that;
		context->stack_types[ context->stack_service_size++ ] = type;
		
		// Record the tree branch on which this service lives
		if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
		{
			char s[ BRANCH_SIG_LEN ];
			mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
		}
	}
	return ret;
}

/** Pop a service.
*/

static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
{
	mlt_service result = NULL;
	
	*type = invalid_type;
	if ( context->stack_service_size > 0 )
	{
		result = context->stack_service[ -- context->stack_service_size ];
		if ( type != NULL )
			*type = context->stack_types[ context->stack_service_size ];
		// Set the service's profile and locale so mlt_property time-to-position conversions can get fps
		mlt_properties_set_data( MLT_SERVICE_PROPERTIES( result ), "_profile", context->profile, 0, NULL, NULL );
		mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( result ), context->lc_numeric );
	}
	return result;
}

/** Push a node.
*/

static int context_push_node( deserialise_context context, xmlNodePtr node )
{
	int ret = context->stack_node_size >= STACK_SIZE - 1;
	if ( ret == 0 )
		context->stack_node[ context->stack_node_size ++ ] = node;
	return ret;
}

/** Pop a node.
*/

static xmlNodePtr context_pop_node( deserialise_context context )
{
	xmlNodePtr result = NULL;
	if ( context->stack_node_size > 0 )
		result = context->stack_node[ -- context->stack_node_size ];
	return result;
}


// Set the destructor on a new service
static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
{
	int registered = mlt_properties_get_int( properties, "registered" );
	char *key = mlt_properties_get( properties, "registered" );
	mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
	mlt_properties_set_int( properties, "registered", ++ registered );
}


// Prepend the property value with the document root
static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
{
	char *resource = mlt_properties_get( properties, name );
	if ( resource != NULL && resource[0] )
	{
		// Qualify file name properties	
		char *root = mlt_properties_get( context->producer_map, "root" );
		if ( root != NULL && strcmp( root, "" ) )
		{
			char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
			if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
			{
				strcpy( full_resource, root );
				strcat( full_resource, "/" );
				strcat( full_resource, resource );
			}
			else
			{
				strcpy( full_resource, resource );
			}
			mlt_properties_set( properties, name, full_resource );
			free( full_resource );
		}
	}
}


/** This function adds a producer to a playlist or multitrack when
    there is no entry or track element.
*/

static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
{
	// Return value (0 = service remains top of stack, 1 means it can be removed)
	int result = 0;

	// Get the parent producer
	enum service_type type = mlt_invalid_type;
	mlt_service container = context_pop_service( context, &type );
	int contained = 0;

	if ( service != NULL && container != NULL )
	{
		char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
		char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
		contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
	}

	if ( contained )
	{
		mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
		char *hide_s = mlt_properties_get( properties, "hide" );

		// Indicate that this service is no longer top of stack
		result = 1;

		switch( type )
		{
			case mlt_tractor_type: 
				{
					mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
					mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
				}
				break;
			case mlt_multitrack_type:
				{
					mlt_multitrack_connect( MLT_MULTITRACK( container ),
						MLT_PRODUCER( service ),
						mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
				}
				break;
			case mlt_playlist_type:
				{
					mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
				}
				break;
			default:
				result = 0;
				mlt_log_warning( NULL, "[producer_xml] Producer defined inside something that isn't a container\n" );
				break;
		};

		// Set the hide state of the track producer
		if ( hide_s != NULL )
		{
			if ( strcmp( hide_s, "video" ) == 0 )
				mlt_properties_set_int( properties, "hide", 1 );
			else if ( strcmp( hide_s, "audio" ) == 0 )
				mlt_properties_set_int( properties, "hide", 2 );
			else if ( strcmp( hide_s, "both" ) == 0 )
				mlt_properties_set_int( properties, "hide", 3 );
		}
	}

	// Put the parent producer back
	if ( container != NULL )
		context_push_service( context, container, type );

	return result;
}

/** Attach filters defined on that to this.
*/

static void attach_filters( mlt_service service, mlt_service that )
{
	if ( that != NULL )
	{
		int i = 0;
		mlt_filter filter = NULL;
		for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
		{
			mlt_service_attach( service, filter );
			attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
		}
	}
}

static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	mlt_profile p = context->profile;
	for ( ; atts != NULL && *atts != NULL; atts += 2 )
	{
		if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
		{
			mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
			if ( my_profile )
			{
				p->description = strdup( my_profile->description );
				p->display_aspect_den = my_profile->display_aspect_den;
				p->display_aspect_num = my_profile->display_aspect_num;
				p->frame_rate_den = my_profile->frame_rate_den;
				p->frame_rate_num = my_profile->frame_rate_num;
				p->width = my_profile->width;
				p->height = my_profile->height;
				p->progressive = my_profile->progressive;
				p->sample_aspect_den = my_profile->sample_aspect_den;
				p->sample_aspect_num = my_profile->sample_aspect_num;
				p->colorspace = my_profile->colorspace;
				p->is_explicit = 1;
				mlt_profile_close( my_profile );
			}
		}
		else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
		{
			if ( p->description )
				free( p->description );
			p->description = strdup( _s(atts[ 1 ]) );
			p->is_explicit = 1;
		}
		else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
			p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
			p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
			p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
			p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
			p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
			p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
			p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
			p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
			p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
		else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
			p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
	}
}

static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	mlt_tractor tractor = mlt_tractor_new( );
	mlt_service service = MLT_TRACTOR_SERVICE( tractor );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
	mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );

	for ( ; atts != NULL && *atts != NULL; atts += 2 )
		mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );

	mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );

	if ( mlt_properties_get( properties, "id" ) != NULL )
		mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
	
	context_push_service( context, service, mlt_tractor_type );
}

static void on_end_tractor( deserialise_context context, const xmlChar *name )
{
	// Get the tractor
	enum service_type type;
	mlt_service tractor = context_pop_service( context, &type );

	if ( tractor != NULL && type == mlt_tractor_type )
	{
		// See if the tractor should be added to a playlist or multitrack
		if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
			context_push_service( context, tractor, type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid state for tractor\n" );
	}
}

static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	enum service_type type;
	mlt_service parent = context_pop_service( context, &type );

	// If we don't have a parent, then create one now, providing we're in a state where we can
	if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
	{
		mlt_tractor tractor = NULL;
		// Push the parent back
		if ( parent != NULL )
			context_push_service( context, parent, type );

		// Create a tractor to contain the multitrack
		tractor = mlt_tractor_new( );
		parent = MLT_TRACTOR_SERVICE( tractor );
		track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
		mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
		type = mlt_tractor_type;

		// Flag it as a synthesised tractor for clean up later
		mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
	}

	if ( type == mlt_tractor_type )
	{
		mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
		mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
			mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );

		if ( mlt_properties_get( properties, "id" ) != NULL )
			mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );

		context_push_service( context, parent, type );
		context_push_service( context, service, mlt_multitrack_type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid multitrack position\n" );
	}
}

static void on_end_multitrack( deserialise_context context, const xmlChar *name )
{
	// Get the multitrack from the stack
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );

	if ( service == NULL || type != mlt_multitrack_type )
		mlt_log_error( NULL, "[producer_xml] End multitrack in the wrong state...\n" );
}

static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	mlt_playlist playlist = mlt_playlist_init( );
	mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );

	for ( ; atts != NULL && *atts != NULL; atts += 2 )
	{
		mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );

		// Out will be overwritten later as we append, so we need to save it
		if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
			mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
	}

	if ( mlt_properties_get( properties, "id" ) != NULL )
		mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );

	context_push_service( context, service, mlt_playlist_type );
}

static void on_end_playlist( deserialise_context context, const xmlChar *name )
{
	// Get the playlist from the stack
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );

	if ( service != NULL && type == mlt_playlist_type )
	{
		mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
		mlt_position in = -1;
		mlt_position out = -1;

		if ( mlt_properties_get( properties, "in" ) )
			in = mlt_properties_get_position( properties, "in" );
		if ( mlt_properties_get( properties, "out" ) )
			out = mlt_properties_get_position( properties, "out" );

		// See if the playlist should be added to a playlist or multitrack
		if ( add_producer( context, service, in, out ) == 0 )
			context_push_service( context, service, type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid state of playlist end %d\n", type );
	}
}

static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	// use a dummy service to hold properties to allow arbitrary nesting
	mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
	mlt_service_init( service, NULL );

	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	context_push_service( context, service, mlt_dummy_producer_type );

	for ( ; atts != NULL && *atts != NULL; atts += 2 )
		mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
}

static void on_end_producer( deserialise_context context, const xmlChar *name )
{
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	if ( service != NULL && type == mlt_dummy_producer_type )
	{
		mlt_service producer = NULL;

		qualify_property( context, properties, "resource" );
		char *resource = trim( mlt_properties_get( properties, "resource" ) );

		// Let Kino-SMIL src be a synonym for resource
		if ( resource == NULL )
		{
			qualify_property( context, properties, "src" );
			resource = trim( mlt_properties_get( properties, "src" ) );
		}

		// Instantiate the producer
		if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
		{
			char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
			if ( resource )
			{
				char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
				strcat( temp, service_name );
				strcat( temp, ":" );
				strcat( temp, resource );
				producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
				free( temp );
			}
			else
			{
				producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
			}
		}

		// Just in case the plugin requested doesn't exist...
		if ( !producer && resource )
			producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
		if ( !producer )
			mlt_log_error( NULL, "[producer_xml] failed to load producer \"%s\"\n", resource );
		if ( !producer )
			producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
		if ( !producer )
			producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
		if ( !producer )
		{
			mlt_service_close( service );
			return;
		}

		// Track this producer
		track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
		mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );

		// Propagate the properties
		qualify_property( context, properties, "resource" );
		qualify_property( context, properties, "luma" );
		qualify_property( context, properties, "luma.resource" );
		qualify_property( context, properties, "composite.luma" );
		qualify_property( context, properties, "producer.resource" );

		// Handle in/out properties separately
		mlt_position in = -1;
		mlt_position out = -1;
	
		// Get in
		if ( mlt_properties_get( properties, "in" ) )
			in = mlt_properties_get_position( properties, "in" );
		// Let Kino-SMIL clipBegin be a synonym for in
		else if ( mlt_properties_get( properties, "clipBegin" ) )
			in = mlt_properties_get_position( properties, "clipBegin" );
		// Get out
		if ( mlt_properties_get( properties, "out" ) )
			out = mlt_properties_get_position( properties, "out" );
		// Let Kino-SMIL clipEnd be a synonym for out
		else if ( mlt_properties_get( properties, "clipEnd" ) )
			out = mlt_properties_get_position( properties, "clipEnd" );
		// Remove in and out
		mlt_properties_set( properties, "in", NULL );
		mlt_properties_set( properties, "out", NULL );

		// Inherit the properties
		mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );

		// Attach all filters from service onto producer
		attach_filters( producer, service );

		// Add the producer to the producer map
		if ( mlt_properties_get( properties, "id" ) != NULL )
			mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );

		// See if the producer should be added to a playlist or multitrack
		if ( add_producer( context, producer, in, out ) == 0 )
		{
			// Otherwise, set in and out on...
			if ( in != -1 || out != -1 )
			{
				// Get the parent service
				enum service_type type;
				mlt_service parent = context_pop_service( context, &type );
				if ( parent != NULL )
				{
					// Get the parent properties
					properties = MLT_SERVICE_PROPERTIES( parent );
				
					char *resource = trim( mlt_properties_get( properties, "resource" ) );
				
					// Put the parent producer back
					context_push_service( context, parent, type );
					
					// If the parent is a track or entry
					if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
					{
						if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
						if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
					}
					else
					{
						// Otherwise, set in and out on producer directly
						mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
					}
				}
				else
				{
					// Otherwise, set in and out on producer directly
					mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
				}
			}

			// Push the producer onto the stack
			context_push_service( context, producer, mlt_producer_type );
		}

		mlt_service_close( service );
	}
}

static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	// Get the playlist from the stack
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_position length = 0;
	
	if ( type == mlt_playlist_type && service != NULL )
	{
		// Look for the length attribute
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
		{
			if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
			{
				length = atoll( _s(atts[1]) );
				break;
			}
		}

		// Append a blank to the playlist
		mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );

		// Push the playlist back onto the stack
		context_push_service( context, service, type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
	}
}

static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	mlt_producer entry = NULL;
	mlt_properties temp = mlt_properties_new( );
	mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
	mlt_properties_set_lcnumeric( temp, context->lc_numeric );

	for ( ; atts != NULL && *atts != NULL; atts += 2 )
	{
		mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
		
		// Look for the producer attribute
		if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
		{
			mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
			if ( producer !=  NULL )
				mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
		}
	}

	// If we have a valid entry
	if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
	{
		mlt_playlist_clip_info info;
		enum service_type parent_type = invalid_type;
		mlt_service parent = context_pop_service( context, &parent_type );
		mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );

		if ( parent_type == mlt_playlist_type )
		{
			// Append the producer to the playlist
			mlt_position in = -1;
			mlt_position out = -1;
			if ( mlt_properties_get( temp, "in" ) )
				in = mlt_properties_get_position( temp, "in" );
			if ( mlt_properties_get( temp, "out" ) )
				out = mlt_properties_get_position( temp, "out" );
			mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );

			// Handle the repeat property
			if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
			{
				mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
										  mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
										  mlt_properties_get_int( temp, "repeat" ) );
			}

			mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
			entry = info.cut;
		}
		else
		{
			mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
		}

		context_push_service( context, parent, parent_type );
	}

	// Push the cut onto the stack
	context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );

	mlt_properties_close( temp );
}

static void on_end_entry( deserialise_context context, const xmlChar *name )
{
	// Get the entry from the stack
	enum service_type entry_type = invalid_type;
	mlt_service entry = context_pop_service( context, &entry_type );

	if ( entry == NULL && entry_type != mlt_entry_type )
	{
		mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
	}
}

static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	// use a dummy service to hold properties to allow arbitrary nesting
	mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
	mlt_service_init( service, NULL );

	// Push the dummy service onto the stack
	context_push_service( context, service, mlt_entry_type );
	
	mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
	
	for ( ; atts != NULL && *atts != NULL; atts += 2 )
	{
		mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
		
		// Look for the producer attribute
		if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
		{
			mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
			if ( producer !=  NULL )
				mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
		}
	}
}

static void on_end_track( deserialise_context context, const xmlChar *name )
{
	// Get the track from the stack
	enum service_type track_type;
	mlt_service track = context_pop_service( context, &track_type );

	if ( track != NULL && track_type == mlt_entry_type )
	{
		mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
		enum service_type parent_type = invalid_type;
		mlt_service parent = context_pop_service( context, &parent_type );
		mlt_multitrack multitrack = NULL;

		mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
		mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );

		if ( parent_type == mlt_tractor_type )
			multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
		else if ( parent_type == mlt_multitrack_type )
			multitrack = MLT_MULTITRACK( parent );
		else
			mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );

		if ( multitrack != NULL )
		{
			// Set producer i/o if specified
			if ( mlt_properties_get( track_props, "in" ) != NULL ||
				 mlt_properties_get( track_props, "out" ) != NULL )
			{
				mlt_position in = -1;
				mlt_position out = -1;
				if ( mlt_properties_get( track_props, "in" ) )
					in = mlt_properties_get_position( track_props, "in" );
				if ( mlt_properties_get( track_props, "out" ) )
					out = mlt_properties_get_position( track_props, "out" );
				mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
				mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
				mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
				track_props = MLT_PRODUCER_PROPERTIES( cut );
				mlt_producer_close( cut );
			}
			else
			{
				mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
			}

			// Set the hide state of the track producer
			char *hide_s = mlt_properties_get( track_props, "hide" );
			if ( hide_s != NULL )
			{
				if ( strcmp( hide_s, "video" ) == 0 )
					mlt_properties_set_int( producer_props, "hide", 1 );
				else if ( strcmp( hide_s, "audio" ) == 0 )
					mlt_properties_set_int( producer_props, "hide", 2 );
				else if ( strcmp( hide_s, "both" ) == 0 )
					mlt_properties_set_int( producer_props, "hide", 3 );
			}
		}

		if ( parent != NULL )
			context_push_service( context, parent, parent_type );

		mlt_service_close( track );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
	}
}

static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	// use a dummy service to hold properties to allow arbitrary nesting
	mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
	mlt_service_init( service, NULL );

	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	context_push_service( context, service, mlt_dummy_filter_type );

	// Set the properties
	for ( ; atts != NULL && *atts != NULL; atts += 2 )
		mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
}

static void on_end_filter( deserialise_context context, const xmlChar *name )
{
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	enum service_type parent_type = invalid_type;
	mlt_service parent = context_pop_service( context, &parent_type );

	if ( service != NULL && type == mlt_dummy_filter_type )
	{
		char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
		mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
		mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );

		if ( !filter )
		{
			mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
			if ( parent )
				context_push_service( context, parent, parent_type );
			mlt_service_close( service );
			return;
		}

		track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
		mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );

		// Propogate the properties
		qualify_property( context, properties, "resource" );
		qualify_property( context, properties, "luma" );
		qualify_property( context, properties, "luma.resource" );
		qualify_property( context, properties, "composite.luma" );
		qualify_property( context, properties, "producer.resource" );
		mlt_properties_inherit( filter_props, properties );

		// Attach all filters from service onto filter
		attach_filters( filter, service );

		// Associate the filter with the parent
		if ( parent != NULL )
		{
			if ( parent_type == mlt_tractor_type )
			{
				mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
				mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
				mlt_filter_set_in_and_out( MLT_FILTER( filter ), 
										   mlt_properties_get_int( properties, "in" ),
										   mlt_properties_get_int( properties, "out" ) );
			}
			else
			{
				mlt_service_attach( parent, MLT_FILTER( filter ) );
			}

			// Put the parent back on the stack
			context_push_service( context, parent, parent_type );
		}
		else
		{
			mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
		}

		// Close the dummy filter service
		mlt_service_close( service );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
	}
}

static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	// use a dummy service to hold properties to allow arbitrary nesting
	mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
	mlt_service_init( service, NULL );

	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	context_push_service( context, service, mlt_dummy_transition_type );

	// Set the properties
	for ( ; atts != NULL && *atts != NULL; atts += 2 )
		mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
}

static void on_end_transition( deserialise_context context, const xmlChar *name )
{
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	enum service_type parent_type = invalid_type;
	mlt_service parent = context_pop_service( context, &parent_type );

	if ( service != NULL && type == mlt_dummy_transition_type )
	{
		char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
		mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
		mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );

		if ( !effect )
		{
			mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
			if ( parent )
				context_push_service( context, parent, parent_type );
			mlt_service_close( service );
			return;
		}
		track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
		mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );

		// Propogate the properties
		qualify_property( context, properties, "resource" );
		qualify_property( context, properties, "luma" );
		qualify_property( context, properties, "luma.resource" );
		qualify_property( context, properties, "composite.luma" );
		qualify_property( context, properties, "producer.resource" );
		mlt_properties_inherit( effect_props, properties );

		// Attach all filters from service onto effect
		attach_filters( effect, service );

		// Associate the filter with the parent
		if ( parent != NULL )
		{
			if ( parent_type == mlt_tractor_type )
			{
				mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
				if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
					mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
				mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
											mlt_properties_get_int( properties, "a_track" ),
											mlt_properties_get_int( properties, "b_track" ) );
				mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
										   mlt_properties_get_int( properties, "in" ),
										   mlt_properties_get_int( properties, "out" ) );
			}
			else
			{
				mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
			}

			// Put the parent back on the stack
			context_push_service( context, parent, parent_type );
		}
		else
		{
			mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
		}

		// Close the dummy filter service
		mlt_service_close( service );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
	}
}

static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	if ( context->pass == 1 )
	{
		mlt_consumer consumer = mlt_consumer_new( context->profile );
		mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );

		mlt_properties_set_lcnumeric( properties, context->lc_numeric );
		context_push_service( context, MLT_CONSUMER_SERVICE(consumer), mlt_dummy_consumer_type );

		// Set the properties from attributes
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
			mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
	}
}

static void on_end_consumer( deserialise_context context, const xmlChar *name )
{
	if ( context->pass == 1 )
	{
		// Get the consumer from the stack
		enum service_type type;
		mlt_service service = context_pop_service( context, &type );

		if ( service && type == mlt_dummy_consumer_type )
		{
			mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
			qualify_property( context, properties, "resource" );
			qualify_property( context, properties, "target" );
			char *resource = trim( mlt_properties_get( properties, "resource" ) );

			if ( context->multi_consumer > 1 )
			{
				// Instantiate the multi consumer
				if ( !context->consumer )
				{
					context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
					if ( context->consumer )
					{
						// Track this consumer
						track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
						mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
					}
				}
				if ( context->consumer )
				{
					// Set this service instance on multi consumer
					char key[20];
					snprintf( key, sizeof(key), "%d", context->consumer_count++ );
					mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, service, 0,
						(mlt_destructor) mlt_service_close, NULL );
				}
			}
			else
			{
				// Instantiate the consumer
				char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
				context->consumer = mlt_factory_consumer( context->profile, id, resource );
				if ( context->consumer )
				{
					// Track this consumer
					track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
					mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );

					// Inherit the properties
					mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
				}
				// Close the dummy
				mlt_service_close( service );
			}
		}
	}
}

static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
{
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
	const char *value = NULL;

	if ( service != NULL )
	{
		// Set the properties
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
		{
			if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
				context->property = strdup( _s(atts[ 1 ]) );
			else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
				value = _s(atts[ 1 ]);
		}

		if ( context->property != NULL )
			mlt_properties_set( properties, context->property, value == NULL ? "" : value );

		// Tell parser to collect any further nodes for serialisation
		context->is_value = 1;

		context_push_service( context, service, type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
	}
}

static void on_end_property( deserialise_context context, const xmlChar *name )
{
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	if ( service != NULL )
	{
		// Tell parser to stop building a tree
		context->is_value = 0;
	
		// See if there is a xml tree for the value
		if ( context->property != NULL && context->value_doc != NULL )
		{
			xmlChar *value;
			int size;
		
			// Serialise the tree to get value
			xmlDocDumpMemory( context->value_doc, &value, &size );
			mlt_properties_set( properties, context->property, _s(value) );
#ifdef WIN32
			xmlFreeFunc xmlFree = NULL;
			xmlMemGet( &xmlFree, NULL, NULL, NULL);
#endif
			xmlFree( value );
			xmlFreeDoc( context->value_doc );
			context->value_doc = NULL;
		}

		// Close this property handling
		free( context->property );
		context->property = NULL;

		context_push_service( context, service, type );
	}
	else
	{
		mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
	}
}

static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	
	if ( context->pass == 0 )
	{
		if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
		     xmlStrcmp( name, _x("profile") ) == 0 ||
		     xmlStrcmp( name, _x("profileinfo") ) == 0 )
			on_start_profile( context, name, atts );
		if ( xmlStrcmp( name, _x("consumer") ) == 0 )
			context->multi_consumer++;
		return;
	}
	context->branch[ context->depth ] ++;
	context->depth ++;
	
	// Build a tree from nodes within a property value
	if ( context->is_value == 1 && context->pass == 1 )
	{
		xmlNodePtr node = xmlNewNode( NULL, name );
		
		if ( context->value_doc == NULL )
		{
			// Start a new tree
			context->value_doc = xmlNewDoc( _x("1.0") );
			xmlDocSetRootElement( context->value_doc, node );
		}
		else
		{
			// Append child to tree
			xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
		}
		context_push_node( context, node );
		
		// Set the attributes
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
			xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
	}
	else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
		on_start_tractor( context, name, atts );
	else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
		on_start_multitrack( context, name, atts );
	else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
		on_start_playlist( context, name, atts );
	else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
		on_start_producer( context, name, atts );
	else if ( xmlStrcmp( name, _x("blank") ) == 0 )
		on_start_blank( context, name, atts );
	else if ( xmlStrcmp( name, _x("entry") ) == 0 )
		on_start_entry( context, name, atts );
	else if ( xmlStrcmp( name, _x("track") ) == 0 )
		on_start_track( context, name, atts );
	else if ( xmlStrcmp( name, _x("filter") ) == 0 )
		on_start_filter( context, name, atts );
	else if ( xmlStrcmp( name, _x("transition") ) == 0 )
		on_start_transition( context, name, atts );
	else if ( xmlStrcmp( name, _x("property") ) == 0 )
		on_start_property( context, name, atts );
	else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
		on_start_consumer( context, name, atts );
	else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
	{
		for ( ; atts != NULL && *atts != NULL; atts += 2 )
		{
			if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
				mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
			else if ( !context->lc_numeric )
				context->lc_numeric = strdup( _s( atts[1] ) );
		}
	}
}

static void on_end_element( void *ctx, const xmlChar *name )
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	
	if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
		context_pop_node( context );
	else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
		on_end_multitrack( context, name );
	else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
		on_end_playlist( context, name );
	else if ( xmlStrcmp( name, _x("track") ) == 0 )
		on_end_track( context, name );
	else if ( xmlStrcmp( name, _x("entry") ) == 0 )
		on_end_entry( context, name );
	else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
		on_end_tractor( context, name );
	else if ( xmlStrcmp( name, _x("property") ) == 0 )
		on_end_property( context, name );
	else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
		on_end_producer( context, name );
	else if ( xmlStrcmp( name, _x("filter") ) == 0 )
		on_end_filter( context, name );
	else if ( xmlStrcmp( name, _x("transition") ) == 0 )
		on_end_transition( context, name );
	else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
		on_end_consumer( context, name );

	context->branch[ context->depth ] = 0;
	context->depth --;
}

static void on_characters( void *ctx, const xmlChar *ch, int len )
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	char *value = calloc( len + 1, 1 );
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	mlt_properties properties = MLT_SERVICE_PROPERTIES( service );

	if ( service != NULL )
		context_push_service( context, service, type );

	value[ len ] = 0;
	strncpy( value, (const char*) ch, len );

	if ( context->stack_node_size > 0 )
		xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );

	// libxml2 generates an on_characters immediately after a get_entity within
	// an element value, and we ignore it because it is called again during
	// actual substitution.
	else if ( context->property != NULL && context->entity_is_replace == 0 )
	{
		char *s = mlt_properties_get( properties, context->property );
		if ( s != NULL )
		{
			// Append new text to existing content
			char *new = calloc( strlen( s ) + len + 1, 1 );
			strcat( new, s );
			strcat( new, value );
			mlt_properties_set( properties, context->property, new );
			free( new );
		}
		else
			mlt_properties_set( properties, context->property, value );
	}
	context->entity_is_replace = 0;
	
	free( value);
}

/** Convert parameters parsed from resource into entity declarations.
*/
static void params_to_entities( deserialise_context context )
{
	if ( context->params != NULL )
	{	
		int i;
		
		// Add our params as entitiy declarations
		for ( i = 0; i < mlt_properties_count( context->params ); i++ )
		{
			xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
			xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
				context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
		}

		// Flag completion
		mlt_properties_close( context->params );
		context->params = NULL;
	}
}

// The following 3 facilitate entity substitution in the SAX parser
static void on_internal_subset( void *ctx, const xmlChar* name,
	const xmlChar* publicId, const xmlChar* systemId )
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	
	context->publicId = publicId;
	context->systemId = systemId;
	xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
	
	// Override default entities with our parameters
	params_to_entities( context );
}

// TODO: Check this with Dan... I think this is for parameterisation
// but it's breaking standard escaped entities (like &lt; etc).
static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
	const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	
	xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
}

// TODO: Check this functionality (see on_entity_declaration)
static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
{
	struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
	deserialise_context context = ( deserialise_context )( xmlcontext->_private );
	xmlEntityPtr e = NULL;

	// Setup for entity declarations if not ready
	if ( xmlGetIntSubset( context->entity_doc ) == NULL )
	{
		xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
		context->publicId = _x("");
		context->systemId = _x("");
	}

	// Add our parameters if not already
	params_to_entities( context );
	
	e = xmlGetPredefinedEntity( name );
	
	// Send signal to on_characters that an entity substitutin is pending
	if ( e == NULL )
	{
		e = xmlGetDocEntity( context->entity_doc, name );
		if ( e != NULL )
			context->entity_is_replace = 1;
	}
	
	return e;
}

static void	on_error( void * ctx, const char * msg, ... )
{
	struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );

	switch( err_ptr->level )
	{
	case XML_ERR_WARNING:
		mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
				         err_ptr->message, err_ptr->line, err_ptr->int2 );
		break;
	case XML_ERR_ERROR:
		mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
				       err_ptr->message, err_ptr->line, err_ptr->int2 );
		break;
	default:
	case XML_ERR_FATAL:
		mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
				       err_ptr->message, err_ptr->line, err_ptr->int2 );
		break;
	}
}

/** Convert a hexadecimal character to its value.
*/
static int tohex( char p )
{
	return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
}

/** Decode a url-encoded string containing hexadecimal character sequences.
*/
static char *url_decode( char *dest, char *src )
{
	char *p = dest;
	
	while ( *src )
	{
		if ( *src == '%' )
		{
			*p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
			src += 3;
		}
		else
		{
			*p ++ = *src ++;
		}
	}

	*p = *src;
	return dest;
}

/** Extract the filename from a URL attaching parameters to a properties list.
*/
static void parse_url( mlt_properties properties, char *url )
{
	int i;
	int n = strlen( url );
	char *name = NULL;
	char *value = NULL;
	
	for ( i = 0; i < n; i++ )
	{
		switch ( url[ i ] )
		{
			case '?':
				url[ i++ ] = '\0';
				name = &url[ i ];
				break;
			
			case ':':
			case '=':
				url[ i++ ] = '\0';
				value = &url[ i ];
				break;
			
			case '&':
				url[ i++ ] = '\0';
				if ( name != NULL && value != NULL )
					mlt_properties_set( properties, name, value );
				name = &url[ i ];
				value = NULL;
				break;
		}
	}
	if ( name != NULL && value != NULL )
		mlt_properties_set( properties, name, value );
}

// Quick workaround to avoid unecessary libxml2 warnings
static int file_exists( char *file )
{
	char *name = strdup( file );
	int exists = 0;
	if ( name != NULL && strchr( name, '?' ) )
		*( strchr( name, '?' ) ) = '\0';
	if ( name != NULL )
	{
		FILE *f = fopen( name, "r" );
		exists = f != NULL;
		if ( exists ) fclose( f );
	}
	free( name );
	return exists;
}

mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
{
	xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
	struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
	mlt_properties properties = NULL;
	int i = 0;
	struct _xmlParserCtxt *xmlcontext;
	int well_formed = 0;
	char *filename = NULL;
	int info = strcmp( id, "xml-string" ) ? 0 : 1;

	// Strip file:// prefix
	if ( data && strlen( data ) >= 7 && strncmp( data, "file://", 7 ) == 0 )
		data += 7;

	if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
		return NULL;

	context = calloc( 1, sizeof( struct deserialise_context_s ) );
	if ( context == NULL )
		return NULL;

	context->producer_map = mlt_properties_new();
	context->destructors = mlt_properties_new();
	context->params = mlt_properties_new();
	context->profile = profile;

	// Decode URL and parse parameters
	mlt_properties_set( context->producer_map, "root", "" );
	if ( info == 0 )
	{
		filename = strdup( data );
		parse_url( context->params, url_decode( filename, data ) );

		// We need the directory prefix which was used for the xml
		if ( strchr( filename, '/' ) )
		{
			char *root = NULL;
			mlt_properties_set( context->producer_map, "root", filename );
			root = mlt_properties_get( context->producer_map, "root" );
			*( strrchr( root, '/' ) ) = '\0';

			// If we don't have an absolute path here, we're heading for disaster...
			if ( root[ 0 ] != '/' )
			{
				char *cwd = getcwd( NULL, 0 );
				char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
				sprintf( real, "%s/%s", cwd, root );
				mlt_properties_set( context->producer_map, "root", real );
				free( real );
				free( cwd );
			}
		}
	}

	// We need to track the number of registered filters
	mlt_properties_set_int( context->destructors, "registered", 0 );

	// Setup SAX callbacks for first pass
	sax->startElement = on_start_element;
	sax->warning = on_error;
	sax->error = on_error;
	sax->fatalError = on_error;

	// Setup libxml2 SAX parsing
	xmlInitParser(); 
	xmlSubstituteEntitiesDefault( 1 );
	// This is used to facilitate entity substitution in the SAX parser
	context->entity_doc = xmlNewDoc( _x("1.0") );
	if ( info == 0 )
		xmlcontext = xmlCreateFileParserCtxt( filename );
	else
		xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );

	// Invalid context - clean up and return NULL
	if ( xmlcontext == NULL )
	{
		mlt_properties_close( context->producer_map );
		mlt_properties_close( context->destructors );
		mlt_properties_close( context->params );
		free( context );
		free( sax );
		free( filename );
		return NULL;
	}

	// Parse
	xmlcontext->sax = sax;
	xmlcontext->_private = ( void* )context;	
	xmlParseDocument( xmlcontext );
	well_formed = xmlcontext->wellFormed;
	
	// Cleanup after parsing
	xmlcontext->sax = NULL;
	xmlcontext->_private = NULL;
	xmlFreeParserCtxt( xmlcontext );
	context->stack_node_size = 0;
	context->stack_service_size = 0;

	// Bad xml - clean up and return NULL
	if ( !well_formed )
	{
		mlt_properties_close( context->producer_map );
		mlt_properties_close( context->destructors );
		mlt_properties_close( context->params );
		xmlFreeDoc( context->entity_doc );
		free( context );
		free( sax );
		free( filename );
		return NULL;
	}

	// Setup the second pass
	context->pass ++;
	if ( info == 0 )
		xmlcontext = xmlCreateFileParserCtxt( filename );
	else
		xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );

	// Invalid context - clean up and return NULL
	if ( xmlcontext == NULL )
	{
		mlt_properties_close( context->producer_map );
		mlt_properties_close( context->destructors );
		mlt_properties_close( context->params );
		xmlFreeDoc( context->entity_doc );
		free( context );
		free( sax );
		free( filename );
		return NULL;
	}

	// Setup SAX callbacks for second pass
	sax->endElement = on_end_element;
	sax->characters = on_characters;
	sax->cdataBlock = on_characters;
	sax->internalSubset = on_internal_subset;
	sax->entityDecl = on_entity_declaration;
	sax->getEntity = on_get_entity;

	// Parse
	xmlcontext->sax = sax;
	xmlcontext->_private = ( void* )context;
	xmlParseDocument( xmlcontext );
	well_formed = xmlcontext->wellFormed;

	// Cleanup after parsing
	xmlFreeDoc( context->entity_doc );
	free( sax );
	xmlMemoryDump( ); // for debugging

	// Get the last producer on the stack
	enum service_type type;
	mlt_service service = context_pop_service( context, &type );
	if ( well_formed && service != NULL )
	{
		// Verify it is a producer service (mlt_type="mlt_producer")
		// (producer, playlist, multitrack)
		char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
		if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
			service = NULL;
	}

#ifdef DEBUG
	xmlDocPtr doc = xml_make_doc( service );
	xmlDocFormatDump( stdout, doc, 1 );
	xmlFreeDoc( doc );
	service = NULL;
#endif
	
	if ( well_formed && service != NULL )
	{
		char *title = mlt_properties_get( context->producer_map, "title" );
		
		// Need the complete producer list for various reasons
		properties = context->destructors;

		// Now make sure we don't have a reference to the service in the properties
		for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
		{
			char *name = mlt_properties_get_name( properties, i );
			if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
			{
				mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
				break;
			}
		}

		// We are done referencing destructor property list
		// Set this var to service properties for convenience
		properties = MLT_SERVICE_PROPERTIES( service );
	
		// Assign the title
		mlt_properties_set( properties, "title", title );

		// Optimise for overlapping producers
		mlt_producer_optimise( MLT_PRODUCER( service ) );

		// Handle deep copies
		if ( getenv( "MLT_XML_DEEP" ) == NULL )
		{
			// Now assign additional properties
			if ( info == 0 )
				mlt_properties_set( properties, "resource", data );

			// This tells consumer_xml not to deep copy
			mlt_properties_set( properties, "xml", "was here" );
		}
		else
		{
			// Allow the project to be edited
			mlt_properties_set( properties, "_xml", "was here" );
			mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
		}

		// Make consumer available
		mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
		mlt_properties_set_data( properties, "consumer", context->consumer, 0,
			(mlt_destructor) mlt_consumer_close, NULL );
	}
	else
	{
		// Return null if not well formed
		service = NULL;
	}

	// Clean up
	mlt_properties_close( context->producer_map );
	if ( context->params != NULL )
		mlt_properties_close( context->params );
	mlt_properties_close( context->destructors );
	if ( context->lc_numeric )
		free( context->lc_numeric );
	free( context );
	free( filename );

	return MLT_PRODUCER( service );
}