]> git.sesse.net Git - mlt/blob - src/modules/xml/producer_xml.c
partially fix leak of service allocated in on_start_xxx
[mlt] / src / modules / xml / producer_xml.c
1 /*
2  * producer_xml.c -- a libxml2 parser of mlt service networks
3  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 //       when the returned producer is closed).
23
24 #include <framework/mlt.h>
25 #include <framework/mlt_log.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
34
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
37
38 #define _x (const xmlChar*)
39 #define _s (const char*)
40
41 #undef DEBUG
42 #ifdef DEBUG
43 extern xmlDocPtr xml_make_doc( mlt_service service );
44 #endif
45
46 enum service_type
47 {
48         mlt_invalid_type,
49         mlt_unknown_type,
50         mlt_producer_type,
51         mlt_playlist_type,
52         mlt_entry_type,
53         mlt_tractor_type,
54         mlt_multitrack_type,
55         mlt_filter_type,
56         mlt_transition_type,
57         mlt_consumer_type,
58         mlt_field_type,
59         mlt_services_type,
60         mlt_dummy_filter_type,
61         mlt_dummy_transition_type,
62         mlt_dummy_producer_type,
63         mlt_dummy_consumer_type
64 };
65
66 struct deserialise_context_s
67 {
68         enum service_type stack_types[ STACK_SIZE ];
69         mlt_service stack_service[ STACK_SIZE ];
70         int stack_service_size;
71         mlt_properties producer_map;
72         mlt_properties destructors;
73         char *property;
74         int is_value;
75         xmlDocPtr value_doc;
76         xmlNodePtr stack_node[ STACK_SIZE ];
77         int stack_node_size;
78         xmlDocPtr entity_doc;
79         int entity_is_replace;
80         int depth;
81         int branch[ STACK_SIZE ];
82         const xmlChar *publicId;
83         const xmlChar *systemId;
84         mlt_properties params;
85         mlt_profile profile;
86         int pass;
87         char *lc_numeric;
88         mlt_consumer consumer;
89         int multi_consumer;
90         int consumer_count;
91         int seekable;
92 };
93 typedef struct deserialise_context_s *deserialise_context;
94
95 /** Trim the leading and trailing whitespace from a string in-place.
96 */
97 static char* trim( char *s )
98 {
99         int n;
100         if ( s && ( n = strlen( s ) ) )
101         {
102                 int i = 0;
103                 while ( i < n && isspace( s[i] ) ) i++;
104                 while ( --n && isspace( s[n] ) );
105                 n = n - i + 1;
106                 if ( n > 0 )
107                         memmove( s, s + i, n );
108                 s[ n ] = 0;
109         }
110         return s;
111 }
112
113 /** Convert the numerical current branch address to a dot-delimited string.
114 */
115 static char *serialise_branch( deserialise_context context, char *s )
116 {
117         int i;
118         
119         s[0] = 0;
120         for ( i = 0; i < context->depth; i++ )
121         {
122                 int len = strlen( s );
123                 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
124         }
125         return s;
126 }
127
128 /** Push a service.
129 */
130
131 static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
132 {
133         int ret = context->stack_service_size >= STACK_SIZE - 1;
134         if ( ret == 0 )
135         {
136                 context->stack_service[ context->stack_service_size ] = that;
137                 context->stack_types[ context->stack_service_size++ ] = type;
138                 
139                 // Record the tree branch on which this service lives
140                 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
141                 {
142                         char s[ BRANCH_SIG_LEN ];
143                         mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
144                 }
145         }
146         return ret;
147 }
148
149 /** Pop a service.
150 */
151
152 static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
153 {
154         mlt_service result = NULL;
155         
156         if ( type ) *type = invalid_type;
157         if ( context->stack_service_size > 0 )
158         {
159                 result = context->stack_service[ -- context->stack_service_size ];
160                 if ( type != NULL )
161                         *type = context->stack_types[ context->stack_service_size ];
162                 // Set the service's profile and locale so mlt_property time-to-position conversions can get fps
163                 if ( result )
164                 {
165                         mlt_properties_set_data( MLT_SERVICE_PROPERTIES( result ), "_profile", context->profile, 0, NULL, NULL );
166                         mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( result ), context->lc_numeric );
167                 }
168         }
169         return result;
170 }
171
172 /** Push a node.
173 */
174
175 static int context_push_node( deserialise_context context, xmlNodePtr node )
176 {
177         int ret = context->stack_node_size >= STACK_SIZE - 1;
178         if ( ret == 0 )
179                 context->stack_node[ context->stack_node_size ++ ] = node;
180         return ret;
181 }
182
183 /** Pop a node.
184 */
185
186 static xmlNodePtr context_pop_node( deserialise_context context )
187 {
188         xmlNodePtr result = NULL;
189         if ( context->stack_node_size > 0 )
190                 result = context->stack_node[ -- context->stack_node_size ];
191         return result;
192 }
193
194
195 // Set the destructor on a new service
196 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
197 {
198         int registered = mlt_properties_get_int( properties, "registered" );
199         char *key = mlt_properties_get( properties, "registered" );
200         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
201         mlt_properties_set_int( properties, "registered", ++ registered );
202 }
203
204
205 // Prepend the property value with the document root
206 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
207 {
208         char *resource = mlt_properties_get( properties, name );
209         if ( resource != NULL && resource[0] )
210         {
211                 // Qualify file name properties 
212                 char *root = mlt_properties_get( context->producer_map, "root" );
213                 if ( root != NULL && strcmp( root, "" ) )
214                 {
215                         char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
216                         if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
217                         {
218                                 strcpy( full_resource, root );
219                                 strcat( full_resource, "/" );
220                                 strcat( full_resource, resource );
221                         }
222                         else
223                         {
224                                 strcpy( full_resource, resource );
225                         }
226                         mlt_properties_set( properties, name, full_resource );
227                         free( full_resource );
228                 }
229         }
230 }
231
232
233 /** This function adds a producer to a playlist or multitrack when
234     there is no entry or track element.
235 */
236
237 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
238 {
239         // Return value (0 = service remains top of stack, 1 means it can be removed)
240         int result = 0;
241
242         // Get the parent producer
243         enum service_type type = mlt_invalid_type;
244         mlt_service container = context_pop_service( context, &type );
245         int contained = 0;
246
247         if ( service != NULL && container != NULL )
248         {
249                 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
250                 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
251                 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
252         }
253
254         if ( contained )
255         {
256                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
257                 char *hide_s = mlt_properties_get( properties, "hide" );
258
259                 // Indicate that this service is no longer top of stack
260                 result = 1;
261
262                 switch( type )
263                 {
264                         case mlt_tractor_type: 
265                                 {
266                                         mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
267                                         mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
268                                 }
269                                 break;
270                         case mlt_multitrack_type:
271                                 {
272                                         mlt_multitrack_connect( MLT_MULTITRACK( container ),
273                                                 MLT_PRODUCER( service ),
274                                                 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
275                                 }
276                                 break;
277                         case mlt_playlist_type:
278                                 {
279                                         mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
280                                 }
281                                 break;
282                         default:
283                                 result = 0;
284                                 mlt_log_warning( NULL, "[producer_xml] Producer defined inside something that isn't a container\n" );
285                                 break;
286                 };
287
288                 // Set the hide state of the track producer
289                 if ( hide_s != NULL )
290                 {
291                         if ( strcmp( hide_s, "video" ) == 0 )
292                                 mlt_properties_set_int( properties, "hide", 1 );
293                         else if ( strcmp( hide_s, "audio" ) == 0 )
294                                 mlt_properties_set_int( properties, "hide", 2 );
295                         else if ( strcmp( hide_s, "both" ) == 0 )
296                                 mlt_properties_set_int( properties, "hide", 3 );
297                 }
298         }
299
300         // Put the parent producer back
301         if ( container != NULL )
302                 context_push_service( context, container, type );
303
304         return result;
305 }
306
307 /** Attach filters defined on that to this.
308 */
309
310 static void attach_filters( mlt_service service, mlt_service that )
311 {
312         if ( that != NULL )
313         {
314                 int i = 0;
315                 mlt_filter filter = NULL;
316                 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
317                 {
318                         mlt_service_attach( service, filter );
319                         attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
320                 }
321         }
322 }
323
324 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
325 {
326         mlt_profile p = context->profile;
327         for ( ; atts != NULL && *atts != NULL; atts += 2 )
328         {
329                 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
330                 {
331                         mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
332                         if ( my_profile )
333                         {
334                                 p->description = strdup( my_profile->description );
335                                 p->display_aspect_den = my_profile->display_aspect_den;
336                                 p->display_aspect_num = my_profile->display_aspect_num;
337                                 p->frame_rate_den = my_profile->frame_rate_den;
338                                 p->frame_rate_num = my_profile->frame_rate_num;
339                                 p->width = my_profile->width;
340                                 p->height = my_profile->height;
341                                 p->progressive = my_profile->progressive;
342                                 p->sample_aspect_den = my_profile->sample_aspect_den;
343                                 p->sample_aspect_num = my_profile->sample_aspect_num;
344                                 p->colorspace = my_profile->colorspace;
345                                 p->is_explicit = 1;
346                                 mlt_profile_close( my_profile );
347                         }
348                 }
349                 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
350                 {
351                         if ( p->description )
352                                 free( p->description );
353                         p->description = strdup( _s(atts[ 1 ]) );
354                         p->is_explicit = 1;
355                 }
356                 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
357                         p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
358                 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
359                         p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
360                 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
361                         p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
362                 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
363                         p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
364                 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
365                         p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
366                 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
367                         p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
368                 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
369                         p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
370                 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
371                         p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
372                 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
373                         p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
374                 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
375                         p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
376         }
377 }
378
379 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
380 {
381         mlt_tractor tractor = mlt_tractor_new( );
382         mlt_service service = MLT_TRACTOR_SERVICE( tractor );
383         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
384
385         track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
386         mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );
387
388         for ( ; atts != NULL && *atts != NULL; atts += 2 )
389                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
390
391         mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
392
393         if ( mlt_properties_get( properties, "id" ) != NULL )
394                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
395         
396         context_push_service( context, service, mlt_tractor_type );
397 }
398
399 static void on_end_tractor( deserialise_context context, const xmlChar *name )
400 {
401         // Get the tractor
402         enum service_type type;
403         mlt_service tractor = context_pop_service( context, &type );
404
405         if ( tractor != NULL && type == mlt_tractor_type )
406         {
407                 // See if the tractor should be added to a playlist or multitrack
408                 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
409                         context_push_service( context, tractor, type );
410         }
411         else
412         {
413                 mlt_log_error( NULL, "[producer_xml] Invalid state for tractor\n" );
414         }
415 }
416
417 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
418 {
419         enum service_type type;
420         mlt_service parent = context_pop_service( context, &type );
421
422         // If we don't have a parent, then create one now, providing we're in a state where we can
423         if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
424         {
425                 mlt_tractor tractor = NULL;
426                 // Push the parent back
427                 if ( parent != NULL )
428                         context_push_service( context, parent, type );
429
430                 // Create a tractor to contain the multitrack
431                 tractor = mlt_tractor_new( );
432                 parent = MLT_TRACTOR_SERVICE( tractor );
433                 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
434                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
435                 type = mlt_tractor_type;
436
437                 // Flag it as a synthesised tractor for clean up later
438                 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
439         }
440
441         if ( type == mlt_tractor_type )
442         {
443                 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
444                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
445                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
446                         mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
447
448                 if ( mlt_properties_get( properties, "id" ) != NULL )
449                         mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
450
451                 context_push_service( context, parent, type );
452                 context_push_service( context, service, mlt_multitrack_type );
453         }
454         else
455         {
456                 mlt_log_error( NULL, "[producer_xml] Invalid multitrack position\n" );
457         }
458 }
459
460 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
461 {
462         // Get the multitrack from the stack
463         enum service_type type;
464         mlt_service service = context_pop_service( context, &type );
465
466         if ( service == NULL || type != mlt_multitrack_type )
467                 mlt_log_error( NULL, "[producer_xml] End multitrack in the wrong state...\n" );
468 }
469
470 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
471 {
472         mlt_playlist playlist = mlt_playlist_new( context->profile );
473         mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
474         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
475
476         track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
477
478         for ( ; atts != NULL && *atts != NULL; atts += 2 )
479         {
480                 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
481
482                 // Out will be overwritten later as we append, so we need to save it
483                 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
484                         mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
485         }
486
487         if ( mlt_properties_get( properties, "id" ) != NULL )
488                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
489
490         context_push_service( context, service, mlt_playlist_type );
491 }
492
493 static void on_end_playlist( deserialise_context context, const xmlChar *name )
494 {
495         // Get the playlist from the stack
496         enum service_type type;
497         mlt_service service = context_pop_service( context, &type );
498
499         if ( service != NULL && type == mlt_playlist_type )
500         {
501                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
502                 mlt_position in = -1;
503                 mlt_position out = -1;
504
505                 if ( mlt_properties_get( properties, "in" ) )
506                         in = mlt_properties_get_position( properties, "in" );
507                 if ( mlt_properties_get( properties, "out" ) )
508                         out = mlt_properties_get_position( properties, "out" );
509
510                 // See if the playlist should be added to a playlist or multitrack
511                 if ( add_producer( context, service, in, out ) == 0 )
512                         context_push_service( context, service, type );
513         }
514         else
515         {
516                 mlt_log_error( NULL, "[producer_xml] Invalid state of playlist end %d\n", type );
517         }
518 }
519
520 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
521 {
522         // use a dummy service to hold properties to allow arbitrary nesting
523         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
524         mlt_service_init( service, NULL );
525
526         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
527
528         context_push_service( context, service, mlt_dummy_producer_type );
529
530         for ( ; atts != NULL && *atts != NULL; atts += 2 )
531                 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
532 }
533
534 static void on_end_producer( deserialise_context context, const xmlChar *name )
535 {
536         enum service_type type;
537         mlt_service service = context_pop_service( context, &type );
538         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
539
540         if ( service != NULL && type == mlt_dummy_producer_type )
541         {
542                 mlt_service producer = NULL;
543
544                 qualify_property( context, properties, "resource" );
545                 char *resource = trim( mlt_properties_get( properties, "resource" ) );
546
547                 // Let Kino-SMIL src be a synonym for resource
548                 if ( resource == NULL )
549                 {
550                         qualify_property( context, properties, "src" );
551                         resource = trim( mlt_properties_get( properties, "src" ) );
552                 }
553
554                 // Instantiate the producer
555                 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
556                 {
557                         char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
558                         if ( resource )
559                         {
560                                 char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
561                                 strcat( temp, service_name );
562                                 strcat( temp, ":" );
563                                 strcat( temp, resource );
564                                 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
565                                 free( temp );
566                         }
567                         else
568                         {
569                                 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
570                         }
571                 }
572
573                 // Just in case the plugin requested doesn't exist...
574                 if ( !producer && resource )
575                         producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
576                 if ( !producer )
577                         mlt_log_error( NULL, "[producer_xml] failed to load producer \"%s\"\n", resource );
578                 if ( !producer )
579                         producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
580                 if ( !producer )
581                         producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
582                 if ( !producer )
583                 {
584                         mlt_service_close( service );
585                         free( service );
586                         return;
587                 }
588
589                 // Track this producer
590                 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
591                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );
592                 context->seekable &= mlt_properties_get_int( MLT_SERVICE_PROPERTIES( producer ), "seekable" );
593
594                 // Propagate the properties
595                 qualify_property( context, properties, "resource" );
596                 qualify_property( context, properties, "luma" );
597                 qualify_property( context, properties, "luma.resource" );
598                 qualify_property( context, properties, "composite.luma" );
599                 qualify_property( context, properties, "producer.resource" );
600
601                 // Handle in/out properties separately
602                 mlt_position in = -1;
603                 mlt_position out = -1;
604         
605                 // Get in
606                 if ( mlt_properties_get( properties, "in" ) )
607                         in = mlt_properties_get_position( properties, "in" );
608                 // Let Kino-SMIL clipBegin be a synonym for in
609                 else if ( mlt_properties_get( properties, "clipBegin" ) )
610                         in = mlt_properties_get_position( properties, "clipBegin" );
611                 // Get out
612                 if ( mlt_properties_get( properties, "out" ) )
613                         out = mlt_properties_get_position( properties, "out" );
614                 // Let Kino-SMIL clipEnd be a synonym for out
615                 else if ( mlt_properties_get( properties, "clipEnd" ) )
616                         out = mlt_properties_get_position( properties, "clipEnd" );
617                 // Remove in and out
618                 mlt_properties_set( properties, "in", NULL );
619                 mlt_properties_set( properties, "out", NULL );
620
621                 // Inherit the properties
622                 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
623
624                 // Attach all filters from service onto producer
625                 attach_filters( producer, service );
626
627                 // Add the producer to the producer map
628                 if ( mlt_properties_get( properties, "id" ) != NULL )
629                         mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
630
631                 // See if the producer should be added to a playlist or multitrack
632                 if ( add_producer( context, producer, in, out ) == 0 )
633                 {
634                         // Otherwise, set in and out on...
635                         if ( in != -1 || out != -1 )
636                         {
637                                 // Get the parent service
638                                 enum service_type type;
639                                 mlt_service parent = context_pop_service( context, &type );
640                                 if ( parent != NULL )
641                                 {
642                                         // Get the parent properties
643                                         properties = MLT_SERVICE_PROPERTIES( parent );
644                                 
645                                         char *resource = trim( mlt_properties_get( properties, "resource" ) );
646                                 
647                                         // Put the parent producer back
648                                         context_push_service( context, parent, type );
649                                         
650                                         // If the parent is a track or entry
651                                         if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
652                                         {
653                                                 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
654                                                 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
655                                         }
656                                         else
657                                         {
658                                                 // Otherwise, set in and out on producer directly
659                                                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
660                                         }
661                                 }
662                                 else
663                                 {
664                                         // Otherwise, set in and out on producer directly
665                                         mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
666                                 }
667                         }
668
669                         // Push the producer onto the stack
670                         context_push_service( context, producer, mlt_producer_type );
671                 }
672         }
673
674         if ( service )
675         {
676                 mlt_service_close( service );
677                 free( service );
678         }
679 }
680
681 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
682 {
683         // Get the playlist from the stack
684         enum service_type type;
685         mlt_service service = context_pop_service( context, &type );
686         
687         if ( type == mlt_playlist_type && service != NULL )
688         {
689                 // Look for the length attribute
690                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
691                 {
692                         if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
693                         {
694                                 // Append a blank to the playlist
695                                 mlt_playlist_blank_time( MLT_PLAYLIST( service ), _s(atts[1]) );
696                                 break;
697                         }
698                 }
699
700                 // Push the playlist back onto the stack
701                 context_push_service( context, service, type );
702         }
703         else
704         {
705                 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
706         }
707 }
708
709 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
710 {
711         mlt_producer entry = NULL;
712         mlt_properties temp = mlt_properties_new( );
713         mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
714         mlt_properties_set_lcnumeric( temp, context->lc_numeric );
715
716         for ( ; atts != NULL && *atts != NULL; atts += 2 )
717         {
718                 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
719                 
720                 // Look for the producer attribute
721                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
722                 {
723                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
724                         if ( producer !=  NULL )
725                                 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
726                 }
727         }
728
729         // If we have a valid entry
730         if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
731         {
732                 mlt_playlist_clip_info info;
733                 enum service_type parent_type = invalid_type;
734                 mlt_service parent = context_pop_service( context, &parent_type );
735                 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
736
737                 if ( parent_type == mlt_playlist_type )
738                 {
739                         // Append the producer to the playlist
740                         mlt_position in = -1;
741                         mlt_position out = -1;
742                         if ( mlt_properties_get( temp, "in" ) )
743                                 in = mlt_properties_get_position( temp, "in" );
744                         if ( mlt_properties_get( temp, "out" ) )
745                                 out = mlt_properties_get_position( temp, "out" );
746                         mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
747
748                         // Handle the repeat property
749                         if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
750                         {
751                                 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
752                                                                                   mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
753                                                                                   mlt_properties_get_int( temp, "repeat" ) );
754                         }
755
756                         mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
757                         entry = info.cut;
758                 }
759                 else
760                 {
761                         mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
762                 }
763
764                 context_push_service( context, parent, parent_type );
765         }
766
767         // Push the cut onto the stack
768         context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
769
770         mlt_properties_close( temp );
771 }
772
773 static void on_end_entry( deserialise_context context, const xmlChar *name )
774 {
775         // Get the entry from the stack
776         enum service_type entry_type = invalid_type;
777         mlt_service entry = context_pop_service( context, &entry_type );
778
779         if ( entry == NULL && entry_type != mlt_entry_type )
780         {
781                 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
782         }
783 }
784
785 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
786 {
787         // use a dummy service to hold properties to allow arbitrary nesting
788         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
789         mlt_service_init( service, NULL );
790
791         // Push the dummy service onto the stack
792         context_push_service( context, service, mlt_entry_type );
793         
794         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
795         
796         for ( ; atts != NULL && *atts != NULL; atts += 2 )
797         {
798                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
799                 
800                 // Look for the producer attribute
801                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
802                 {
803                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
804                         if ( producer !=  NULL )
805                                 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
806                 }
807         }
808 }
809
810 static void on_end_track( deserialise_context context, const xmlChar *name )
811 {
812         // Get the track from the stack
813         enum service_type track_type;
814         mlt_service track = context_pop_service( context, &track_type );
815
816         if ( track != NULL && track_type == mlt_entry_type )
817         {
818                 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
819                 enum service_type parent_type = invalid_type;
820                 mlt_service parent = context_pop_service( context, &parent_type );
821                 mlt_multitrack multitrack = NULL;
822
823                 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
824                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
825
826                 if ( parent_type == mlt_tractor_type )
827                         multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
828                 else if ( parent_type == mlt_multitrack_type )
829                         multitrack = MLT_MULTITRACK( parent );
830                 else
831                         mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
832
833                 if ( multitrack != NULL )
834                 {
835                         // Set producer i/o if specified
836                         if ( mlt_properties_get( track_props, "in" ) != NULL ||
837                                  mlt_properties_get( track_props, "out" ) != NULL )
838                         {
839                                 mlt_position in = -1;
840                                 mlt_position out = -1;
841                                 if ( mlt_properties_get( track_props, "in" ) )
842                                         in = mlt_properties_get_position( track_props, "in" );
843                                 if ( mlt_properties_get( track_props, "out" ) )
844                                         out = mlt_properties_get_position( track_props, "out" );
845                                 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
846                                 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
847                                 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
848                                 track_props = MLT_PRODUCER_PROPERTIES( cut );
849                                 mlt_producer_close( cut );
850                         }
851                         else
852                         {
853                                 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
854                         }
855
856                         // Set the hide state of the track producer
857                         char *hide_s = mlt_properties_get( track_props, "hide" );
858                         if ( hide_s != NULL )
859                         {
860                                 if ( strcmp( hide_s, "video" ) == 0 )
861                                         mlt_properties_set_int( producer_props, "hide", 1 );
862                                 else if ( strcmp( hide_s, "audio" ) == 0 )
863                                         mlt_properties_set_int( producer_props, "hide", 2 );
864                                 else if ( strcmp( hide_s, "both" ) == 0 )
865                                         mlt_properties_set_int( producer_props, "hide", 3 );
866                         }
867                 }
868
869                 if ( parent != NULL )
870                         context_push_service( context, parent, parent_type );
871         }
872         else
873         {
874                 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
875         }
876
877         if ( track )
878         {
879                 mlt_service_close( track );
880                 free( track );
881         }
882 }
883
884 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
885 {
886         // use a dummy service to hold properties to allow arbitrary nesting
887         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
888         mlt_service_init( service, NULL );
889
890         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
891
892         context_push_service( context, service, mlt_dummy_filter_type );
893
894         // Set the properties
895         for ( ; atts != NULL && *atts != NULL; atts += 2 )
896                 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
897 }
898
899 static void on_end_filter( deserialise_context context, const xmlChar *name )
900 {
901         enum service_type type;
902         mlt_service service = context_pop_service( context, &type );
903         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
904
905         enum service_type parent_type = invalid_type;
906         mlt_service parent = context_pop_service( context, &parent_type );
907
908         if ( service != NULL && type == mlt_dummy_filter_type )
909         {
910                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
911                 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
912                 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
913
914                 if ( !filter )
915                 {
916                         mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
917                         if ( parent )
918                                 context_push_service( context, parent, parent_type );
919                         mlt_service_close( service );
920                         free( service );
921                         return;
922                 }
923
924                 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
925                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
926
927                 // Propogate the properties
928                 qualify_property( context, properties, "resource" );
929                 qualify_property( context, properties, "luma" );
930                 qualify_property( context, properties, "luma.resource" );
931                 qualify_property( context, properties, "composite.luma" );
932                 qualify_property( context, properties, "producer.resource" );
933                 mlt_properties_inherit( filter_props, properties );
934
935                 // Attach all filters from service onto filter
936                 attach_filters( filter, service );
937
938                 // Associate the filter with the parent
939                 if ( parent != NULL )
940                 {
941                         if ( parent_type == mlt_tractor_type )
942                         {
943                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
944                                 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
945                                 mlt_filter_set_in_and_out( MLT_FILTER( filter ), 
946                                                                                    mlt_properties_get_int( properties, "in" ),
947                                                                                    mlt_properties_get_int( properties, "out" ) );
948                         }
949                         else
950                         {
951                                 mlt_service_attach( parent, MLT_FILTER( filter ) );
952                         }
953
954                         // Put the parent back on the stack
955                         context_push_service( context, parent, parent_type );
956                 }
957                 else
958                 {
959                         mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
960                 }
961         }
962         else
963         {
964                 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
965         }
966
967         if ( service )
968         {
969                 mlt_service_close( service );
970                 free(service);
971         }
972 }
973
974 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
975 {
976         // use a dummy service to hold properties to allow arbitrary nesting
977         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
978         mlt_service_init( service, NULL );
979
980         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
981
982         context_push_service( context, service, mlt_dummy_transition_type );
983
984         // Set the properties
985         for ( ; atts != NULL && *atts != NULL; atts += 2 )
986                 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
987 }
988
989 static void on_end_transition( deserialise_context context, const xmlChar *name )
990 {
991         enum service_type type;
992         mlt_service service = context_pop_service( context, &type );
993         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
994
995         enum service_type parent_type = invalid_type;
996         mlt_service parent = context_pop_service( context, &parent_type );
997
998         if ( service != NULL && type == mlt_dummy_transition_type )
999         {
1000                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1001                 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
1002                 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
1003
1004                 if ( !effect )
1005                 {
1006                         mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
1007                         if ( parent )
1008                                 context_push_service( context, parent, parent_type );
1009                         mlt_service_close( service );
1010                         free( service );
1011                         return;
1012                 }
1013                 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1014                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1015
1016                 // Propogate the properties
1017                 qualify_property( context, properties, "resource" );
1018                 qualify_property( context, properties, "luma" );
1019                 qualify_property( context, properties, "luma.resource" );
1020                 qualify_property( context, properties, "composite.luma" );
1021                 qualify_property( context, properties, "producer.resource" );
1022                 mlt_properties_inherit( effect_props, properties );
1023
1024                 // Attach all filters from service onto effect
1025                 attach_filters( effect, service );
1026
1027                 // Associate the filter with the parent
1028                 if ( parent != NULL )
1029                 {
1030                         if ( parent_type == mlt_tractor_type )
1031                         {
1032                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1033                                 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1034                                         mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1035                                 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1036                                                                                         mlt_properties_get_int( properties, "a_track" ),
1037                                                                                         mlt_properties_get_int( properties, "b_track" ) );
1038                                 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1039                                                                                    mlt_properties_get_int( properties, "in" ),
1040                                                                                    mlt_properties_get_int( properties, "out" ) );
1041                         }
1042                         else
1043                         {
1044                                 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1045                         }
1046
1047                         // Put the parent back on the stack
1048                         context_push_service( context, parent, parent_type );
1049                 }
1050                 else
1051                 {
1052                         mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1053                 }
1054
1055         }
1056         else
1057         {
1058                 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1059         }
1060
1061         if ( service )
1062         {
1063                 mlt_service_close( service );
1064                 free( service );
1065         }
1066 }
1067
1068 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1069 {
1070         if ( context->pass == 1 )
1071         {
1072                 mlt_consumer consumer = mlt_consumer_new( context->profile );
1073                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1074
1075                 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1076                 context_push_service( context, MLT_CONSUMER_SERVICE(consumer), mlt_dummy_consumer_type );
1077
1078                 // Set the properties from attributes
1079                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1080                         mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1081         }
1082 }
1083
1084 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1085 {
1086         if ( context->pass == 1 )
1087         {
1088                 // Get the consumer from the stack
1089                 enum service_type type;
1090                 mlt_service service = context_pop_service( context, &type );
1091
1092                 if ( service && type == mlt_dummy_consumer_type )
1093                 {
1094                         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1095                         qualify_property( context, properties, "resource" );
1096                         qualify_property( context, properties, "target" );
1097                         char *resource = trim( mlt_properties_get( properties, "resource" ) );
1098
1099                         if ( context->multi_consumer > 1 )
1100                         {
1101                                 // Instantiate the multi consumer
1102                                 if ( !context->consumer )
1103                                 {
1104                                         context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1105                                         if ( context->consumer )
1106                                         {
1107                                                 // Track this consumer
1108                                                 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1109                                                 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1110                                         }
1111                                 }
1112                                 if ( context->consumer )
1113                                 {
1114                                         // Set this service instance on multi consumer
1115                                         char key[20];
1116                                         snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1117                                         mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, service, 0,
1118                                                 (mlt_destructor) mlt_service_close, NULL );
1119                                 }
1120                         }
1121                         else
1122                         {
1123                                 // Instantiate the consumer
1124                                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1125                                 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1126                                 if ( context->consumer )
1127                                 {
1128                                         // Track this consumer
1129                                         track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1130                                         mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1131
1132                                         // Inherit the properties
1133                                         mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1134                                 }
1135                                 // Close the dummy
1136                                 mlt_service_close( service );
1137                         }
1138                 }
1139         }
1140 }
1141
1142 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1143 {
1144         enum service_type type;
1145         mlt_service service = context_pop_service( context, &type );
1146         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1147         const char *value = NULL;
1148
1149         if ( service != NULL )
1150         {
1151                 // Set the properties
1152                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1153                 {
1154                         if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1155                                 context->property = strdup( _s(atts[ 1 ]) );
1156                         else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1157                                 value = _s(atts[ 1 ]);
1158                 }
1159
1160                 if ( context->property != NULL )
1161                         mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1162
1163                 // Tell parser to collect any further nodes for serialisation
1164                 context->is_value = 1;
1165
1166                 context_push_service( context, service, type );
1167         }
1168         else
1169         {
1170                 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1171         }
1172 }
1173
1174 static void on_end_property( deserialise_context context, const xmlChar *name )
1175 {
1176         enum service_type type;
1177         mlt_service service = context_pop_service( context, &type );
1178         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1179
1180         if ( service != NULL )
1181         {
1182                 // Tell parser to stop building a tree
1183                 context->is_value = 0;
1184         
1185                 // See if there is a xml tree for the value
1186                 if ( context->property != NULL && context->value_doc != NULL )
1187                 {
1188                         xmlChar *value;
1189                         int size;
1190                 
1191                         // Serialise the tree to get value
1192                         xmlDocDumpMemory( context->value_doc, &value, &size );
1193                         mlt_properties_set( properties, context->property, _s(value) );
1194 #ifdef WIN32
1195                         xmlFreeFunc xmlFree = NULL;
1196                         xmlMemGet( &xmlFree, NULL, NULL, NULL);
1197 #endif
1198                         xmlFree( value );
1199                         xmlFreeDoc( context->value_doc );
1200                         context->value_doc = NULL;
1201                 }
1202
1203                 // Close this property handling
1204                 free( context->property );
1205                 context->property = NULL;
1206
1207                 context_push_service( context, service, type );
1208         }
1209         else
1210         {
1211                 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1212         }
1213 }
1214
1215 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1216 {
1217         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1218         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1219         
1220         if ( context->pass == 0 )
1221         {
1222                 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1223                      xmlStrcmp( name, _x("profile") ) == 0 ||
1224                      xmlStrcmp( name, _x("profileinfo") ) == 0 )
1225                         on_start_profile( context, name, atts );
1226                 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1227                         context->multi_consumer++;
1228                 return;
1229         }
1230         context->branch[ context->depth ] ++;
1231         context->depth ++;
1232         
1233         // Build a tree from nodes within a property value
1234         if ( context->is_value == 1 && context->pass == 1 )
1235         {
1236                 xmlNodePtr node = xmlNewNode( NULL, name );
1237                 
1238                 if ( context->value_doc == NULL )
1239                 {
1240                         // Start a new tree
1241                         context->value_doc = xmlNewDoc( _x("1.0") );
1242                         xmlDocSetRootElement( context->value_doc, node );
1243                 }
1244                 else
1245                 {
1246                         // Append child to tree
1247                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1248                 }
1249                 context_push_node( context, node );
1250                 
1251                 // Set the attributes
1252                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1253                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1254         }
1255         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1256                 on_start_tractor( context, name, atts );
1257         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1258                 on_start_multitrack( context, name, atts );
1259         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1260                 on_start_playlist( context, name, atts );
1261         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1262                 on_start_producer( context, name, atts );
1263         else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1264                 on_start_blank( context, name, atts );
1265         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1266                 on_start_entry( context, name, atts );
1267         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1268                 on_start_track( context, name, atts );
1269         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1270                 on_start_filter( context, name, atts );
1271         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1272                 on_start_transition( context, name, atts );
1273         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1274                 on_start_property( context, name, atts );
1275         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1276                 on_start_consumer( context, name, atts );
1277         else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1278         {
1279                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1280                 {
1281                         if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1282                                 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1283                         else if ( !context->lc_numeric )
1284                                 context->lc_numeric = strdup( _s( atts[1] ) );
1285                 }
1286         }
1287 }
1288
1289 static void on_end_element( void *ctx, const xmlChar *name )
1290 {
1291         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1292         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1293         
1294         if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1295                 context_pop_node( context );
1296         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1297                 on_end_multitrack( context, name );
1298         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1299                 on_end_playlist( context, name );
1300         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1301                 on_end_track( context, name );
1302         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1303                 on_end_entry( context, name );
1304         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1305                 on_end_tractor( context, name );
1306         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1307                 on_end_property( context, name );
1308         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1309                 on_end_producer( context, name );
1310         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1311                 on_end_filter( context, name );
1312         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1313                 on_end_transition( context, name );
1314         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1315                 on_end_consumer( context, name );
1316
1317         context->branch[ context->depth ] = 0;
1318         context->depth --;
1319 }
1320
1321 static void on_characters( void *ctx, const xmlChar *ch, int len )
1322 {
1323         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1324         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1325         char *value = calloc( 1, len + 1 );
1326         enum service_type type;
1327         mlt_service service = context_pop_service( context, &type );
1328         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1329
1330         if ( service != NULL )
1331                 context_push_service( context, service, type );
1332
1333         value[ len ] = 0;
1334         strncpy( value, (const char*) ch, len );
1335
1336         if ( context->stack_node_size > 0 )
1337                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1338
1339         // libxml2 generates an on_characters immediately after a get_entity within
1340         // an element value, and we ignore it because it is called again during
1341         // actual substitution.
1342         else if ( context->property != NULL && context->entity_is_replace == 0 )
1343         {
1344                 char *s = mlt_properties_get( properties, context->property );
1345                 if ( s != NULL )
1346                 {
1347                         // Append new text to existing content
1348                         char *new = calloc( 1, strlen( s ) + len + 1 );
1349                         strcat( new, s );
1350                         strcat( new, value );
1351                         mlt_properties_set( properties, context->property, new );
1352                         free( new );
1353                 }
1354                 else
1355                         mlt_properties_set( properties, context->property, value );
1356         }
1357         context->entity_is_replace = 0;
1358         
1359         free( value);
1360 }
1361
1362 /** Convert parameters parsed from resource into entity declarations.
1363 */
1364 static void params_to_entities( deserialise_context context )
1365 {
1366         if ( context->params != NULL )
1367         {       
1368                 int i;
1369                 
1370                 // Add our params as entitiy declarations
1371                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1372                 {
1373                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1374                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1375                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1376                 }
1377
1378                 // Flag completion
1379                 mlt_properties_close( context->params );
1380                 context->params = NULL;
1381         }
1382 }
1383
1384 // The following 3 facilitate entity substitution in the SAX parser
1385 static void on_internal_subset( void *ctx, const xmlChar* name,
1386         const xmlChar* publicId, const xmlChar* systemId )
1387 {
1388         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1389         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1390         
1391         context->publicId = publicId;
1392         context->systemId = systemId;
1393         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1394         
1395         // Override default entities with our parameters
1396         params_to_entities( context );
1397 }
1398
1399 // TODO: Check this with Dan... I think this is for parameterisation
1400 // but it's breaking standard escaped entities (like &lt; etc).
1401 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1402         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1403 {
1404         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1405         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1406         
1407         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1408 }
1409
1410 // TODO: Check this functionality (see on_entity_declaration)
1411 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1412 {
1413         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1414         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1415         xmlEntityPtr e = NULL;
1416
1417         // Setup for entity declarations if not ready
1418         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1419         {
1420                 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1421                 context->publicId = _x("");
1422                 context->systemId = _x("");
1423         }
1424
1425         // Add our parameters if not already
1426         params_to_entities( context );
1427         
1428         e = xmlGetPredefinedEntity( name );
1429         
1430         // Send signal to on_characters that an entity substitutin is pending
1431         if ( e == NULL )
1432         {
1433                 e = xmlGetDocEntity( context->entity_doc, name );
1434                 if ( e != NULL )
1435                         context->entity_is_replace = 1;
1436         }
1437         
1438         return e;
1439 }
1440
1441 static void     on_error( void * ctx, const char * msg, ... )
1442 {
1443         struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1444
1445         switch( err_ptr->level )
1446         {
1447         case XML_ERR_WARNING:
1448                 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1449                                          err_ptr->message, err_ptr->line, err_ptr->int2 );
1450                 break;
1451         case XML_ERR_ERROR:
1452                 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1453                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1454                 break;
1455         default:
1456         case XML_ERR_FATAL:
1457                 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1458                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1459                 break;
1460         }
1461 }
1462
1463 /** Convert a hexadecimal character to its value.
1464 */
1465 static int tohex( char p )
1466 {
1467         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1468 }
1469
1470 /** Decode a url-encoded string containing hexadecimal character sequences.
1471 */
1472 static char *url_decode( char *dest, char *src )
1473 {
1474         char *p = dest;
1475         
1476         while ( *src )
1477         {
1478                 if ( *src == '%' )
1479                 {
1480                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1481                         src += 3;
1482                 }
1483                 else
1484                 {
1485                         *p ++ = *src ++;
1486                 }
1487         }
1488
1489         *p = *src;
1490         return dest;
1491 }
1492
1493 /** Extract the filename from a URL attaching parameters to a properties list.
1494 */
1495 static void parse_url( mlt_properties properties, char *url )
1496 {
1497         int i;
1498         int n = strlen( url );
1499         char *name = NULL;
1500         char *value = NULL;
1501         
1502         for ( i = 0; i < n; i++ )
1503         {
1504                 switch ( url[ i ] )
1505                 {
1506                         case '?':
1507                                 url[ i++ ] = '\0';
1508                                 name = &url[ i ];
1509                                 break;
1510                         
1511                         case ':':
1512                         case '=':
1513 #ifdef WIN32
1514                                 if ( url[i] == ':' && url[i + 1] != '/' )
1515                                 {
1516 #endif
1517                                         url[ i++ ] = '\0';
1518                                         value = &url[ i ];
1519 #ifdef WIN32
1520                                 }
1521 #endif
1522                                 break;
1523                         
1524                         case '&':
1525                                 url[ i++ ] = '\0';
1526                                 if ( name != NULL && value != NULL )
1527                                         mlt_properties_set( properties, name, value );
1528                                 name = &url[ i ];
1529                                 value = NULL;
1530                                 break;
1531                 }
1532         }
1533         if ( name != NULL && value != NULL )
1534                 mlt_properties_set( properties, name, value );
1535 }
1536
1537 // Quick workaround to avoid unecessary libxml2 warnings
1538 static int file_exists( char *file )
1539 {
1540         char *name = strdup( file );
1541         int exists = 0;
1542         if ( name != NULL && strchr( name, '?' ) )
1543                 *( strchr( name, '?' ) ) = '\0';
1544         if ( name != NULL )
1545         {
1546                 FILE *f = fopen( name, "r" );
1547                 exists = f != NULL;
1548                 if ( exists ) fclose( f );
1549         }
1550         free( name );
1551         return exists;
1552 }
1553
1554 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1555 {
1556         xmlSAXHandler *sax, *sax_orig;
1557         struct deserialise_context_s *context;
1558         mlt_properties properties = NULL;
1559         int i = 0;
1560         struct _xmlParserCtxt *xmlcontext;
1561         int well_formed = 0;
1562         char *filename = NULL;
1563         int is_filename = strcmp( id, "xml-string" );
1564
1565         // Strip file:// prefix
1566         if ( data && strlen( data ) >= 7 && strncmp( data, "file://", 7 ) == 0 )
1567                 data += 7;
1568
1569         if ( data == NULL || !strcmp( data, "" ) || ( is_filename && !file_exists( data ) ) )
1570                 return NULL;
1571
1572         context = calloc( 1, sizeof( struct deserialise_context_s ) );
1573         if ( context == NULL )
1574                 return NULL;
1575
1576         context->producer_map = mlt_properties_new();
1577         context->destructors = mlt_properties_new();
1578         context->params = mlt_properties_new();
1579         context->profile = profile;
1580         context->seekable = 1;
1581
1582         // Decode URL and parse parameters
1583         mlt_properties_set( context->producer_map, "root", "" );
1584         if ( is_filename )
1585         {
1586                 filename = strdup( data );
1587                 parse_url( context->params, url_decode( filename, data ) );
1588
1589                 // We need the directory prefix which was used for the xml
1590                 if ( strchr( filename, '/' ) )
1591                 {
1592                         char *root = NULL;
1593                         mlt_properties_set( context->producer_map, "root", filename );
1594                         root = mlt_properties_get( context->producer_map, "root" );
1595                         *( strrchr( root, '/' ) ) = '\0';
1596
1597                         // If we don't have an absolute path here, we're heading for disaster...
1598                         if ( root[ 0 ] != '/' )
1599                         {
1600                                 char *cwd = getcwd( NULL, 0 );
1601                                 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1602                                 sprintf( real, "%s/%s", cwd, root );
1603                                 mlt_properties_set( context->producer_map, "root", real );
1604                                 free( real );
1605                                 free( cwd );
1606                         }
1607                 }
1608         }
1609
1610         // We need to track the number of registered filters
1611         mlt_properties_set_int( context->destructors, "registered", 0 );
1612
1613         // Setup SAX callbacks for first pass
1614         sax = calloc( 1, sizeof( xmlSAXHandler ) );
1615         sax->startElement = on_start_element;
1616         sax->warning = on_error;
1617         sax->error = on_error;
1618         sax->fatalError = on_error;
1619
1620         // Setup libxml2 SAX parsing
1621         xmlInitParser(); 
1622         xmlSubstituteEntitiesDefault( 1 );
1623         // This is used to facilitate entity substitution in the SAX parser
1624         context->entity_doc = xmlNewDoc( _x("1.0") );
1625         if ( is_filename )
1626                 xmlcontext = xmlCreateFileParserCtxt( filename );
1627         else
1628                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1629
1630         // Invalid context - clean up and return NULL
1631         if ( xmlcontext == NULL )
1632         {
1633                 mlt_properties_close( context->producer_map );
1634                 mlt_properties_close( context->destructors );
1635                 mlt_properties_close( context->params );
1636                 free( context );
1637                 free( sax );
1638                 free( filename );
1639                 return NULL;
1640         }
1641
1642         // Parse
1643         sax_orig = xmlcontext->sax;
1644         xmlcontext->sax = sax;
1645         xmlcontext->_private = ( void* )context;        
1646         xmlParseDocument( xmlcontext );
1647         well_formed = xmlcontext->wellFormed;
1648         
1649         // Cleanup after parsing
1650         xmlcontext->sax = sax_orig;
1651         xmlcontext->_private = NULL;
1652         if ( xmlcontext->myDoc )
1653                 xmlFreeDoc( xmlcontext->myDoc );
1654         xmlFreeParserCtxt( xmlcontext );
1655         context->stack_node_size = 0;
1656         context->stack_service_size = 0;
1657
1658         // Bad xml - clean up and return NULL
1659         if ( !well_formed )
1660         {
1661                 mlt_properties_close( context->producer_map );
1662                 mlt_properties_close( context->destructors );
1663                 mlt_properties_close( context->params );
1664                 xmlFreeDoc( context->entity_doc );
1665                 free( context );
1666                 free( sax );
1667                 free( filename );
1668                 return NULL;
1669         }
1670
1671         // Setup the second pass
1672         context->pass ++;
1673         if ( is_filename )
1674                 xmlcontext = xmlCreateFileParserCtxt( filename );
1675         else
1676                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1677
1678         // Invalid context - clean up and return NULL
1679         if ( xmlcontext == NULL )
1680         {
1681                 mlt_properties_close( context->producer_map );
1682                 mlt_properties_close( context->destructors );
1683                 mlt_properties_close( context->params );
1684                 xmlFreeDoc( context->entity_doc );
1685                 free( context );
1686                 free( sax );
1687                 free( filename );
1688                 return NULL;
1689         }
1690
1691         // Setup SAX callbacks for second pass
1692         sax->endElement = on_end_element;
1693         sax->characters = on_characters;
1694         sax->cdataBlock = on_characters;
1695         sax->internalSubset = on_internal_subset;
1696         sax->entityDecl = on_entity_declaration;
1697         sax->getEntity = on_get_entity;
1698
1699         // Parse
1700         sax_orig = xmlcontext->sax;
1701         xmlcontext->sax = sax;
1702         xmlcontext->_private = ( void* )context;
1703         xmlParseDocument( xmlcontext );
1704         well_formed = xmlcontext->wellFormed;
1705
1706         // Cleanup after parsing
1707         xmlFreeDoc( context->entity_doc );
1708         free( sax );
1709         xmlMemoryDump( ); // for debugging
1710         xmlcontext->sax = sax_orig;
1711         xmlcontext->_private = NULL;
1712         if ( xmlcontext->myDoc )
1713                 xmlFreeDoc( xmlcontext->myDoc );
1714         xmlFreeParserCtxt( xmlcontext );
1715
1716         // Get the last producer on the stack
1717         enum service_type type;
1718         mlt_service service = context_pop_service( context, &type );
1719         if ( well_formed && service != NULL )
1720         {
1721                 // Verify it is a producer service (mlt_type="mlt_producer")
1722                 // (producer, playlist, multitrack)
1723                 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1724                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1725                         service = NULL;
1726         }
1727
1728 #ifdef DEBUG
1729         xmlDocPtr doc = xml_make_doc( service );
1730         xmlDocFormatDump( stdout, doc, 1 );
1731         xmlFreeDoc( doc );
1732         service = NULL;
1733 #endif
1734         
1735         if ( well_formed && service != NULL )
1736         {
1737                 char *title = mlt_properties_get( context->producer_map, "title" );
1738                 
1739                 // Need the complete producer list for various reasons
1740                 properties = context->destructors;
1741
1742                 // Now make sure we don't have a reference to the service in the properties
1743                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1744                 {
1745                         char *name = mlt_properties_get_name( properties, i );
1746                         if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1747                         {
1748                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1749                                 break;
1750                         }
1751                 }
1752
1753                 // We are done referencing destructor property list
1754                 // Set this var to service properties for convenience
1755                 properties = MLT_SERVICE_PROPERTIES( service );
1756         
1757                 // Assign the title
1758                 mlt_properties_set( properties, "title", title );
1759
1760                 // Optimise for overlapping producers
1761                 mlt_producer_optimise( MLT_PRODUCER( service ) );
1762
1763                 // Handle deep copies
1764                 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1765                 {
1766                         // Now assign additional properties
1767                         if ( is_filename && (
1768                                 mlt_service_identify( service ) == tractor_type ||
1769                                 mlt_service_identify( service ) == playlist_type ||
1770                                 mlt_service_identify( service ) == multitrack_type ) )
1771                         {
1772                                 mlt_properties_set_int( properties, "_original_type",
1773                                         mlt_service_identify( service ) );
1774                                 mlt_properties_set( properties, "_original_resource",
1775                                         mlt_properties_get( properties, "resource" ) );
1776                                 mlt_properties_set( properties, "resource", data );
1777                         }
1778
1779                         // This tells consumer_xml not to deep copy
1780                         mlt_properties_set( properties, "xml", "was here" );
1781                 }
1782                 else
1783                 {
1784                         // Allow the project to be edited
1785                         mlt_properties_set( properties, "_xml", "was here" );
1786                         mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1787                 }
1788
1789                 // Make consumer available
1790                 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1791                 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1792                         (mlt_destructor) mlt_consumer_close, NULL );
1793
1794                 mlt_properties_set_int( properties, "seekable", context->seekable );
1795         }
1796         else
1797         {
1798                 // Return null if not well formed
1799                 service = NULL;
1800         }
1801
1802         // Clean up
1803         mlt_properties_close( context->producer_map );
1804         if ( context->params != NULL )
1805                 mlt_properties_close( context->params );
1806         mlt_properties_close( context->destructors );
1807         if ( context->lc_numeric )
1808                 free( context->lc_numeric );
1809         free( context );
1810         free( filename );
1811
1812         return MLT_PRODUCER( service );
1813 }