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;
313 mlt_profile_close( my_profile );
316 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
318 if ( p->description )
319 free( p->description );
320 p->description = strdup( _s(atts[ 1 ]) );
323 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
324 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
325 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
326 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
327 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
328 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
329 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
330 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
331 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
332 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
333 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
334 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
335 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
336 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
337 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
338 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
339 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
340 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
344 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
346 mlt_tractor tractor = mlt_tractor_new( );
347 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
348 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
350 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
352 for ( ; atts != NULL && *atts != NULL; atts += 2 )
353 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
355 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
357 if ( mlt_properties_get( properties, "id" ) != NULL )
358 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
360 context_push_service( context, service, mlt_tractor_type );
363 static void on_end_tractor( deserialise_context context, const xmlChar *name )
366 enum service_type type;
367 mlt_service tractor = context_pop_service( context, &type );
369 if ( tractor != NULL && type == mlt_tractor_type )
371 // See if the tractor should be added to a playlist or multitrack
372 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
373 context_push_service( context, tractor, type );
377 fprintf( stderr, "Invalid state for tractor\n" );
381 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
383 enum service_type type;
384 mlt_service parent = context_pop_service( context, &type );
386 // If we don't have a parent, then create one now, providing we're in a state where we can
387 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
389 mlt_tractor tractor = NULL;
390 // Push the parent back
391 if ( parent != NULL )
392 context_push_service( context, parent, type );
394 // Create a tractor to contain the multitrack
395 tractor = mlt_tractor_new( );
396 parent = MLT_TRACTOR_SERVICE( tractor );
397 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
398 type = mlt_tractor_type;
400 // Flag it as a synthesised tractor for clean up later
401 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
404 if ( type == mlt_tractor_type )
406 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
407 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
408 for ( ; atts != NULL && *atts != NULL; atts += 2 )
409 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
411 if ( mlt_properties_get( properties, "id" ) != NULL )
412 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
414 context_push_service( context, parent, type );
415 context_push_service( context, service, mlt_multitrack_type );
419 fprintf( stderr, "Invalid multitrack position\n" );
423 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
425 // Get the multitrack from the stack
426 enum service_type type;
427 mlt_service service = context_pop_service( context, &type );
429 if ( service == NULL || type != mlt_multitrack_type )
430 fprintf( stderr, "End multitrack in the wrong state...\n" );
433 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
435 mlt_playlist playlist = mlt_playlist_init( );
436 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
437 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
439 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
441 for ( ; atts != NULL && *atts != NULL; atts += 2 )
443 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
445 // Out will be overwritten later as we append, so we need to save it
446 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
447 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
450 if ( mlt_properties_get( properties, "id" ) != NULL )
451 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
453 context_push_service( context, service, mlt_playlist_type );
456 static void on_end_playlist( deserialise_context context, const xmlChar *name )
458 // Get the playlist from the stack
459 enum service_type type;
460 mlt_service service = context_pop_service( context, &type );
462 if ( service != NULL && type == mlt_playlist_type )
464 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
465 mlt_position in = -1;
466 mlt_position out = -1;
468 if ( mlt_properties_get( properties, "in" ) )
469 in = mlt_properties_get_position( properties, "in" );
470 if ( mlt_properties_get( properties, "out" ) )
471 out = mlt_properties_get_position( properties, "out" );
473 // See if the playlist should be added to a playlist or multitrack
474 if ( add_producer( context, service, in, out ) == 0 )
475 context_push_service( context, service, type );
479 fprintf( stderr, "Invalid state of playlist end\n" );
483 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
485 // use a dummy service to hold properties to allow arbitrary nesting
486 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
487 mlt_service_init( service, NULL );
489 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
491 context_push_service( context, service, mlt_dummy_producer_type );
493 for ( ; atts != NULL && *atts != NULL; atts += 2 )
494 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
497 // Parse a SMIL clock value (as produced by Kino 0.9.1) and return position in frames
498 static mlt_position parse_clock_value( char *value, double fps )
500 // This implementation expects a fully specified clock value - no optional
502 char *pos, *copy = strdup( value );
504 mlt_position result = -1;
507 pos = strchr( value, ':' );
514 pos = strchr( value, ':' );
521 pos = strchr( value, '.' );
530 result = ( fps * ( ( (hh * 3600) + (mm * 60) + ss ) * 1000 + ms ) / 1000 + 0.5 );
535 static void on_end_producer( deserialise_context context, const xmlChar *name )
537 enum service_type type;
538 mlt_service service = context_pop_service( context, &type );
539 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
541 if ( service != NULL && type == mlt_dummy_producer_type )
543 mlt_service producer = NULL;
545 qualify_property( context, properties, "resource" );
546 char *resource = mlt_properties_get( properties, "resource" );
548 // Let Kino-SMIL src be a synonym for resource
549 if ( resource == NULL )
551 qualify_property( context, properties, "src" );
552 resource = mlt_properties_get( properties, "src" );
555 // Instantiate the producer
556 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
559 strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
560 if ( resource != NULL )
563 strncat( temp, resource, 1023 - strlen( temp ) );
565 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
568 // Just in case the plugin requested doesn't exist...
569 if ( producer == NULL && resource != NULL )
570 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
572 if ( producer == NULL )
573 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
575 if ( producer == NULL )
576 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
578 // Track this producer
579 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
581 // Propogate the properties
582 qualify_property( context, properties, "resource" );
583 qualify_property( context, properties, "luma" );
584 qualify_property( context, properties, "luma.resource" );
585 qualify_property( context, properties, "composite.luma" );
586 qualify_property( context, properties, "producer.resource" );
588 // Handle in/out properties separately
589 mlt_position in = -1;
590 mlt_position out = -1;
593 if ( mlt_properties_get( properties, "in" ) != NULL )
594 in = mlt_properties_get_position( properties, "in" );
595 // Let Kino-SMIL clipBegin be a synonym for in
596 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
598 if ( strchr( mlt_properties_get( properties, "clipBegin" ), ':' ) )
600 in = parse_clock_value( mlt_properties_get( properties, "clipBegin" ),
601 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
603 // Parse frames value
604 in = mlt_properties_get_position( properties, "clipBegin" );
607 if ( mlt_properties_get( properties, "out" ) != NULL )
608 out = mlt_properties_get_position( properties, "out" );
609 // Let Kino-SMIL clipEnd be a synonym for out
610 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
612 if ( strchr( mlt_properties_get( properties, "clipEnd" ), ':' ) )
614 out = parse_clock_value( mlt_properties_get( properties, "clipEnd" ),
615 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
617 // Parse frames value
618 out = mlt_properties_get_position( properties, "clipEnd" );
621 mlt_properties_set( properties, "in", NULL );
622 mlt_properties_set( properties, "out", NULL );
624 // Inherit the properties
625 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
627 // Attach all filters from service onto producer
628 attach_filters( producer, service );
630 // Add the producer to the producer map
631 if ( mlt_properties_get( properties, "id" ) != NULL )
632 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
634 // See if the producer should be added to a playlist or multitrack
635 if ( add_producer( context, producer, in, out ) == 0 )
637 // Otherwise, set in and out on...
638 if ( in != -1 || out != -1 )
640 // Get the parent service
641 enum service_type type;
642 mlt_service parent = context_pop_service( context, &type );
643 if ( parent != NULL )
645 // Get the parent properties
646 properties = MLT_SERVICE_PROPERTIES( parent );
648 char *resource = mlt_properties_get( properties, "resource" );
650 // Put the parent producer back
651 context_push_service( context, parent, type );
653 // If the parent is a track or entry
654 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
656 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
657 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
661 // Otherwise, set in and out on producer directly
662 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
667 // Otherwise, set in and out on producer directly
668 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
672 // Push the producer onto the stack
673 context_push_service( context, producer, mlt_producer_type );
676 mlt_service_close( service );
680 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
682 // Get the playlist from the stack
683 enum service_type type;
684 mlt_service service = context_pop_service( context, &type );
685 mlt_position length = 0;
687 if ( type == mlt_playlist_type && service != NULL )
689 // Look for the length attribute
690 for ( ; atts != NULL && *atts != NULL; atts += 2 )
692 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
694 length = atoll( _s(atts[1]) );
699 // Append a blank to the playlist
700 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
702 // Push the playlist back onto the stack
703 context_push_service( context, service, type );
707 fprintf( stderr, "blank without a playlist - a definite no no\n" );
711 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
713 mlt_producer entry = NULL;
714 mlt_properties temp = mlt_properties_new( );
716 for ( ; atts != NULL && *atts != NULL; atts += 2 )
718 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
720 // Look for the producer attribute
721 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
723 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
724 if ( producer != NULL )
725 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
729 // If we have a valid entry
730 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
732 mlt_playlist_clip_info info;
733 enum service_type parent_type = invalid_type;
734 mlt_service parent = context_pop_service( context, &parent_type );
735 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
737 if ( parent_type == mlt_playlist_type )
739 // Append the producer to the playlist
740 mlt_position in = -1;
741 mlt_position out = -1;
742 if ( mlt_properties_get( temp, "in" ) )
743 in = mlt_properties_get_position( temp, "in" );
744 if ( mlt_properties_get( temp, "out" ) )
745 out = mlt_properties_get_position( temp, "out" );
746 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
748 // Handle the repeat property
749 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
751 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
752 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
753 mlt_properties_get_int( temp, "repeat" ) );
756 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
761 fprintf( stderr, "Entry not part of a playlist...\n" );
764 context_push_service( context, parent, parent_type );
767 // Push the cut onto the stack
768 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
770 mlt_properties_close( temp );
773 static void on_end_entry( deserialise_context context, const xmlChar *name )
775 // Get the entry from the stack
776 enum service_type entry_type = invalid_type;
777 mlt_service entry = context_pop_service( context, &entry_type );
779 if ( entry == NULL && entry_type != mlt_entry_type )
781 fprintf( stderr, "Invalid state at end of entry\n" );
785 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
787 // use a dummy service to hold properties to allow arbitrary nesting
788 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
789 mlt_service_init( service, NULL );
791 // Push the dummy service onto the stack
792 context_push_service( context, service, mlt_entry_type );
794 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
796 for ( ; atts != NULL && *atts != NULL; atts += 2 )
798 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
800 // Look for the producer attribute
801 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
803 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
804 if ( producer != NULL )
805 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
810 static void on_end_track( deserialise_context context, const xmlChar *name )
812 // Get the track from the stack
813 enum service_type track_type;
814 mlt_service track = context_pop_service( context, &track_type );
816 if ( track != NULL && track_type == mlt_entry_type )
818 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
819 enum service_type parent_type = invalid_type;
820 mlt_service parent = context_pop_service( context, &parent_type );
821 mlt_multitrack multitrack = NULL;
823 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
824 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
826 if ( parent_type == mlt_tractor_type )
827 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
828 else if ( parent_type == mlt_multitrack_type )
829 multitrack = MLT_MULTITRACK( parent );
831 fprintf( stderr, "track contained in an invalid container\n" );
833 if ( multitrack != NULL )
835 // Set producer i/o if specified
836 if ( mlt_properties_get( track_props, "in" ) != NULL ||
837 mlt_properties_get( track_props, "out" ) != NULL )
839 mlt_position in = -1;
840 mlt_position out = -1;
841 if ( mlt_properties_get( track_props, "in" ) )
842 in = mlt_properties_get_position( track_props, "in" );
843 if ( mlt_properties_get( track_props, "out" ) )
844 out = mlt_properties_get_position( track_props, "out" );
845 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
846 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
847 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
848 track_props = MLT_PRODUCER_PROPERTIES( cut );
849 mlt_producer_close( cut );
853 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
856 // Set the hide state of the track producer
857 char *hide_s = mlt_properties_get( track_props, "hide" );
858 if ( hide_s != NULL )
860 if ( strcmp( hide_s, "video" ) == 0 )
861 mlt_properties_set_int( producer_props, "hide", 1 );
862 else if ( strcmp( hide_s, "audio" ) == 0 )
863 mlt_properties_set_int( producer_props, "hide", 2 );
864 else if ( strcmp( hide_s, "both" ) == 0 )
865 mlt_properties_set_int( producer_props, "hide", 3 );
869 if ( parent != NULL )
870 context_push_service( context, parent, parent_type );
872 mlt_service_close( track );
876 fprintf( stderr, "Invalid state at end of track\n" );
880 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
882 // use a dummy service to hold properties to allow arbitrary nesting
883 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
884 mlt_service_init( service, NULL );
886 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
888 context_push_service( context, service, mlt_dummy_filter_type );
890 // Set the properties
891 for ( ; atts != NULL && *atts != NULL; atts += 2 )
892 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
895 static void on_end_filter( deserialise_context context, const xmlChar *name )
897 enum service_type type;
898 mlt_service service = context_pop_service( context, &type );
899 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
901 enum service_type parent_type = invalid_type;
902 mlt_service parent = context_pop_service( context, &parent_type );
904 if ( service != NULL && type == mlt_dummy_filter_type )
906 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, mlt_properties_get( properties, "mlt_service" ), NULL ) );
907 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
909 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
911 // Propogate the properties
912 qualify_property( context, properties, "resource" );
913 qualify_property( context, properties, "luma" );
914 qualify_property( context, properties, "luma.resource" );
915 qualify_property( context, properties, "composite.luma" );
916 qualify_property( context, properties, "producer.resource" );
917 mlt_properties_inherit( filter_props, properties );
919 // Attach all filters from service onto filter
920 attach_filters( filter, service );
922 // Associate the filter with the parent
923 if ( parent != NULL )
925 if ( parent_type == mlt_tractor_type )
927 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
928 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
929 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
930 mlt_properties_get_int( properties, "in" ),
931 mlt_properties_get_int( properties, "out" ) );
935 mlt_service_attach( parent, MLT_FILTER( filter ) );
938 // Put the parent back on the stack
939 context_push_service( context, parent, parent_type );
943 fprintf( stderr, "filter closed with invalid parent...\n" );
946 // Close the dummy filter service
947 mlt_service_close( service );
951 fprintf( stderr, "Invalid top of stack on filter close\n" );
955 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
957 // use a dummy service to hold properties to allow arbitrary nesting
958 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
959 mlt_service_init( service, NULL );
961 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
963 context_push_service( context, service, mlt_dummy_transition_type );
965 // Set the properties
966 for ( ; atts != NULL && *atts != NULL; atts += 2 )
967 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
970 static void on_end_transition( deserialise_context context, const xmlChar *name )
972 enum service_type type;
973 mlt_service service = context_pop_service( context, &type );
974 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
976 enum service_type parent_type = invalid_type;
977 mlt_service parent = context_pop_service( context, &parent_type );
979 if ( service != NULL && type == mlt_dummy_transition_type )
981 char *id = mlt_properties_get( properties, "mlt_service" );
982 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
983 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
985 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
987 // Propogate the properties
988 qualify_property( context, properties, "resource" );
989 qualify_property( context, properties, "luma" );
990 qualify_property( context, properties, "luma.resource" );
991 qualify_property( context, properties, "composite.luma" );
992 qualify_property( context, properties, "producer.resource" );
993 mlt_properties_inherit( effect_props, properties );
995 // Attach all filters from service onto effect
996 attach_filters( effect, service );
998 // Associate the filter with the parent
999 if ( parent != NULL )
1001 if ( parent_type == mlt_tractor_type )
1003 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1004 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1005 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1006 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1007 mlt_properties_get_int( properties, "a_track" ),
1008 mlt_properties_get_int( properties, "b_track" ) );
1009 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1010 mlt_properties_get_int( properties, "in" ),
1011 mlt_properties_get_int( properties, "out" ) );
1015 fprintf( stderr, "Misplaced transition - ignoring\n" );
1018 // Put the parent back on the stack
1019 context_push_service( context, parent, parent_type );
1023 fprintf( stderr, "transition closed with invalid parent...\n" );
1026 // Close the dummy filter service
1027 mlt_service_close( service );
1031 fprintf( stderr, "Invalid top of stack on transition close\n" );
1035 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1037 enum service_type type;
1038 mlt_service service = context_pop_service( context, &type );
1039 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1040 const char *value = NULL;
1042 if ( service != NULL )
1044 // Set the properties
1045 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1047 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1048 context->property = strdup( _s(atts[ 1 ]) );
1049 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1050 value = _s(atts[ 1 ]);
1053 if ( context->property != NULL )
1054 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1056 // Tell parser to collect any further nodes for serialisation
1057 context->is_value = 1;
1059 context_push_service( context, service, type );
1063 fprintf( stderr, "Property without a service '%s'?\n", ( const char * )name );
1067 static void on_end_property( deserialise_context context, const xmlChar *name )
1069 enum service_type type;
1070 mlt_service service = context_pop_service( context, &type );
1071 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1073 if ( service != NULL )
1075 // Tell parser to stop building a tree
1076 context->is_value = 0;
1078 // See if there is a xml tree for the value
1079 if ( context->property != NULL && context->value_doc != NULL )
1084 // Serialise the tree to get value
1085 xmlDocDumpMemory( context->value_doc, &value, &size );
1086 mlt_properties_set( properties, context->property, _s(value) );
1088 xmlFreeDoc( context->value_doc );
1089 context->value_doc = NULL;
1092 // Close this property handling
1093 free( context->property );
1094 context->property = NULL;
1096 context_push_service( context, service, type );
1100 fprintf( stderr, "Property without a service '%s'??\n", (const char *)name );
1104 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1106 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1107 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1109 //printf("on_start_element: %s\n", name );
1110 if ( context->pass == 0 )
1112 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1113 xmlStrcmp( name, _x("profile") ) == 0 ||
1114 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1115 on_start_profile( context, name, atts );
1118 context->branch[ context->depth ] ++;
1121 // Build a tree from nodes within a property value
1122 if ( context->is_value == 1 )
1124 xmlNodePtr node = xmlNewNode( NULL, name );
1126 if ( context->value_doc == NULL )
1129 context->value_doc = xmlNewDoc( _x("1.0") );
1130 xmlDocSetRootElement( context->value_doc, node );
1134 // Append child to tree
1135 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1137 context_push_node( context, node );
1139 // Set the attributes
1140 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1141 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1143 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1144 on_start_tractor( context, name, atts );
1145 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1146 on_start_multitrack( context, name, atts );
1147 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1148 on_start_playlist( context, name, atts );
1149 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1150 on_start_producer( context, name, atts );
1151 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1152 on_start_blank( context, name, atts );
1153 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1154 on_start_entry( context, name, atts );
1155 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1156 on_start_track( context, name, atts );
1157 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1158 on_start_filter( context, name, atts );
1159 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1160 on_start_transition( context, name, atts );
1161 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1162 on_start_property( context, name, atts );
1163 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1164 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1165 mlt_properties_set( context->producer_map, ( const char * )atts[ 0 ], ( const char * )atts[ 1 ] );
1168 static void on_end_element( void *ctx, const xmlChar *name )
1170 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1171 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1173 //printf("on_end_element: %s\n", name );
1174 if ( context->is_value == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1175 context_pop_node( context );
1176 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1177 on_end_multitrack( context, name );
1178 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1179 on_end_playlist( context, name );
1180 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1181 on_end_track( context, name );
1182 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1183 on_end_entry( context, name );
1184 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1185 on_end_tractor( context, name );
1186 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1187 on_end_property( context, name );
1188 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1189 on_end_producer( context, name );
1190 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1191 on_end_filter( context, name );
1192 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1193 on_end_transition( context, name );
1195 context->branch[ context->depth ] = 0;
1199 static void on_characters( void *ctx, const xmlChar *ch, int len )
1201 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1202 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1203 char *value = calloc( len + 1, 1 );
1204 enum service_type type;
1205 mlt_service service = context_pop_service( context, &type );
1206 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1208 if ( service != NULL )
1209 context_push_service( context, service, type );
1212 strncpy( value, (const char*) ch, len );
1214 if ( context->stack_node_size > 0 )
1215 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1217 // libxml2 generates an on_characters immediately after a get_entity within
1218 // an element value, and we ignore it because it is called again during
1219 // actual substitution.
1220 else if ( context->property != NULL && context->entity_is_replace == 0 )
1222 char *s = mlt_properties_get( properties, context->property );
1225 // Append new text to existing content
1226 char *new = calloc( strlen( s ) + len + 1, 1 );
1228 strcat( new, value );
1229 mlt_properties_set( properties, context->property, new );
1233 mlt_properties_set( properties, context->property, value );
1235 context->entity_is_replace = 0;
1240 /** Convert parameters parsed from resource into entity declarations.
1242 static void params_to_entities( deserialise_context context )
1244 if ( context->params != NULL )
1248 // Add our params as entitiy declarations
1249 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1251 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1252 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1253 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1257 mlt_properties_close( context->params );
1258 context->params = NULL;
1262 // The following 3 facilitate entity substitution in the SAX parser
1263 static void on_internal_subset( void *ctx, const xmlChar* name,
1264 const xmlChar* publicId, const xmlChar* systemId )
1266 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1267 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1269 context->publicId = publicId;
1270 context->systemId = systemId;
1271 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1273 // Override default entities with our parameters
1274 params_to_entities( context );
1277 // TODO: Check this with Dan... I think this is for parameterisation
1278 // but it's breaking standard escaped entities (like < etc).
1279 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1280 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1282 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1283 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1285 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1288 // TODO: Check this functionality (see on_entity_declaration)
1289 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1291 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1292 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1293 xmlEntityPtr e = NULL;
1295 // Setup for entity declarations if not ready
1296 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1298 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1299 context->publicId = _x("");
1300 context->systemId = _x("");
1303 // Add our parameters if not already
1304 params_to_entities( context );
1306 e = xmlGetPredefinedEntity( name );
1308 // Send signal to on_characters that an entity substitutin is pending
1311 e = xmlGetDocEntity( context->entity_doc, name );
1313 context->entity_is_replace = 1;
1319 /** Convert a hexadecimal character to its value.
1321 static int tohex( char p )
1323 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1326 /** Decode a url-encoded string containing hexadecimal character sequences.
1328 static char *url_decode( char *dest, char *src )
1336 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1349 /** Extract the filename from a URL attaching parameters to a properties list.
1351 static void parse_url( mlt_properties properties, char *url )
1354 int n = strlen( url );
1358 for ( i = 0; i < n; i++ )
1375 if ( name != NULL && value != NULL )
1376 mlt_properties_set( properties, name, value );
1382 if ( name != NULL && value != NULL )
1383 mlt_properties_set( properties, name, value );
1386 // Quick workaround to avoid unecessary libxml2 warnings
1387 static int file_exists( char *file )
1389 char *name = strdup( file );
1391 if ( name != NULL && strchr( name, '?' ) )
1392 *( strchr( name, '?' ) ) = '\0';
1395 FILE *f = fopen( name, "r" );
1397 if ( exists ) fclose( f );
1403 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1405 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1406 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1407 mlt_properties properties = NULL;
1409 struct _xmlParserCtxt *xmlcontext;
1410 int well_formed = 0;
1411 char *filename = NULL;
1412 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1414 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1417 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1418 if ( context == NULL )
1421 context->producer_map = mlt_properties_new();
1422 context->destructors = mlt_properties_new();
1423 context->params = mlt_properties_new();
1424 context->profile = profile;
1426 // Decode URL and parse parameters
1427 mlt_properties_set( context->producer_map, "root", "" );
1430 filename = strdup( data );
1431 parse_url( context->params, url_decode( filename, data ) );
1433 // We need the directory prefix which was used for the xml
1434 if ( strchr( filename, '/' ) )
1437 mlt_properties_set( context->producer_map, "root", filename );
1438 root = mlt_properties_get( context->producer_map, "root" );
1439 *( strrchr( root, '/' ) ) = '\0';
1441 // If we don't have an absolute path here, we're heading for disaster...
1442 if ( root[ 0 ] != '/' )
1444 char *cwd = getcwd( NULL, 0 );
1445 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1446 sprintf( real, "%s/%s", cwd, root );
1447 mlt_properties_set( context->producer_map, "root", real );
1454 // We need to track the number of registered filters
1455 mlt_properties_set_int( context->destructors, "registered", 0 );
1457 // Setup SAX callbacks
1458 sax->startElement = on_start_element;
1460 // Setup libxml2 SAX parsing
1462 xmlSubstituteEntitiesDefault( 1 );
1463 // This is used to facilitate entity substitution in the SAX parser
1464 context->entity_doc = xmlNewDoc( _x("1.0") );
1466 xmlcontext = xmlCreateFileParserCtxt( filename );
1468 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1470 // Invalid context - clean up and return NULL
1471 if ( xmlcontext == NULL )
1473 mlt_properties_close( context->producer_map );
1474 mlt_properties_close( context->destructors );
1475 mlt_properties_close( context->params );
1483 xmlcontext->sax = sax;
1484 xmlcontext->_private = ( void* )context;
1485 xmlParseDocument( xmlcontext );
1487 // Cleanup after parsing
1488 xmlcontext->sax = NULL;
1489 xmlcontext->_private = NULL;
1490 xmlFreeParserCtxt( xmlcontext );
1492 // Setup the second pass
1495 xmlcontext = xmlCreateFileParserCtxt( filename );
1497 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1499 // Invalid context - clean up and return NULL
1500 if ( xmlcontext == NULL )
1502 mlt_properties_close( context->producer_map );
1503 mlt_properties_close( context->destructors );
1504 mlt_properties_close( context->params );
1505 xmlFreeDoc( context->entity_doc );
1512 // Setup SAX callbacks
1513 sax->endElement = on_end_element;
1514 sax->characters = on_characters;
1515 sax->cdataBlock = on_characters;
1516 sax->internalSubset = on_internal_subset;
1517 sax->entityDecl = on_entity_declaration;
1518 sax->getEntity = on_get_entity;
1521 xmlcontext->sax = sax;
1522 xmlcontext->_private = ( void* )context;
1523 xmlParseDocument( xmlcontext );
1524 well_formed = xmlcontext->wellFormed;
1526 // Cleanup after parsing
1527 xmlFreeDoc( context->entity_doc );
1529 xmlMemoryDump( ); // for debugging
1531 // Get the last producer on the stack
1532 enum service_type type;
1533 mlt_service service = context_pop_service( context, &type );
1534 if ( well_formed && service != NULL )
1536 // Verify it is a producer service (mlt_type="mlt_producer")
1537 // (producer, playlist, multitrack)
1538 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1539 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1544 xmlDocPtr doc = xml_make_doc( service );
1545 xmlDocFormatDump( stdout, doc, 1 );
1550 if ( well_formed && service != NULL )
1552 char *title = mlt_properties_get( context->producer_map, "title" );
1554 // Need the complete producer list for various reasons
1555 properties = context->destructors;
1557 // Now make sure we don't have a reference to the service in the properties
1558 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1560 char *name = mlt_properties_get_name( properties, i );
1561 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1563 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1568 // We are done referencing destructor property list
1569 // Set this var to service properties for convenience
1570 properties = MLT_SERVICE_PROPERTIES( service );
1573 mlt_properties_set( properties, "title", title );
1575 // Optimise for overlapping producers
1576 mlt_producer_optimise( MLT_PRODUCER( service ) );
1578 // Handle deep copies
1579 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1581 // Now assign additional properties
1583 mlt_properties_set( properties, "resource", data );
1585 // This tells consumer_xml not to deep copy
1586 mlt_properties_set( properties, "xml", "was here" );
1590 // Allow the project to be edited
1591 mlt_properties_set( properties, "_xml", "was here" );
1592 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1597 // Return null if not well formed
1602 mlt_properties_close( context->producer_map );
1603 if ( context->params != NULL )
1604 mlt_properties_close( context->params );
1605 mlt_properties_close( context->destructors );
1609 return MLT_PRODUCER( service );