]> git.sesse.net Git - mlt/blob - src/modules/xml/producer_xml.c
Let melt and xml producer use qglsl consumer (opengl branch).
[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 = 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 = trim( 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 = trim( 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                 context->seekable &= mlt_properties_get_int( MLT_SERVICE_PROPERTIES( producer ), "seekable" );
594
595                 // Propagate the properties
596                 qualify_property( context, properties, "resource" );
597                 qualify_property( context, properties, "luma" );
598                 qualify_property( context, properties, "luma.resource" );
599                 qualify_property( context, properties, "composite.luma" );
600                 qualify_property( context, properties, "producer.resource" );
601
602                 // Handle in/out properties separately
603                 mlt_position in = -1;
604                 mlt_position out = -1;
605         
606                 // Get in
607                 if ( mlt_properties_get( properties, "in" ) )
608                         in = mlt_properties_get_position( properties, "in" );
609                 // Let Kino-SMIL clipBegin be a synonym for in
610                 else if ( mlt_properties_get( properties, "clipBegin" ) )
611                         in = mlt_properties_get_position( properties, "clipBegin" );
612                 // Get out
613                 if ( mlt_properties_get( properties, "out" ) )
614                         out = mlt_properties_get_position( properties, "out" );
615                 // Let Kino-SMIL clipEnd be a synonym for out
616                 else if ( mlt_properties_get( properties, "clipEnd" ) )
617                         out = mlt_properties_get_position( properties, "clipEnd" );
618                 // Remove in and out
619                 mlt_properties_set( properties, "in", NULL );
620                 mlt_properties_set( properties, "out", NULL );
621
622                 // Inherit the properties
623                 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
624
625                 // Attach all filters from service onto producer
626                 attach_filters( producer, service );
627
628                 // Add the producer to the producer map
629                 if ( mlt_properties_get( properties, "id" ) != NULL )
630                         mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
631
632                 // See if the producer should be added to a playlist or multitrack
633                 if ( add_producer( context, producer, in, out ) == 0 )
634                 {
635                         // Otherwise, set in and out on...
636                         if ( in != -1 || out != -1 )
637                         {
638                                 // Get the parent service
639                                 enum service_type type;
640                                 mlt_service parent = context_pop_service( context, &type );
641                                 if ( parent != NULL )
642                                 {
643                                         // Get the parent properties
644                                         properties = MLT_SERVICE_PROPERTIES( parent );
645                                 
646                                         char *resource = trim( mlt_properties_get( properties, "resource" ) );
647                                 
648                                         // Put the parent producer back
649                                         context_push_service( context, parent, type );
650                                         
651                                         // If the parent is a track or entry
652                                         if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
653                                         {
654                                                 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
655                                                 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
656                                         }
657                                         else
658                                         {
659                                                 // Otherwise, set in and out on producer directly
660                                                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
661                                         }
662                                 }
663                                 else
664                                 {
665                                         // Otherwise, set in and out on producer directly
666                                         mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
667                                 }
668                         }
669
670                         // Push the producer onto the stack
671                         context_push_service( context, producer, mlt_producer_type );
672                 }
673         }
674
675         if ( service )
676         {
677                 mlt_service_close( service );
678                 free( service );
679         }
680 }
681
682 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
683 {
684         // Get the playlist from the stack
685         enum service_type type;
686         mlt_service service = context_pop_service( context, &type );
687         
688         if ( type == mlt_playlist_type && service != NULL )
689         {
690                 // Look for the length attribute
691                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
692                 {
693                         if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
694                         {
695                                 // Append a blank to the playlist
696                                 mlt_playlist_blank_time( MLT_PLAYLIST( service ), _s(atts[1]) );
697                                 break;
698                         }
699                 }
700
701                 // Push the playlist back onto the stack
702                 context_push_service( context, service, type );
703         }
704         else
705         {
706                 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
707         }
708 }
709
710 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
711 {
712         mlt_producer entry = NULL;
713         mlt_properties temp = mlt_properties_new( );
714         mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
715         mlt_properties_set_lcnumeric( temp, context->lc_numeric );
716
717         for ( ; atts != NULL && *atts != NULL; atts += 2 )
718         {
719                 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
720                 
721                 // Look for the producer attribute
722                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
723                 {
724                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
725                         if ( producer !=  NULL )
726                                 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
727                 }
728         }
729
730         // If we have a valid entry
731         if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
732         {
733                 mlt_playlist_clip_info info;
734                 enum service_type parent_type = invalid_type;
735                 mlt_service parent = context_pop_service( context, &parent_type );
736                 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
737
738                 if ( parent_type == mlt_playlist_type )
739                 {
740                         // Append the producer to the playlist
741                         mlt_position in = -1;
742                         mlt_position out = -1;
743                         if ( mlt_properties_get( temp, "in" ) )
744                                 in = mlt_properties_get_position( temp, "in" );
745                         if ( mlt_properties_get( temp, "out" ) )
746                                 out = mlt_properties_get_position( temp, "out" );
747                         mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
748
749                         // Handle the repeat property
750                         if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
751                         {
752                                 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
753                                                                                   mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
754                                                                                   mlt_properties_get_int( temp, "repeat" ) );
755                         }
756
757                         mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
758                         entry = info.cut;
759                 }
760                 else
761                 {
762                         mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
763                 }
764
765                 context_push_service( context, parent, parent_type );
766         }
767
768         // Push the cut onto the stack
769         context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
770
771         mlt_properties_close( temp );
772 }
773
774 static void on_end_entry( deserialise_context context, const xmlChar *name )
775 {
776         // Get the entry from the stack
777         enum service_type entry_type = invalid_type;
778         mlt_service entry = context_pop_service( context, &entry_type );
779
780         if ( entry == NULL && entry_type != mlt_entry_type )
781         {
782                 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
783         }
784 }
785
786 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
787 {
788         // use a dummy service to hold properties to allow arbitrary nesting
789         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
790         mlt_service_init( service, NULL );
791
792         // Push the dummy service onto the stack
793         context_push_service( context, service, mlt_entry_type );
794         
795         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
796         
797         for ( ; atts != NULL && *atts != NULL; atts += 2 )
798         {
799                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
800                 
801                 // Look for the producer attribute
802                 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
803                 {
804                         mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
805                         if ( producer !=  NULL )
806                                 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
807                 }
808         }
809 }
810
811 static void on_end_track( deserialise_context context, const xmlChar *name )
812 {
813         // Get the track from the stack
814         enum service_type track_type;
815         mlt_service track = context_pop_service( context, &track_type );
816
817         if ( track != NULL && track_type == mlt_entry_type )
818         {
819                 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
820                 enum service_type parent_type = invalid_type;
821                 mlt_service parent = context_pop_service( context, &parent_type );
822                 mlt_multitrack multitrack = NULL;
823
824                 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
825                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
826
827                 if ( parent_type == mlt_tractor_type )
828                         multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
829                 else if ( parent_type == mlt_multitrack_type )
830                         multitrack = MLT_MULTITRACK( parent );
831                 else
832                         mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
833
834                 if ( multitrack != NULL )
835                 {
836                         // Set producer i/o if specified
837                         if ( mlt_properties_get( track_props, "in" ) != NULL ||
838                                  mlt_properties_get( track_props, "out" ) != NULL )
839                         {
840                                 mlt_position in = -1;
841                                 mlt_position out = -1;
842                                 if ( mlt_properties_get( track_props, "in" ) )
843                                         in = mlt_properties_get_position( track_props, "in" );
844                                 if ( mlt_properties_get( track_props, "out" ) )
845                                         out = mlt_properties_get_position( track_props, "out" );
846                                 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
847                                 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
848                                 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
849                                 track_props = MLT_PRODUCER_PROPERTIES( cut );
850                                 mlt_producer_close( cut );
851                         }
852                         else
853                         {
854                                 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
855                         }
856
857                         // Set the hide state of the track producer
858                         char *hide_s = mlt_properties_get( track_props, "hide" );
859                         if ( hide_s != NULL )
860                         {
861                                 if ( strcmp( hide_s, "video" ) == 0 )
862                                         mlt_properties_set_int( producer_props, "hide", 1 );
863                                 else if ( strcmp( hide_s, "audio" ) == 0 )
864                                         mlt_properties_set_int( producer_props, "hide", 2 );
865                                 else if ( strcmp( hide_s, "both" ) == 0 )
866                                         mlt_properties_set_int( producer_props, "hide", 3 );
867                         }
868                 }
869
870                 if ( parent != NULL )
871                         context_push_service( context, parent, parent_type );
872         }
873         else
874         {
875                 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
876         }
877
878         if ( track )
879         {
880                 mlt_service_close( track );
881                 free( track );
882         }
883 }
884
885 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
886 {
887         // use a dummy service to hold properties to allow arbitrary nesting
888         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
889         mlt_service_init( service, NULL );
890
891         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
892
893         context_push_service( context, service, mlt_dummy_filter_type );
894
895         // Set the properties
896         for ( ; atts != NULL && *atts != NULL; atts += 2 )
897                 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
898 }
899
900 static void on_end_filter( deserialise_context context, const xmlChar *name )
901 {
902         enum service_type type;
903         mlt_service service = context_pop_service( context, &type );
904         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
905
906         enum service_type parent_type = invalid_type;
907         mlt_service parent = context_pop_service( context, &parent_type );
908
909         if ( service != NULL && type == mlt_dummy_filter_type )
910         {
911                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
912                 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
913                 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
914
915                 if ( !filter )
916                 {
917                         mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
918                         if ( parent )
919                                 context_push_service( context, parent, parent_type );
920                         mlt_service_close( service );
921                         free( service );
922                         return;
923                 }
924
925                 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
926                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
927
928                 // Propogate the properties
929                 qualify_property( context, properties, "resource" );
930                 qualify_property( context, properties, "luma" );
931                 qualify_property( context, properties, "luma.resource" );
932                 qualify_property( context, properties, "composite.luma" );
933                 qualify_property( context, properties, "producer.resource" );
934                 mlt_properties_inherit( filter_props, properties );
935
936                 // Attach all filters from service onto filter
937                 attach_filters( filter, service );
938
939                 // Associate the filter with the parent
940                 if ( parent != NULL )
941                 {
942                         if ( parent_type == mlt_tractor_type )
943                         {
944                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
945                                 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
946                                 mlt_filter_set_in_and_out( MLT_FILTER( filter ), 
947                                                                                    mlt_properties_get_int( properties, "in" ),
948                                                                                    mlt_properties_get_int( properties, "out" ) );
949                         }
950                         else
951                         {
952                                 mlt_service_attach( parent, MLT_FILTER( filter ) );
953                         }
954
955                         // Put the parent back on the stack
956                         context_push_service( context, parent, parent_type );
957                 }
958                 else
959                 {
960                         mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
961                 }
962         }
963         else
964         {
965                 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
966         }
967
968         if ( service )
969         {
970                 mlt_service_close( service );
971                 free(service);
972         }
973 }
974
975 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
976 {
977         // use a dummy service to hold properties to allow arbitrary nesting
978         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
979         mlt_service_init( service, NULL );
980
981         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
982
983         context_push_service( context, service, mlt_dummy_transition_type );
984
985         // Set the properties
986         for ( ; atts != NULL && *atts != NULL; atts += 2 )
987                 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
988 }
989
990 static void on_end_transition( deserialise_context context, const xmlChar *name )
991 {
992         enum service_type type;
993         mlt_service service = context_pop_service( context, &type );
994         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
995
996         enum service_type parent_type = invalid_type;
997         mlt_service parent = context_pop_service( context, &parent_type );
998
999         if ( service != NULL && type == mlt_dummy_transition_type )
1000         {
1001                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1002                 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
1003                 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
1004
1005                 if ( !effect )
1006                 {
1007                         mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
1008                         if ( parent )
1009                                 context_push_service( context, parent, parent_type );
1010                         mlt_service_close( service );
1011                         free( service );
1012                         return;
1013                 }
1014                 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1015                 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1016
1017                 // Propogate the properties
1018                 qualify_property( context, properties, "resource" );
1019                 qualify_property( context, properties, "luma" );
1020                 qualify_property( context, properties, "luma.resource" );
1021                 qualify_property( context, properties, "composite.luma" );
1022                 qualify_property( context, properties, "producer.resource" );
1023                 mlt_properties_inherit( effect_props, properties );
1024
1025                 // Attach all filters from service onto effect
1026                 attach_filters( effect, service );
1027
1028                 // Associate the filter with the parent
1029                 if ( parent != NULL )
1030                 {
1031                         if ( parent_type == mlt_tractor_type )
1032                         {
1033                                 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1034                                 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1035                                         mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1036                                 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1037                                                                                         mlt_properties_get_int( properties, "a_track" ),
1038                                                                                         mlt_properties_get_int( properties, "b_track" ) );
1039                                 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1040                                                                                    mlt_properties_get_int( properties, "in" ),
1041                                                                                    mlt_properties_get_int( properties, "out" ) );
1042                         }
1043                         else
1044                         {
1045                                 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1046                         }
1047
1048                         // Put the parent back on the stack
1049                         context_push_service( context, parent, parent_type );
1050                 }
1051                 else
1052                 {
1053                         mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1054                 }
1055
1056         }
1057         else
1058         {
1059                 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1060         }
1061
1062         if ( service )
1063         {
1064                 mlt_service_close( service );
1065                 free( service );
1066         }
1067 }
1068
1069 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1070 {
1071         if ( context->pass == 1 )
1072         {
1073                 mlt_properties properties = mlt_properties_new();
1074
1075                 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1076                 context_push_service( context, (mlt_service) properties, 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_properties properties = (mlt_properties) context_pop_service( context, &type );
1091
1092                 if ( properties && type == mlt_dummy_consumer_type )
1093                 {
1094                         qualify_property( context, properties, "resource" );
1095                         qualify_property( context, properties, "target" );
1096                         char *resource = trim( mlt_properties_get( properties, "resource" ) );
1097
1098                         if ( context->multi_consumer > 1 || context->qglsl )
1099                         {
1100                                 // Instantiate the multi consumer
1101                                 if ( !context->consumer )
1102                                 {
1103                                         if ( context->qglsl )
1104                                                 context->consumer = context->qglsl;
1105                                         else
1106                                                 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1107                                         if ( context->consumer )
1108                                         {
1109                                                 // Track this consumer
1110                                                 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1111                                                 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1112                                         }
1113                                 }
1114                                 if ( context->consumer )
1115                                 {
1116                                         // Set this properties object on multi consumer
1117                                         char key[20];
1118                                         snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1119                                         mlt_properties_inc_ref( properties );
1120                                         mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, properties, 0,
1121                                                 (mlt_destructor) mlt_properties_close, NULL );
1122                                 }
1123                         }
1124                         else
1125                         {
1126                                 // Instantiate the consumer
1127                                 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1128                                 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1129                                 if ( context->consumer )
1130                                 {
1131                                         // Track this consumer
1132                                         track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1133                                         mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1134
1135                                         // Inherit the properties
1136                                         mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1137                                 }
1138                         }
1139                 }
1140                 // Close the dummy
1141                 if ( properties )
1142                         mlt_properties_close( properties );
1143         }
1144 }
1145
1146 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1147 {
1148         enum service_type type;
1149         mlt_service service = context_pop_service( context, &type );
1150         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1151         const char *value = NULL;
1152
1153         if ( service != NULL )
1154         {
1155                 // Set the properties
1156                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1157                 {
1158                         if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1159                                 context->property = strdup( _s(atts[ 1 ]) );
1160                         else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1161                                 value = _s(atts[ 1 ]);
1162                 }
1163
1164                 if ( context->property != NULL )
1165                         mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1166
1167                 // Tell parser to collect any further nodes for serialisation
1168                 context->is_value = 1;
1169
1170                 context_push_service( context, service, type );
1171         }
1172         else
1173         {
1174                 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1175         }
1176 }
1177
1178 static void on_end_property( deserialise_context context, const xmlChar *name )
1179 {
1180         enum service_type type;
1181         mlt_service service = context_pop_service( context, &type );
1182         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1183
1184         if ( service != NULL )
1185         {
1186                 // Tell parser to stop building a tree
1187                 context->is_value = 0;
1188         
1189                 // See if there is a xml tree for the value
1190                 if ( context->property != NULL && context->value_doc != NULL )
1191                 {
1192                         xmlChar *value;
1193                         int size;
1194                 
1195                         // Serialise the tree to get value
1196                         xmlDocDumpMemory( context->value_doc, &value, &size );
1197                         mlt_properties_set( properties, context->property, _s(value) );
1198 #ifdef WIN32
1199                         xmlFreeFunc xmlFree = NULL;
1200                         xmlMemGet( &xmlFree, NULL, NULL, NULL);
1201 #endif
1202                         xmlFree( value );
1203                         xmlFreeDoc( context->value_doc );
1204                         context->value_doc = NULL;
1205                 }
1206
1207                 // Close this property handling
1208                 free( context->property );
1209                 context->property = NULL;
1210
1211                 context_push_service( context, service, type );
1212         }
1213         else
1214         {
1215                 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1216         }
1217 }
1218
1219 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1220 {
1221         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1222         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1223         
1224         if ( context->pass == 0 )
1225         {
1226                 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1227                      xmlStrcmp( name, _x("profile") ) == 0 ||
1228                      xmlStrcmp( name, _x("profileinfo") ) == 0 )
1229                         on_start_profile( context, name, atts );
1230                 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1231                         context->multi_consumer++;
1232
1233                 // Check for a service beginning with glsl. or movit.
1234                 for ( ; atts != NULL && *atts != NULL; atts += 2 ) {
1235                         if ( !xmlStrncmp( atts[1], _x("glsl."), 5 ) || !xmlStrncmp( atts[1], _x("movit."), 6 ) ) {
1236                                 mlt_properties_set_int( context->params, "qglsl", 1 );
1237                                 break;
1238                         }
1239                 }
1240                 return;
1241         }
1242         context->branch[ context->depth ] ++;
1243         context->depth ++;
1244         
1245         // Build a tree from nodes within a property value
1246         if ( context->is_value == 1 && context->pass == 1 )
1247         {
1248                 xmlNodePtr node = xmlNewNode( NULL, name );
1249                 
1250                 if ( context->value_doc == NULL )
1251                 {
1252                         // Start a new tree
1253                         context->value_doc = xmlNewDoc( _x("1.0") );
1254                         xmlDocSetRootElement( context->value_doc, node );
1255                 }
1256                 else
1257                 {
1258                         // Append child to tree
1259                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1260                 }
1261                 context_push_node( context, node );
1262                 
1263                 // Set the attributes
1264                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1265                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1266         }
1267         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1268                 on_start_tractor( context, name, atts );
1269         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1270                 on_start_multitrack( context, name, atts );
1271         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1272                 on_start_playlist( context, name, atts );
1273         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1274                 on_start_producer( context, name, atts );
1275         else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1276                 on_start_blank( context, name, atts );
1277         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1278                 on_start_entry( context, name, atts );
1279         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1280                 on_start_track( context, name, atts );
1281         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1282                 on_start_filter( context, name, atts );
1283         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1284                 on_start_transition( context, name, atts );
1285         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1286                 on_start_property( context, name, atts );
1287         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1288                 on_start_consumer( context, name, atts );
1289         else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1290         {
1291                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1292                 {
1293                         if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1294                                 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1295                         else if ( !context->lc_numeric )
1296                                 context->lc_numeric = strdup( _s( atts[1] ) );
1297                 }
1298         }
1299 }
1300
1301 static void on_end_element( void *ctx, const xmlChar *name )
1302 {
1303         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1304         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1305         
1306         if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1307                 context_pop_node( context );
1308         else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1309                 on_end_multitrack( context, name );
1310         else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1311                 on_end_playlist( context, name );
1312         else if ( xmlStrcmp( name, _x("track") ) == 0 )
1313                 on_end_track( context, name );
1314         else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1315                 on_end_entry( context, name );
1316         else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1317                 on_end_tractor( context, name );
1318         else if ( xmlStrcmp( name, _x("property") ) == 0 )
1319                 on_end_property( context, name );
1320         else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1321                 on_end_producer( context, name );
1322         else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1323                 on_end_filter( context, name );
1324         else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1325                 on_end_transition( context, name );
1326         else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1327                 on_end_consumer( context, name );
1328
1329         context->branch[ context->depth ] = 0;
1330         context->depth --;
1331 }
1332
1333 static void on_characters( void *ctx, const xmlChar *ch, int len )
1334 {
1335         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1336         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1337         char *value = calloc( 1, len + 1 );
1338         enum service_type type;
1339         mlt_service service = context_pop_service( context, &type );
1340         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1341
1342         if ( service != NULL )
1343                 context_push_service( context, service, type );
1344
1345         value[ len ] = 0;
1346         strncpy( value, (const char*) ch, len );
1347
1348         if ( context->stack_node_size > 0 )
1349                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1350
1351         // libxml2 generates an on_characters immediately after a get_entity within
1352         // an element value, and we ignore it because it is called again during
1353         // actual substitution.
1354         else if ( context->property != NULL && context->entity_is_replace == 0 )
1355         {
1356                 char *s = mlt_properties_get( properties, context->property );
1357                 if ( s != NULL )
1358                 {
1359                         // Append new text to existing content
1360                         char *new = calloc( 1, strlen( s ) + len + 1 );
1361                         strcat( new, s );
1362                         strcat( new, value );
1363                         mlt_properties_set( properties, context->property, new );
1364                         free( new );
1365                 }
1366                 else
1367                         mlt_properties_set( properties, context->property, value );
1368         }
1369         context->entity_is_replace = 0;
1370
1371         // Check for a service beginning with glsl. or movit.
1372         if ( !strncmp( value, "glsl.", 5 ) || !strncmp( value, "movit.", 6 ) )
1373                 mlt_properties_set_int( context->params, "qglsl", 1 );
1374
1375         free( value);
1376 }
1377
1378 /** Convert parameters parsed from resource into entity declarations.
1379 */
1380 static void params_to_entities( deserialise_context context )
1381 {
1382         if ( context->params != NULL )
1383         {       
1384                 int i;
1385                 
1386                 // Add our params as entitiy declarations
1387                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1388                 {
1389                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1390                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1391                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1392                 }
1393
1394                 // Flag completion
1395                 mlt_properties_close( context->params );
1396                 context->params = NULL;
1397         }
1398 }
1399
1400 // The following 3 facilitate entity substitution in the SAX parser
1401 static void on_internal_subset( void *ctx, const xmlChar* name,
1402         const xmlChar* publicId, const xmlChar* systemId )
1403 {
1404         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1405         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1406         
1407         context->publicId = publicId;
1408         context->systemId = systemId;
1409         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1410         
1411         // Override default entities with our parameters
1412         params_to_entities( context );
1413 }
1414
1415 // TODO: Check this with Dan... I think this is for parameterisation
1416 // but it's breaking standard escaped entities (like &lt; etc).
1417 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1418         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1419 {
1420         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1421         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1422         
1423         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1424 }
1425
1426 // TODO: Check this functionality (see on_entity_declaration)
1427 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1428 {
1429         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1430         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1431         xmlEntityPtr e = NULL;
1432
1433         // Setup for entity declarations if not ready
1434         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1435         {
1436                 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1437                 context->publicId = _x("");
1438                 context->systemId = _x("");
1439         }
1440
1441         // Add our parameters if not already
1442         params_to_entities( context );
1443         
1444         e = xmlGetPredefinedEntity( name );
1445         
1446         // Send signal to on_characters that an entity substitutin is pending
1447         if ( e == NULL )
1448         {
1449                 e = xmlGetDocEntity( context->entity_doc, name );
1450                 if ( e != NULL )
1451                         context->entity_is_replace = 1;
1452         }
1453         
1454         return e;
1455 }
1456
1457 static void     on_error( void * ctx, const char * msg, ... )
1458 {
1459         struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1460
1461         switch( err_ptr->level )
1462         {
1463         case XML_ERR_WARNING:
1464                 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1465                                          err_ptr->message, err_ptr->line, err_ptr->int2 );
1466                 break;
1467         case XML_ERR_ERROR:
1468                 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1469                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1470                 break;
1471         default:
1472         case XML_ERR_FATAL:
1473                 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1474                                        err_ptr->message, err_ptr->line, err_ptr->int2 );
1475                 break;
1476         }
1477 }
1478
1479 /** Convert a hexadecimal character to its value.
1480 */
1481 static int tohex( char p )
1482 {
1483         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1484 }
1485
1486 /** Decode a url-encoded string containing hexadecimal character sequences.
1487 */
1488 static char *url_decode( char *dest, char *src )
1489 {
1490         char *p = dest;
1491         
1492         while ( *src )
1493         {
1494                 if ( *src == '%' )
1495                 {
1496                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1497                         src += 3;
1498                 }
1499                 else
1500                 {
1501                         *p ++ = *src ++;
1502                 }
1503         }
1504
1505         *p = *src;
1506         return dest;
1507 }
1508
1509 /** Extract the filename from a URL attaching parameters to a properties list.
1510 */
1511 static void parse_url( mlt_properties properties, char *url )
1512 {
1513         int i;
1514         int n = strlen( url );
1515         char *name = NULL;
1516         char *value = NULL;
1517         
1518         for ( i = 0; i < n; i++ )
1519         {
1520                 switch ( url[ i ] )
1521                 {
1522                         case '?':
1523                                 url[ i++ ] = '\0';
1524                                 name = &url[ i ];
1525                                 break;
1526                         
1527                         case ':':
1528                         case '=':
1529 #ifdef WIN32
1530                                 if ( url[i] == ':' && url[i + 1] != '/' )
1531                                 {
1532 #endif
1533                                         url[ i++ ] = '\0';
1534                                         value = &url[ i ];
1535 #ifdef WIN32
1536                                 }
1537 #endif
1538                                 break;
1539                         
1540                         case '&':
1541                                 url[ i++ ] = '\0';
1542                                 if ( name != NULL && value != NULL )
1543                                         mlt_properties_set( properties, name, value );
1544                                 name = &url[ i ];
1545                                 value = NULL;
1546                                 break;
1547                 }
1548         }
1549         if ( name != NULL && value != NULL )
1550                 mlt_properties_set( properties, name, value );
1551 }
1552
1553 // Quick workaround to avoid unecessary libxml2 warnings
1554 static int file_exists( char *file )
1555 {
1556         char *name = strdup( file );
1557         int exists = 0;
1558         if ( name != NULL && strchr( name, '?' ) )
1559                 *( strchr( name, '?' ) ) = '\0';
1560         if ( name != NULL )
1561         {
1562                 FILE *f = fopen( name, "r" );
1563                 exists = f != NULL;
1564                 if ( exists ) fclose( f );
1565         }
1566         free( name );
1567         return exists;
1568 }
1569
1570 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1571 {
1572         xmlSAXHandler *sax, *sax_orig;
1573         struct deserialise_context_s *context;
1574         mlt_properties properties = NULL;
1575         int i = 0;
1576         struct _xmlParserCtxt *xmlcontext;
1577         int well_formed = 0;
1578         char *filename = NULL;
1579         int is_filename = strcmp( id, "xml-string" );
1580
1581         // Strip file:// prefix
1582         if ( data && strlen( data ) >= 7 && strncmp( data, "file://", 7 ) == 0 )
1583                 data += 7;
1584
1585         if ( data == NULL || !strcmp( data, "" ) || ( is_filename && !file_exists( data ) ) )
1586                 return NULL;
1587
1588         context = calloc( 1, sizeof( struct deserialise_context_s ) );
1589         if ( context == NULL )
1590                 return NULL;
1591
1592         context->producer_map = mlt_properties_new();
1593         context->destructors = mlt_properties_new();
1594         context->params = mlt_properties_new();
1595         context->profile = profile;
1596         context->seekable = 1;
1597
1598         // Decode URL and parse parameters
1599         mlt_properties_set( context->producer_map, "root", "" );
1600         if ( is_filename )
1601         {
1602                 filename = strdup( data );
1603                 parse_url( context->params, url_decode( filename, data ) );
1604
1605                 // We need the directory prefix which was used for the xml
1606                 if ( strchr( filename, '/' ) )
1607                 {
1608                         char *root = NULL;
1609                         mlt_properties_set( context->producer_map, "root", filename );
1610                         root = mlt_properties_get( context->producer_map, "root" );
1611                         *( strrchr( root, '/' ) ) = '\0';
1612
1613                         // If we don't have an absolute path here, we're heading for disaster...
1614                         if ( root[ 0 ] != '/' )
1615                         {
1616                                 char *cwd = getcwd( NULL, 0 );
1617                                 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1618                                 sprintf( real, "%s/%s", cwd, root );
1619                                 mlt_properties_set( context->producer_map, "root", real );
1620                                 free( real );
1621                                 free( cwd );
1622                         }
1623                 }
1624         }
1625
1626         // We need to track the number of registered filters
1627         mlt_properties_set_int( context->destructors, "registered", 0 );
1628
1629         // Setup SAX callbacks for first pass
1630         sax = calloc( 1, sizeof( xmlSAXHandler ) );
1631         sax->startElement = on_start_element;
1632         sax->characters = on_characters;
1633         sax->warning = on_error;
1634         sax->error = on_error;
1635         sax->fatalError = on_error;
1636
1637         // Setup libxml2 SAX parsing
1638         xmlInitParser(); 
1639         xmlSubstituteEntitiesDefault( 1 );
1640         // This is used to facilitate entity substitution in the SAX parser
1641         context->entity_doc = xmlNewDoc( _x("1.0") );
1642         if ( is_filename )
1643                 xmlcontext = xmlCreateFileParserCtxt( filename );
1644         else
1645                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1646
1647         // Invalid context - clean up and return NULL
1648         if ( xmlcontext == NULL )
1649         {
1650                 mlt_properties_close( context->producer_map );
1651                 mlt_properties_close( context->destructors );
1652                 mlt_properties_close( context->params );
1653                 free( context );
1654                 free( sax );
1655                 free( filename );
1656                 return NULL;
1657         }
1658
1659         // Parse
1660         sax_orig = xmlcontext->sax;
1661         xmlcontext->sax = sax;
1662         xmlcontext->_private = ( void* )context;        
1663         xmlParseDocument( xmlcontext );
1664         well_formed = xmlcontext->wellFormed;
1665         
1666         // Cleanup after parsing
1667         xmlcontext->sax = sax_orig;
1668         xmlcontext->_private = NULL;
1669         if ( xmlcontext->myDoc )
1670                 xmlFreeDoc( xmlcontext->myDoc );
1671         xmlFreeParserCtxt( xmlcontext );
1672         context->stack_node_size = 0;
1673         context->stack_service_size = 0;
1674
1675         // Bad xml - clean up and return NULL
1676         if ( !well_formed )
1677         {
1678                 mlt_properties_close( context->producer_map );
1679                 mlt_properties_close( context->destructors );
1680                 mlt_properties_close( context->params );
1681                 xmlFreeDoc( context->entity_doc );
1682                 free( context );
1683                 free( sax );
1684                 free( filename );
1685                 return NULL;
1686         }
1687
1688         // Setup the second pass
1689         context->pass ++;
1690         if ( is_filename )
1691                 xmlcontext = xmlCreateFileParserCtxt( filename );
1692         else
1693                 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1694
1695         // Invalid context - clean up and return NULL
1696         if ( xmlcontext == NULL )
1697         {
1698                 mlt_properties_close( context->producer_map );
1699                 mlt_properties_close( context->destructors );
1700                 mlt_properties_close( context->params );
1701                 xmlFreeDoc( context->entity_doc );
1702                 free( context );
1703                 free( sax );
1704                 free( filename );
1705                 return NULL;
1706         }
1707
1708         // Create the qglsl consumer now, if requested, so that glsl.manager
1709         // may exist when trying to load glsl. or movit. services.
1710         // The "if requested" part can come from query string qglsl=1 or when
1711         // a service beginning with glsl. or movit. appears in the XML.
1712         if ( mlt_properties_get_int( context->params, "qglsl" ) )
1713                 context->qglsl = mlt_factory_consumer( profile, "qglsl", NULL );
1714
1715         // Setup SAX callbacks for second pass
1716         sax->endElement = on_end_element;
1717         sax->cdataBlock = on_characters;
1718         sax->internalSubset = on_internal_subset;
1719         sax->entityDecl = on_entity_declaration;
1720         sax->getEntity = on_get_entity;
1721
1722         // Parse
1723         sax_orig = xmlcontext->sax;
1724         xmlcontext->sax = sax;
1725         xmlcontext->_private = ( void* )context;
1726         xmlParseDocument( xmlcontext );
1727         well_formed = xmlcontext->wellFormed;
1728
1729         // Cleanup after parsing
1730         xmlFreeDoc( context->entity_doc );
1731         free( sax );
1732         xmlMemoryDump( ); // for debugging
1733         xmlcontext->sax = sax_orig;
1734         xmlcontext->_private = NULL;
1735         if ( xmlcontext->myDoc )
1736                 xmlFreeDoc( xmlcontext->myDoc );
1737         xmlFreeParserCtxt( xmlcontext );
1738
1739         // Get the last producer on the stack
1740         enum service_type type;
1741         mlt_service service = context_pop_service( context, &type );
1742         if ( well_formed && service != NULL )
1743         {
1744                 // Verify it is a producer service (mlt_type="mlt_producer")
1745                 // (producer, playlist, multitrack)
1746                 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1747                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1748                         service = NULL;
1749         }
1750
1751 #ifdef DEBUG
1752         xmlDocPtr doc = xml_make_doc( service );
1753         xmlDocFormatDump( stdout, doc, 1 );
1754         xmlFreeDoc( doc );
1755         service = NULL;
1756 #endif
1757         
1758         if ( well_formed && service != NULL )
1759         {
1760                 char *title = mlt_properties_get( context->producer_map, "title" );
1761                 
1762                 // Need the complete producer list for various reasons
1763                 properties = context->destructors;
1764
1765                 // Now make sure we don't have a reference to the service in the properties
1766                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1767                 {
1768                         char *name = mlt_properties_get_name( properties, i );
1769                         if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1770                         {
1771                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1772                                 break;
1773                         }
1774                 }
1775
1776                 // We are done referencing destructor property list
1777                 // Set this var to service properties for convenience
1778                 properties = MLT_SERVICE_PROPERTIES( service );
1779         
1780                 // Assign the title
1781                 mlt_properties_set( properties, "title", title );
1782
1783                 // Optimise for overlapping producers
1784                 mlt_producer_optimise( MLT_PRODUCER( service ) );
1785
1786                 // Handle deep copies
1787                 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1788                 {
1789                         // Now assign additional properties
1790                         if ( is_filename && (
1791                                 mlt_service_identify( service ) == tractor_type ||
1792                                 mlt_service_identify( service ) == playlist_type ||
1793                                 mlt_service_identify( service ) == multitrack_type ) )
1794                         {
1795                                 mlt_properties_set_int( properties, "_original_type",
1796                                         mlt_service_identify( service ) );
1797                                 mlt_properties_set( properties, "_original_resource",
1798                                         mlt_properties_get( properties, "resource" ) );
1799                                 mlt_properties_set( properties, "resource", data );
1800                         }
1801
1802                         // This tells consumer_xml not to deep copy
1803                         mlt_properties_set( properties, "xml", "was here" );
1804                 }
1805                 else
1806                 {
1807                         // Allow the project to be edited
1808                         mlt_properties_set( properties, "_xml", "was here" );
1809                         mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1810                 }
1811
1812                 // Make consumer available
1813                 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1814                 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1815                         (mlt_destructor) mlt_consumer_close, NULL );
1816
1817                 mlt_properties_set_int( properties, "seekable", context->seekable );
1818         }
1819         else
1820         {
1821                 // Return null if not well formed
1822                 service = NULL;
1823         }
1824
1825         // Clean up
1826         if ( context->qglsl && context->consumer != context->qglsl )
1827                 mlt_consumer_close( context->qglsl );
1828         mlt_properties_close( context->producer_map );
1829         if ( context->params != NULL )
1830                 mlt_properties_close( context->params );
1831         mlt_properties_close( context->destructors );
1832         if ( context->lc_numeric )
1833                 free( context->lc_numeric );
1834         free( context );
1835         free( filename );
1836
1837         return MLT_PRODUCER( service );
1838 }