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>
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.
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.
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
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 // when the returned producer is closed).
24 #include <framework/mlt.h>
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
38 #define _x (const xmlChar*)
39 #define _s (const char*)
43 extern xmlDocPtr xml_make_doc( mlt_service service );
60 mlt_dummy_filter_type,
61 mlt_dummy_transition_type,
62 mlt_dummy_producer_type,
65 struct deserialise_context_s
67 enum service_type stack_types[ STACK_SIZE ];
68 mlt_service stack_service[ STACK_SIZE ];
69 int stack_service_size;
70 mlt_properties producer_map;
71 mlt_properties destructors;
75 xmlNodePtr stack_node[ STACK_SIZE ];
78 int entity_is_replace;
80 int branch[ STACK_SIZE ];
81 const xmlChar *publicId;
82 const xmlChar *systemId;
83 mlt_properties params;
87 typedef struct deserialise_context_s *deserialise_context;
89 /** Convert the numerical current branch address to a dot-delimited string.
91 static char *serialise_branch( deserialise_context this, char *s )
96 for ( i = 0; i < this->depth; i++ )
98 int len = strlen( s );
99 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", this->branch[ i ] );
107 static int context_push_service( deserialise_context this, mlt_service that, enum service_type type )
109 int ret = this->stack_service_size >= STACK_SIZE - 1;
112 this->stack_service[ this->stack_service_size ] = that;
113 this->stack_types[ this->stack_service_size++ ] = type;
115 // Record the tree branch on which this service lives
116 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
118 char s[ BRANCH_SIG_LEN ];
119 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( this, s ) );
128 static mlt_service context_pop_service( deserialise_context this, enum service_type *type )
130 mlt_service result = NULL;
131 if ( this->stack_service_size > 0 )
133 result = this->stack_service[ -- this->stack_service_size ];
135 *type = this->stack_types[ this->stack_service_size ];
143 static int context_push_node( deserialise_context this, xmlNodePtr node )
145 int ret = this->stack_node_size >= STACK_SIZE - 1;
147 this->stack_node[ this->stack_node_size ++ ] = node;
154 static xmlNodePtr context_pop_node( deserialise_context this )
156 xmlNodePtr result = NULL;
157 if ( this->stack_node_size > 0 )
158 result = this->stack_node[ -- this->stack_node_size ];
163 // Set the destructor on a new service
164 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
166 int registered = mlt_properties_get_int( properties, "registered" );
167 char *key = mlt_properties_get( properties, "registered" );
168 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
169 mlt_properties_set_int( properties, "registered", ++ registered );
173 // Prepend the property value with the document root
174 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
176 char *resource = mlt_properties_get( properties, name );
177 if ( resource != NULL && resource[0] )
179 // Qualify file name properties
180 char *root = mlt_properties_get( context->producer_map, "root" );
181 if ( root != NULL && strcmp( root, "" ) )
183 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
184 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
186 strcpy( full_resource, root );
187 strcat( full_resource, "/" );
188 strcat( full_resource, resource );
192 strcpy( full_resource, resource );
194 mlt_properties_set( properties, name, full_resource );
195 free( full_resource );
201 /** This function adds a producer to a playlist or multitrack when
202 there is no entry or track element.
205 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
207 // Return value (0 = service remains top of stack, 1 means it can be removed)
210 // Get the parent producer
211 enum service_type type = mlt_invalid_type;
212 mlt_service container = context_pop_service( context, &type );
215 if ( service != NULL && container != NULL )
217 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
218 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
219 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
224 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
225 char *hide_s = mlt_properties_get( properties, "hide" );
227 // Indicate that this service is no longer top of stack
232 case mlt_tractor_type:
234 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
235 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
238 case mlt_multitrack_type:
240 mlt_multitrack_connect( MLT_MULTITRACK( container ),
241 MLT_PRODUCER( service ),
242 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
245 case mlt_playlist_type:
247 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
252 fprintf( stderr, "Producer defined inside something that isn't a container\n" );
256 // Set the hide state of the track producer
257 if ( hide_s != NULL )
259 if ( strcmp( hide_s, "video" ) == 0 )
260 mlt_properties_set_int( properties, "hide", 1 );
261 else if ( strcmp( hide_s, "audio" ) == 0 )
262 mlt_properties_set_int( properties, "hide", 2 );
263 else if ( strcmp( hide_s, "both" ) == 0 )
264 mlt_properties_set_int( properties, "hide", 3 );
268 // Put the parent producer back
269 if ( container != NULL )
270 context_push_service( context, container, type );
275 /** Attach filters defined on that to this.
278 static void attach_filters( mlt_service this, mlt_service that )
283 mlt_filter filter = NULL;
284 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
286 mlt_service_attach( this, filter );
287 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
292 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
294 mlt_profile p = context->profile;
295 for ( ; atts != NULL && *atts != NULL; atts += 2 )
297 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
299 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
302 p->description = strdup( my_profile->description );
303 p->display_aspect_den = my_profile->display_aspect_den;
304 p->display_aspect_num = my_profile->display_aspect_num;
305 p->frame_rate_den = my_profile->frame_rate_den;
306 p->frame_rate_num = my_profile->frame_rate_num;
307 p->width = my_profile->width;
308 p->height = my_profile->height;
309 p->progressive = my_profile->progressive;
310 p->sample_aspect_den = my_profile->sample_aspect_den;
311 p->sample_aspect_num = my_profile->sample_aspect_num;
312 mlt_profile_close( my_profile );
315 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
317 if ( p->description )
318 free( p->description );
319 p->description = strdup( _s(atts[ 1 ]) );
321 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
322 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
323 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
324 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
325 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
326 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
327 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
328 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
329 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
330 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
331 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
332 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
333 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
334 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
335 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
336 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
337 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
338 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
342 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
344 mlt_tractor tractor = mlt_tractor_new( );
345 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
346 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
348 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
350 for ( ; atts != NULL && *atts != NULL; atts += 2 )
351 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
353 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
355 if ( mlt_properties_get( properties, "id" ) != NULL )
356 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
358 context_push_service( context, service, mlt_tractor_type );
361 static void on_end_tractor( deserialise_context context, const xmlChar *name )
364 enum service_type type;
365 mlt_service tractor = context_pop_service( context, &type );
367 if ( tractor != NULL && type == mlt_tractor_type )
369 // See if the tractor should be added to a playlist or multitrack
370 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
371 context_push_service( context, tractor, type );
375 fprintf( stderr, "Invalid state for tractor\n" );
379 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
381 enum service_type type;
382 mlt_service parent = context_pop_service( context, &type );
384 // If we don't have a parent, then create one now, providing we're in a state where we can
385 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
387 mlt_tractor tractor = NULL;
388 // Push the parent back
389 if ( parent != NULL )
390 context_push_service( context, parent, type );
392 // Create a tractor to contain the multitrack
393 tractor = mlt_tractor_new( );
394 parent = MLT_TRACTOR_SERVICE( tractor );
395 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
396 type = mlt_tractor_type;
398 // Flag it as a synthesised tractor for clean up later
399 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
402 if ( type == mlt_tractor_type )
404 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
405 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
406 for ( ; atts != NULL && *atts != NULL; atts += 2 )
407 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
409 if ( mlt_properties_get( properties, "id" ) != NULL )
410 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
412 context_push_service( context, parent, type );
413 context_push_service( context, service, mlt_multitrack_type );
417 fprintf( stderr, "Invalid multitrack position\n" );
421 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
423 // Get the multitrack from the stack
424 enum service_type type;
425 mlt_service service = context_pop_service( context, &type );
427 if ( service == NULL || type != mlt_multitrack_type )
428 fprintf( stderr, "End multitrack in the wrong state...\n" );
431 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
433 mlt_playlist playlist = mlt_playlist_init( );
434 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
435 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
437 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
439 for ( ; atts != NULL && *atts != NULL; atts += 2 )
441 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
443 // Out will be overwritten later as we append, so we need to save it
444 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
445 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
448 if ( mlt_properties_get( properties, "id" ) != NULL )
449 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
451 context_push_service( context, service, mlt_playlist_type );
454 static void on_end_playlist( deserialise_context context, const xmlChar *name )
456 // Get the playlist from the stack
457 enum service_type type;
458 mlt_service service = context_pop_service( context, &type );
460 if ( service != NULL && type == mlt_playlist_type )
462 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
463 mlt_position in = -1;
464 mlt_position out = -1;
466 if ( mlt_properties_get( properties, "in" ) )
467 in = mlt_properties_get_position( properties, "in" );
468 if ( mlt_properties_get( properties, "out" ) )
469 out = mlt_properties_get_position( properties, "out" );
471 // See if the playlist should be added to a playlist or multitrack
472 if ( add_producer( context, service, in, out ) == 0 )
473 context_push_service( context, service, type );
477 fprintf( stderr, "Invalid state of playlist end\n" );
481 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
483 // use a dummy service to hold properties to allow arbitrary nesting
484 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
485 mlt_service_init( service, NULL );
487 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
489 context_push_service( context, service, mlt_dummy_producer_type );
491 for ( ; atts != NULL && *atts != NULL; atts += 2 )
492 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
495 // Parse a SMIL clock value (as produced by Kino 0.9.1) and return position in frames
496 static mlt_position parse_clock_value( char *value, double fps )
498 // This implementation expects a fully specified clock value - no optional
500 char *pos, *copy = strdup( value );
502 mlt_position result = -1;
505 pos = strchr( value, ':' );
512 pos = strchr( value, ':' );
519 pos = strchr( value, '.' );
528 result = ( fps * ( ( (hh * 3600) + (mm * 60) + ss ) * 1000 + ms ) / 1000 + 0.5 );
533 static void on_end_producer( deserialise_context context, const xmlChar *name )
535 enum service_type type;
536 mlt_service service = context_pop_service( context, &type );
537 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
539 if ( service != NULL && type == mlt_dummy_producer_type )
541 mlt_service producer = NULL;
543 qualify_property( context, properties, "resource" );
544 char *resource = mlt_properties_get( properties, "resource" );
546 // Let Kino-SMIL src be a synonym for resource
547 if ( resource == NULL )
549 qualify_property( context, properties, "src" );
550 resource = mlt_properties_get( properties, "src" );
553 // Instantiate the producer
554 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
557 strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
558 if ( resource != NULL )
561 strncat( temp, resource, 1023 - strlen( temp ) );
563 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
566 // Just in case the plugin requested doesn't exist...
567 if ( producer == NULL && resource != NULL )
568 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
570 if ( producer == NULL )
571 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
573 if ( producer == NULL )
574 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
576 // Track this producer
577 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
579 // Propogate the properties
580 qualify_property( context, properties, "resource" );
581 qualify_property( context, properties, "luma" );
582 qualify_property( context, properties, "luma.resource" );
583 qualify_property( context, properties, "composite.luma" );
584 qualify_property( context, properties, "producer.resource" );
586 // Handle in/out properties separately
587 mlt_position in = -1;
588 mlt_position out = -1;
591 if ( mlt_properties_get( properties, "in" ) != NULL )
592 in = mlt_properties_get_position( properties, "in" );
593 // Let Kino-SMIL clipBegin be a synonym for in
594 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
596 if ( strchr( mlt_properties_get( properties, "clipBegin" ), ':' ) )
598 in = parse_clock_value( mlt_properties_get( properties, "clipBegin" ),
599 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
601 // Parse frames value
602 in = mlt_properties_get_position( properties, "clipBegin" );
605 if ( mlt_properties_get( properties, "out" ) != NULL )
606 out = mlt_properties_get_position( properties, "out" );
607 // Let Kino-SMIL clipEnd be a synonym for out
608 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
610 if ( strchr( mlt_properties_get( properties, "clipEnd" ), ':' ) )
612 out = parse_clock_value( mlt_properties_get( properties, "clipEnd" ),
613 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
615 // Parse frames value
616 out = mlt_properties_get_position( properties, "clipEnd" );
619 mlt_properties_set( properties, "in", NULL );
620 mlt_properties_set( properties, "out", NULL );
622 // Inherit the properties
623 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
625 // Attach all filters from service onto producer
626 attach_filters( producer, service );
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 );
632 // See if the producer should be added to a playlist or multitrack
633 if ( add_producer( context, producer, in, out ) == 0 )
635 // Otherwise, set in and out on...
636 if ( in != -1 || out != -1 )
638 // Get the parent service
639 enum service_type type;
640 mlt_service parent = context_pop_service( context, &type );
641 if ( parent != NULL )
643 // Get the parent properties
644 properties = MLT_SERVICE_PROPERTIES( parent );
646 char *resource = mlt_properties_get( properties, "resource" );
648 // Put the parent producer back
649 context_push_service( context, parent, type );
651 // If the parent is a track or entry
652 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
654 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
655 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
659 // Otherwise, set in and out on producer directly
660 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
665 // Otherwise, set in and out on producer directly
666 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
670 // Push the producer onto the stack
671 context_push_service( context, producer, mlt_producer_type );
674 mlt_service_close( service );
678 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
680 // Get the playlist from the stack
681 enum service_type type;
682 mlt_service service = context_pop_service( context, &type );
683 mlt_position length = 0;
685 if ( type == mlt_playlist_type && service != NULL )
687 // Look for the length attribute
688 for ( ; atts != NULL && *atts != NULL; atts += 2 )
690 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
692 length = atoll( _s(atts[1]) );
697 // Append a blank to the playlist
698 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
700 // Push the playlist back onto the stack
701 context_push_service( context, service, type );
705 fprintf( stderr, "blank without a playlist - a definite no no\n" );
709 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
711 mlt_producer entry = NULL;
712 mlt_properties temp = mlt_properties_new( );
714 for ( ; atts != NULL && *atts != NULL; atts += 2 )
716 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
718 // Look for the producer attribute
719 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
721 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
722 if ( producer != NULL )
723 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
727 // If we have a valid entry
728 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
730 mlt_playlist_clip_info info;
731 enum service_type parent_type = invalid_type;
732 mlt_service parent = context_pop_service( context, &parent_type );
733 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
735 if ( parent_type == mlt_playlist_type )
737 // Append the producer to the playlist
738 mlt_position in = -1;
739 mlt_position out = -1;
740 if ( mlt_properties_get( temp, "in" ) )
741 in = mlt_properties_get_position( temp, "in" );
742 if ( mlt_properties_get( temp, "out" ) )
743 out = mlt_properties_get_position( temp, "out" );
744 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
746 // Handle the repeat property
747 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
749 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
750 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
751 mlt_properties_get_int( temp, "repeat" ) );
754 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
759 fprintf( stderr, "Entry not part of a playlist...\n" );
762 context_push_service( context, parent, parent_type );
765 // Push the cut onto the stack
766 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
768 mlt_properties_close( temp );
771 static void on_end_entry( deserialise_context context, const xmlChar *name )
773 // Get the entry from the stack
774 enum service_type entry_type = invalid_type;
775 mlt_service entry = context_pop_service( context, &entry_type );
777 if ( entry == NULL && entry_type != mlt_entry_type )
779 fprintf( stderr, "Invalid state at end of entry\n" );
783 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
785 // use a dummy service to hold properties to allow arbitrary nesting
786 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
787 mlt_service_init( service, NULL );
789 // Push the dummy service onto the stack
790 context_push_service( context, service, mlt_entry_type );
792 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
794 for ( ; atts != NULL && *atts != NULL; atts += 2 )
796 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
798 // Look for the producer attribute
799 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
801 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
802 if ( producer != NULL )
803 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
808 static void on_end_track( deserialise_context context, const xmlChar *name )
810 // Get the track from the stack
811 enum service_type track_type;
812 mlt_service track = context_pop_service( context, &track_type );
814 if ( track != NULL && track_type == mlt_entry_type )
816 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
817 enum service_type parent_type = invalid_type;
818 mlt_service parent = context_pop_service( context, &parent_type );
819 mlt_multitrack multitrack = NULL;
821 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
822 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
824 if ( parent_type == mlt_tractor_type )
825 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
826 else if ( parent_type == mlt_multitrack_type )
827 multitrack = MLT_MULTITRACK( parent );
829 fprintf( stderr, "track contained in an invalid container\n" );
831 if ( multitrack != NULL )
833 // Set producer i/o if specified
834 if ( mlt_properties_get( track_props, "in" ) != NULL ||
835 mlt_properties_get( track_props, "out" ) != NULL )
837 mlt_position in = -1;
838 mlt_position out = -1;
839 if ( mlt_properties_get( track_props, "in" ) )
840 in = mlt_properties_get_position( track_props, "in" );
841 if ( mlt_properties_get( track_props, "out" ) )
842 out = mlt_properties_get_position( track_props, "out" );
843 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
844 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
845 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
846 track_props = MLT_PRODUCER_PROPERTIES( cut );
847 mlt_producer_close( cut );
851 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
854 // Set the hide state of the track producer
855 char *hide_s = mlt_properties_get( track_props, "hide" );
856 if ( hide_s != NULL )
858 if ( strcmp( hide_s, "video" ) == 0 )
859 mlt_properties_set_int( producer_props, "hide", 1 );
860 else if ( strcmp( hide_s, "audio" ) == 0 )
861 mlt_properties_set_int( producer_props, "hide", 2 );
862 else if ( strcmp( hide_s, "both" ) == 0 )
863 mlt_properties_set_int( producer_props, "hide", 3 );
867 if ( parent != NULL )
868 context_push_service( context, parent, parent_type );
870 mlt_service_close( track );
874 fprintf( stderr, "Invalid state at end of track\n" );
878 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
880 // use a dummy service to hold properties to allow arbitrary nesting
881 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
882 mlt_service_init( service, NULL );
884 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
886 context_push_service( context, service, mlt_dummy_filter_type );
888 // Set the properties
889 for ( ; atts != NULL && *atts != NULL; atts += 2 )
890 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
893 static void on_end_filter( deserialise_context context, const xmlChar *name )
895 enum service_type type;
896 mlt_service service = context_pop_service( context, &type );
897 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
899 enum service_type parent_type = invalid_type;
900 mlt_service parent = context_pop_service( context, &parent_type );
902 if ( service != NULL && type == mlt_dummy_filter_type )
904 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, mlt_properties_get( properties, "mlt_service" ), NULL ) );
905 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
907 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
909 // Propogate the properties
910 qualify_property( context, properties, "resource" );
911 qualify_property( context, properties, "luma" );
912 qualify_property( context, properties, "luma.resource" );
913 qualify_property( context, properties, "composite.luma" );
914 qualify_property( context, properties, "producer.resource" );
915 mlt_properties_inherit( filter_props, properties );
917 // Attach all filters from service onto filter
918 attach_filters( filter, service );
920 // Associate the filter with the parent
921 if ( parent != NULL )
923 if ( parent_type == mlt_tractor_type )
925 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
926 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
927 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
928 mlt_properties_get_int( properties, "in" ),
929 mlt_properties_get_int( properties, "out" ) );
933 mlt_service_attach( parent, MLT_FILTER( filter ) );
936 // Put the parent back on the stack
937 context_push_service( context, parent, parent_type );
941 fprintf( stderr, "filter closed with invalid parent...\n" );
944 // Close the dummy filter service
945 mlt_service_close( service );
949 fprintf( stderr, "Invalid top of stack on filter close\n" );
953 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
955 // use a dummy service to hold properties to allow arbitrary nesting
956 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
957 mlt_service_init( service, NULL );
959 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
961 context_push_service( context, service, mlt_dummy_transition_type );
963 // Set the properties
964 for ( ; atts != NULL && *atts != NULL; atts += 2 )
965 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
968 static void on_end_transition( deserialise_context context, const xmlChar *name )
970 enum service_type type;
971 mlt_service service = context_pop_service( context, &type );
972 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
974 enum service_type parent_type = invalid_type;
975 mlt_service parent = context_pop_service( context, &parent_type );
977 if ( service != NULL && type == mlt_dummy_transition_type )
979 char *id = mlt_properties_get( properties, "mlt_service" );
980 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
981 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
983 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
985 // Propogate the properties
986 qualify_property( context, properties, "resource" );
987 qualify_property( context, properties, "luma" );
988 qualify_property( context, properties, "luma.resource" );
989 qualify_property( context, properties, "composite.luma" );
990 qualify_property( context, properties, "producer.resource" );
991 mlt_properties_inherit( effect_props, properties );
993 // Attach all filters from service onto effect
994 attach_filters( effect, service );
996 // Associate the filter with the parent
997 if ( parent != NULL )
999 if ( parent_type == mlt_tractor_type )
1001 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1002 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1003 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1004 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1005 mlt_properties_get_int( properties, "a_track" ),
1006 mlt_properties_get_int( properties, "b_track" ) );
1007 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1008 mlt_properties_get_int( properties, "in" ),
1009 mlt_properties_get_int( properties, "out" ) );
1013 fprintf( stderr, "Misplaced transition - ignoring\n" );
1016 // Put the parent back on the stack
1017 context_push_service( context, parent, parent_type );
1021 fprintf( stderr, "transition closed with invalid parent...\n" );
1024 // Close the dummy filter service
1025 mlt_service_close( service );
1029 fprintf( stderr, "Invalid top of stack on transition close\n" );
1033 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1035 enum service_type type;
1036 mlt_service service = context_pop_service( context, &type );
1037 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1038 const char *value = NULL;
1040 if ( service != NULL )
1042 // Set the properties
1043 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1045 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1046 context->property = strdup( _s(atts[ 1 ]) );
1047 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1048 value = _s(atts[ 1 ]);
1051 if ( context->property != NULL )
1052 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1054 // Tell parser to collect any further nodes for serialisation
1055 context->is_value = 1;
1057 context_push_service( context, service, type );
1061 fprintf( stderr, "Property without a service '%s'?\n", ( const char * )name );
1065 static void on_end_property( deserialise_context context, const xmlChar *name )
1067 enum service_type type;
1068 mlt_service service = context_pop_service( context, &type );
1069 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1071 if ( service != NULL )
1073 // Tell parser to stop building a tree
1074 context->is_value = 0;
1076 // See if there is a xml tree for the value
1077 if ( context->property != NULL && context->value_doc != NULL )
1082 // Serialise the tree to get value
1083 xmlDocDumpMemory( context->value_doc, &value, &size );
1084 mlt_properties_set( properties, context->property, _s(value) );
1086 xmlFreeDoc( context->value_doc );
1087 context->value_doc = NULL;
1090 // Close this property handling
1091 free( context->property );
1092 context->property = NULL;
1094 context_push_service( context, service, type );
1098 fprintf( stderr, "Property without a service '%s'??\n", (const char *)name );
1102 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1104 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1105 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1107 //printf("on_start_element: %s\n", name );
1108 if ( context->pass == 0 )
1110 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1111 xmlStrcmp( name, _x("profile") ) == 0 ||
1112 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1113 on_start_profile( context, name, atts );
1116 context->branch[ context->depth ] ++;
1119 // Build a tree from nodes within a property value
1120 if ( context->is_value == 1 )
1122 xmlNodePtr node = xmlNewNode( NULL, name );
1124 if ( context->value_doc == NULL )
1127 context->value_doc = xmlNewDoc( _x("1.0") );
1128 xmlDocSetRootElement( context->value_doc, node );
1132 // Append child to tree
1133 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1135 context_push_node( context, node );
1137 // Set the attributes
1138 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1139 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1141 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1142 on_start_tractor( context, name, atts );
1143 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1144 on_start_multitrack( context, name, atts );
1145 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1146 on_start_playlist( context, name, atts );
1147 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1148 on_start_producer( context, name, atts );
1149 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1150 on_start_blank( context, name, atts );
1151 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1152 on_start_entry( context, name, atts );
1153 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1154 on_start_track( context, name, atts );
1155 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1156 on_start_filter( context, name, atts );
1157 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1158 on_start_transition( context, name, atts );
1159 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1160 on_start_property( context, name, atts );
1161 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1162 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1163 mlt_properties_set( context->producer_map, ( const char * )atts[ 0 ], ( const char * )atts[ 1 ] );
1166 static void on_end_element( void *ctx, const xmlChar *name )
1168 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1169 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1171 //printf("on_end_element: %s\n", name );
1172 if ( context->is_value == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1173 context_pop_node( context );
1174 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1175 on_end_multitrack( context, name );
1176 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1177 on_end_playlist( context, name );
1178 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1179 on_end_track( context, name );
1180 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1181 on_end_entry( context, name );
1182 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1183 on_end_tractor( context, name );
1184 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1185 on_end_property( context, name );
1186 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1187 on_end_producer( context, name );
1188 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1189 on_end_filter( context, name );
1190 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1191 on_end_transition( context, name );
1193 context->branch[ context->depth ] = 0;
1197 static void on_characters( void *ctx, const xmlChar *ch, int len )
1199 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1200 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1201 char *value = calloc( len + 1, 1 );
1202 enum service_type type;
1203 mlt_service service = context_pop_service( context, &type );
1204 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1206 if ( service != NULL )
1207 context_push_service( context, service, type );
1210 strncpy( value, (const char*) ch, len );
1212 if ( context->stack_node_size > 0 )
1213 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1215 // libxml2 generates an on_characters immediately after a get_entity within
1216 // an element value, and we ignore it because it is called again during
1217 // actual substitution.
1218 else if ( context->property != NULL && context->entity_is_replace == 0 )
1220 char *s = mlt_properties_get( properties, context->property );
1223 // Append new text to existing content
1224 char *new = calloc( strlen( s ) + len + 1, 1 );
1226 strcat( new, value );
1227 mlt_properties_set( properties, context->property, new );
1231 mlt_properties_set( properties, context->property, value );
1233 context->entity_is_replace = 0;
1238 /** Convert parameters parsed from resource into entity declarations.
1240 static void params_to_entities( deserialise_context context )
1242 if ( context->params != NULL )
1246 // Add our params as entitiy declarations
1247 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1249 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1250 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1251 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1255 mlt_properties_close( context->params );
1256 context->params = NULL;
1260 // The following 3 facilitate entity substitution in the SAX parser
1261 static void on_internal_subset( void *ctx, const xmlChar* name,
1262 const xmlChar* publicId, const xmlChar* systemId )
1264 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1265 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1267 context->publicId = publicId;
1268 context->systemId = systemId;
1269 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1271 // Override default entities with our parameters
1272 params_to_entities( context );
1275 // TODO: Check this with Dan... I think this is for parameterisation
1276 // but it's breaking standard escaped entities (like < etc).
1277 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1278 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1280 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1281 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1283 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1286 // TODO: Check this functionality (see on_entity_declaration)
1287 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1289 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1290 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1291 xmlEntityPtr e = NULL;
1293 // Setup for entity declarations if not ready
1294 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1296 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1297 context->publicId = _x("");
1298 context->systemId = _x("");
1301 // Add our parameters if not already
1302 params_to_entities( context );
1304 e = xmlGetPredefinedEntity( name );
1306 // Send signal to on_characters that an entity substitutin is pending
1309 e = xmlGetDocEntity( context->entity_doc, name );
1311 context->entity_is_replace = 1;
1317 /** Convert a hexadecimal character to its value.
1319 static int tohex( char p )
1321 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1324 /** Decode a url-encoded string containing hexadecimal character sequences.
1326 static char *url_decode( char *dest, char *src )
1334 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1347 /** Extract the filename from a URL attaching parameters to a properties list.
1349 static void parse_url( mlt_properties properties, char *url )
1352 int n = strlen( url );
1356 for ( i = 0; i < n; i++ )
1373 if ( name != NULL && value != NULL )
1374 mlt_properties_set( properties, name, value );
1380 if ( name != NULL && value != NULL )
1381 mlt_properties_set( properties, name, value );
1384 // Quick workaround to avoid unecessary libxml2 warnings
1385 static int file_exists( char *file )
1387 char *name = strdup( file );
1389 if ( name != NULL && strchr( name, '?' ) )
1390 *( strchr( name, '?' ) ) = '\0';
1393 FILE *f = fopen( name, "r" );
1395 if ( exists ) fclose( f );
1401 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1403 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1404 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1405 mlt_properties properties = NULL;
1407 struct _xmlParserCtxt *xmlcontext;
1408 int well_formed = 0;
1409 char *filename = NULL;
1410 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1412 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1415 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1416 if ( context == NULL )
1419 context->producer_map = mlt_properties_new();
1420 context->destructors = mlt_properties_new();
1421 context->params = mlt_properties_new();
1422 context->profile = profile;
1424 // Decode URL and parse parameters
1425 mlt_properties_set( context->producer_map, "root", "" );
1428 filename = strdup( data );
1429 parse_url( context->params, url_decode( filename, data ) );
1431 // We need the directory prefix which was used for the xml
1432 if ( strchr( filename, '/' ) )
1435 mlt_properties_set( context->producer_map, "root", filename );
1436 root = mlt_properties_get( context->producer_map, "root" );
1437 *( strrchr( root, '/' ) ) = '\0';
1439 // If we don't have an absolute path here, we're heading for disaster...
1440 if ( root[ 0 ] != '/' )
1442 char *cwd = getcwd( NULL, 0 );
1443 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1444 sprintf( real, "%s/%s", cwd, root );
1445 mlt_properties_set( context->producer_map, "root", real );
1452 // We need to track the number of registered filters
1453 mlt_properties_set_int( context->destructors, "registered", 0 );
1455 // Setup SAX callbacks
1456 sax->startElement = on_start_element;
1458 // Setup libxml2 SAX parsing
1460 xmlSubstituteEntitiesDefault( 1 );
1461 // This is used to facilitate entity substitution in the SAX parser
1462 context->entity_doc = xmlNewDoc( _x("1.0") );
1464 xmlcontext = xmlCreateFileParserCtxt( filename );
1466 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1468 // Invalid context - clean up and return NULL
1469 if ( xmlcontext == NULL )
1471 mlt_properties_close( context->producer_map );
1472 mlt_properties_close( context->destructors );
1473 mlt_properties_close( context->params );
1481 xmlcontext->sax = sax;
1482 xmlcontext->_private = ( void* )context;
1483 xmlParseDocument( xmlcontext );
1485 // Cleanup after parsing
1486 xmlcontext->sax = NULL;
1487 xmlcontext->_private = NULL;
1488 xmlFreeParserCtxt( xmlcontext );
1490 // Setup the second pass
1493 xmlcontext = xmlCreateFileParserCtxt( filename );
1495 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1497 // Invalid context - clean up and return NULL
1498 if ( xmlcontext == NULL )
1500 mlt_properties_close( context->producer_map );
1501 mlt_properties_close( context->destructors );
1502 mlt_properties_close( context->params );
1503 xmlFreeDoc( context->entity_doc );
1510 // Setup SAX callbacks
1511 sax->endElement = on_end_element;
1512 sax->characters = on_characters;
1513 sax->cdataBlock = on_characters;
1514 sax->internalSubset = on_internal_subset;
1515 sax->entityDecl = on_entity_declaration;
1516 sax->getEntity = on_get_entity;
1519 xmlcontext->sax = sax;
1520 xmlcontext->_private = ( void* )context;
1521 xmlParseDocument( xmlcontext );
1522 well_formed = xmlcontext->wellFormed;
1524 // Cleanup after parsing
1525 xmlFreeDoc( context->entity_doc );
1527 xmlMemoryDump( ); // for debugging
1529 // Get the last producer on the stack
1530 enum service_type type;
1531 mlt_service service = context_pop_service( context, &type );
1532 if ( well_formed && service != NULL )
1534 // Verify it is a producer service (mlt_type="mlt_producer")
1535 // (producer, playlist, multitrack)
1536 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1537 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1542 xmlDocPtr doc = xml_make_doc( service );
1543 xmlDocFormatDump( stdout, doc, 1 );
1548 if ( well_formed && service != NULL )
1550 char *title = mlt_properties_get( context->producer_map, "title" );
1552 // Need the complete producer list for various reasons
1553 properties = context->destructors;
1555 // Now make sure we don't have a reference to the service in the properties
1556 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1558 char *name = mlt_properties_get_name( properties, i );
1559 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1561 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1566 // We are done referencing destructor property list
1567 // Set this var to service properties for convenience
1568 properties = MLT_SERVICE_PROPERTIES( service );
1571 mlt_properties_set( properties, "title", title );
1573 // Optimise for overlapping producers
1574 mlt_producer_optimise( MLT_PRODUCER( service ) );
1576 // Handle deep copies
1577 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1579 // Now assign additional properties
1581 mlt_properties_set( properties, "resource", data );
1583 // This tells consumer_xml not to deep copy
1584 mlt_properties_set( properties, "xml", "was here" );
1588 // Allow the project to be edited
1589 mlt_properties_set( properties, "_xml", "was here" );
1590 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1595 // Return null if not well formed
1600 mlt_properties_close( context->producer_map );
1601 if ( context->params != NULL )
1602 mlt_properties_close( context->params );
1603 mlt_properties_close( context->destructors );
1607 return MLT_PRODUCER( service );