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