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>
25 #include <framework/mlt_log.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,
63 mlt_dummy_consumer_type
66 struct deserialise_context_s
68 enum service_type stack_types[ STACK_SIZE ];
69 mlt_service stack_service[ STACK_SIZE ];
70 int stack_service_size;
71 mlt_properties producer_map;
72 mlt_properties destructors;
76 xmlNodePtr stack_node[ STACK_SIZE ];
79 int entity_is_replace;
81 int branch[ STACK_SIZE ];
82 const xmlChar *publicId;
83 const xmlChar *systemId;
84 mlt_properties params;
88 mlt_consumer consumer;
92 typedef struct deserialise_context_s *deserialise_context;
94 /** Trim the leading and trailing whitespace from a string in-place.
96 static char* trim( char *s )
99 if ( s && ( n = strlen( s ) ) )
102 while ( i < n && isspace( s[i] ) ) i++;
103 while ( --n && isspace( s[n] ) );
106 memmove( s, s + i, n );
112 /** Convert the numerical current branch address to a dot-delimited string.
114 static char *serialise_branch( deserialise_context context, char *s )
119 for ( i = 0; i < context->depth; i++ )
121 int len = strlen( s );
122 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
130 static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
132 int ret = context->stack_service_size >= STACK_SIZE - 1;
135 context->stack_service[ context->stack_service_size ] = that;
136 context->stack_types[ context->stack_service_size++ ] = type;
138 // Record the tree branch on which this service lives
139 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
141 char s[ BRANCH_SIG_LEN ];
142 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
151 static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
153 mlt_service result = NULL;
155 *type = invalid_type;
156 if ( context->stack_service_size > 0 )
158 result = context->stack_service[ -- context->stack_service_size ];
160 *type = context->stack_types[ context->stack_service_size ];
168 static int context_push_node( deserialise_context context, xmlNodePtr node )
170 int ret = context->stack_node_size >= STACK_SIZE - 1;
172 context->stack_node[ context->stack_node_size ++ ] = node;
179 static xmlNodePtr context_pop_node( deserialise_context context )
181 xmlNodePtr result = NULL;
182 if ( context->stack_node_size > 0 )
183 result = context->stack_node[ -- context->stack_node_size ];
188 // Set the destructor on a new service
189 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
191 int registered = mlt_properties_get_int( properties, "registered" );
192 char *key = mlt_properties_get( properties, "registered" );
193 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
194 mlt_properties_set_int( properties, "registered", ++ registered );
198 // Prepend the property value with the document root
199 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
201 char *resource = mlt_properties_get( properties, name );
202 if ( resource != NULL && resource[0] )
204 // Qualify file name properties
205 char *root = mlt_properties_get( context->producer_map, "root" );
206 if ( root != NULL && strcmp( root, "" ) )
208 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
209 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
211 strcpy( full_resource, root );
212 strcat( full_resource, "/" );
213 strcat( full_resource, resource );
217 strcpy( full_resource, resource );
219 mlt_properties_set( properties, name, full_resource );
220 free( full_resource );
226 /** This function adds a producer to a playlist or multitrack when
227 there is no entry or track element.
230 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
232 // Return value (0 = service remains top of stack, 1 means it can be removed)
235 // Get the parent producer
236 enum service_type type = mlt_invalid_type;
237 mlt_service container = context_pop_service( context, &type );
240 if ( service != NULL && container != NULL )
242 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
243 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
244 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
249 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
250 char *hide_s = mlt_properties_get( properties, "hide" );
252 // Indicate that this service is no longer top of stack
257 case mlt_tractor_type:
259 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
260 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
263 case mlt_multitrack_type:
265 mlt_multitrack_connect( MLT_MULTITRACK( container ),
266 MLT_PRODUCER( service ),
267 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
270 case mlt_playlist_type:
272 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
277 mlt_log_warning( container, "Producer defined inside something that isn't a container\n" );
281 // Set the hide state of the track producer
282 if ( hide_s != NULL )
284 if ( strcmp( hide_s, "video" ) == 0 )
285 mlt_properties_set_int( properties, "hide", 1 );
286 else if ( strcmp( hide_s, "audio" ) == 0 )
287 mlt_properties_set_int( properties, "hide", 2 );
288 else if ( strcmp( hide_s, "both" ) == 0 )
289 mlt_properties_set_int( properties, "hide", 3 );
293 // Put the parent producer back
294 if ( container != NULL )
295 context_push_service( context, container, type );
300 /** Attach filters defined on that to this.
303 static void attach_filters( mlt_service service, mlt_service that )
308 mlt_filter filter = NULL;
309 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
311 mlt_service_attach( service, filter );
312 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
317 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
319 mlt_profile p = context->profile;
320 for ( ; atts != NULL && *atts != NULL; atts += 2 )
322 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
324 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
327 p->description = strdup( my_profile->description );
328 p->display_aspect_den = my_profile->display_aspect_den;
329 p->display_aspect_num = my_profile->display_aspect_num;
330 p->frame_rate_den = my_profile->frame_rate_den;
331 p->frame_rate_num = my_profile->frame_rate_num;
332 p->width = my_profile->width;
333 p->height = my_profile->height;
334 p->progressive = my_profile->progressive;
335 p->sample_aspect_den = my_profile->sample_aspect_den;
336 p->sample_aspect_num = my_profile->sample_aspect_num;
337 p->colorspace = my_profile->colorspace;
339 mlt_profile_close( my_profile );
342 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
344 if ( p->description )
345 free( p->description );
346 p->description = strdup( _s(atts[ 1 ]) );
349 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
350 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
351 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
352 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
353 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
354 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
355 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
356 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
357 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
358 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
359 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
360 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
361 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
362 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
363 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
364 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
365 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
366 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
367 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
368 p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
372 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
374 mlt_tractor tractor = mlt_tractor_new( );
375 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
376 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
378 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
379 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );
381 for ( ; atts != NULL && *atts != NULL; atts += 2 )
382 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
384 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
386 if ( mlt_properties_get( properties, "id" ) != NULL )
387 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
389 context_push_service( context, service, mlt_tractor_type );
392 static void on_end_tractor( deserialise_context context, const xmlChar *name )
395 enum service_type type;
396 mlt_service tractor = context_pop_service( context, &type );
398 if ( tractor != NULL && type == mlt_tractor_type )
400 // See if the tractor should be added to a playlist or multitrack
401 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
402 context_push_service( context, tractor, type );
406 mlt_log_warning( tractor, "Invalid state for tractor\n" );
410 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
412 enum service_type type;
413 mlt_service parent = context_pop_service( context, &type );
415 // If we don't have a parent, then create one now, providing we're in a state where we can
416 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
418 mlt_tractor tractor = NULL;
419 // Push the parent back
420 if ( parent != NULL )
421 context_push_service( context, parent, type );
423 // Create a tractor to contain the multitrack
424 tractor = mlt_tractor_new( );
425 parent = MLT_TRACTOR_SERVICE( tractor );
426 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
427 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
428 type = mlt_tractor_type;
430 // Flag it as a synthesised tractor for clean up later
431 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
434 if ( type == mlt_tractor_type )
436 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
437 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
438 for ( ; atts != NULL && *atts != NULL; atts += 2 )
439 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
441 if ( mlt_properties_get( properties, "id" ) != NULL )
442 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
444 context_push_service( context, parent, type );
445 context_push_service( context, service, mlt_multitrack_type );
449 mlt_log_warning( parent, "Invalid multitrack position\n" );
453 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
455 // Get the multitrack from the stack
456 enum service_type type;
457 mlt_service service = context_pop_service( context, &type );
459 if ( service == NULL || type != mlt_multitrack_type )
460 mlt_log_warning( service, "End multitrack in the wrong state...\n" );
463 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
465 mlt_playlist playlist = mlt_playlist_init( );
466 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
467 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
469 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
471 for ( ; atts != NULL && *atts != NULL; atts += 2 )
473 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
475 // Out will be overwritten later as we append, so we need to save it
476 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
477 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
480 if ( mlt_properties_get( properties, "id" ) != NULL )
481 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
483 context_push_service( context, service, mlt_playlist_type );
486 static void on_end_playlist( deserialise_context context, const xmlChar *name )
488 // Get the playlist from the stack
489 enum service_type type;
490 mlt_service service = context_pop_service( context, &type );
492 if ( service != NULL && type == mlt_playlist_type )
494 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
495 mlt_position in = -1;
496 mlt_position out = -1;
498 if ( mlt_properties_get( properties, "in" ) )
499 in = mlt_properties_get_position( properties, "in" );
500 if ( mlt_properties_get( properties, "out" ) )
501 out = mlt_properties_get_position( properties, "out" );
503 // See if the playlist should be added to a playlist or multitrack
504 if ( add_producer( context, service, in, out ) == 0 )
505 context_push_service( context, service, type );
509 mlt_log_warning( service, "Invalid state of playlist end %d\n", type );
513 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
515 // use a dummy service to hold properties to allow arbitrary nesting
516 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
517 mlt_service_init( service, NULL );
519 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
521 context_push_service( context, service, mlt_dummy_producer_type );
523 for ( ; atts != NULL && *atts != NULL; atts += 2 )
524 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
527 // Parse a SMIL clock value (as produced by Kino 0.9.1) and return position in frames
528 static mlt_position parse_clock_value( char *value, double fps )
530 // This implementation expects a fully specified clock value - no optional
532 char *pos, *copy = strdup( value );
534 mlt_position result = -1;
537 pos = strchr( value, ':' );
544 pos = strchr( value, ':' );
551 pos = strchr( value, '.' );
560 result = ( fps * ( ( (hh * 3600) + (mm * 60) + ss ) * 1000 + ms ) / 1000 + 0.5 );
565 static void on_end_producer( deserialise_context context, const xmlChar *name )
567 enum service_type type;
568 mlt_service service = context_pop_service( context, &type );
569 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
571 if ( service != NULL && type == mlt_dummy_producer_type )
573 mlt_service producer = NULL;
575 qualify_property( context, properties, "resource" );
576 char *resource = trim( mlt_properties_get( properties, "resource" ) );
578 // Let Kino-SMIL src be a synonym for resource
579 if ( resource == NULL )
581 qualify_property( context, properties, "src" );
582 resource = trim( mlt_properties_get( properties, "src" ) );
585 // Instantiate the producer
586 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
588 char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
591 char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
592 strcat( temp, service_name );
594 strcat( temp, resource );
595 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
600 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
604 // Just in case the plugin requested doesn't exist...
605 if ( !producer && resource )
606 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
608 mlt_log_warning( service, "failed to load producer \"%s\"\n", resource );
610 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
612 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
615 mlt_service_close( service );
619 // Track this producer
620 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
621 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );
623 // Propagate the properties
624 qualify_property( context, properties, "resource" );
625 qualify_property( context, properties, "luma" );
626 qualify_property( context, properties, "luma.resource" );
627 qualify_property( context, properties, "composite.luma" );
628 qualify_property( context, properties, "producer.resource" );
630 // Handle in/out properties separately
631 mlt_position in = -1;
632 mlt_position out = -1;
635 if ( mlt_properties_get( properties, "in" ) != NULL )
636 in = mlt_properties_get_position( properties, "in" );
637 // Let Kino-SMIL clipBegin be a synonym for in
638 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
640 if ( strchr( mlt_properties_get( properties, "clipBegin" ), ':' ) )
642 in = parse_clock_value( mlt_properties_get( properties, "clipBegin" ),
643 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
645 // Parse frames value
646 in = mlt_properties_get_position( properties, "clipBegin" );
649 if ( mlt_properties_get( properties, "out" ) != NULL )
650 out = mlt_properties_get_position( properties, "out" );
651 // Let Kino-SMIL clipEnd be a synonym for out
652 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
654 if ( strchr( mlt_properties_get( properties, "clipEnd" ), ':' ) )
656 out = parse_clock_value( mlt_properties_get( properties, "clipEnd" ),
657 mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
659 // Parse frames value
660 out = mlt_properties_get_position( properties, "clipEnd" );
663 mlt_properties_set( properties, "in", NULL );
664 mlt_properties_set( properties, "out", NULL );
666 // Inherit the properties
667 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
669 // Attach all filters from service onto producer
670 attach_filters( producer, service );
672 // Add the producer to the producer map
673 if ( mlt_properties_get( properties, "id" ) != NULL )
674 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
676 // See if the producer should be added to a playlist or multitrack
677 if ( add_producer( context, producer, in, out ) == 0 )
679 // Otherwise, set in and out on...
680 if ( in != -1 || out != -1 )
682 // Get the parent service
683 enum service_type type;
684 mlt_service parent = context_pop_service( context, &type );
685 if ( parent != NULL )
687 // Get the parent properties
688 properties = MLT_SERVICE_PROPERTIES( parent );
690 char *resource = trim( mlt_properties_get( properties, "resource" ) );
692 // Put the parent producer back
693 context_push_service( context, parent, type );
695 // If the parent is a track or entry
696 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
698 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
699 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
703 // Otherwise, set in and out on producer directly
704 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
709 // Otherwise, set in and out on producer directly
710 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
714 // Push the producer onto the stack
715 context_push_service( context, producer, mlt_producer_type );
718 mlt_service_close( service );
722 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
724 // Get the playlist from the stack
725 enum service_type type;
726 mlt_service service = context_pop_service( context, &type );
727 mlt_position length = 0;
729 if ( type == mlt_playlist_type && service != NULL )
731 // Look for the length attribute
732 for ( ; atts != NULL && *atts != NULL; atts += 2 )
734 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
736 length = atoll( _s(atts[1]) );
741 // Append a blank to the playlist
742 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
744 // Push the playlist back onto the stack
745 context_push_service( context, service, type );
749 mlt_log_warning( service, "blank without a playlist - a definite no no\n" );
753 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
755 mlt_producer entry = NULL;
756 mlt_properties temp = mlt_properties_new( );
758 for ( ; atts != NULL && *atts != NULL; atts += 2 )
760 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
762 // Look for the producer attribute
763 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
765 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
766 if ( producer != NULL )
767 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
771 // If we have a valid entry
772 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
774 mlt_playlist_clip_info info;
775 enum service_type parent_type = invalid_type;
776 mlt_service parent = context_pop_service( context, &parent_type );
777 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
779 if ( parent_type == mlt_playlist_type )
781 // Append the producer to the playlist
782 mlt_position in = -1;
783 mlt_position out = -1;
784 if ( mlt_properties_get( temp, "in" ) )
785 in = mlt_properties_get_position( temp, "in" );
786 if ( mlt_properties_get( temp, "out" ) )
787 out = mlt_properties_get_position( temp, "out" );
788 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
790 // Handle the repeat property
791 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
793 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
794 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
795 mlt_properties_get_int( temp, "repeat" ) );
798 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
803 mlt_log_warning( parent, "Entry not part of a playlist...\n" );
806 context_push_service( context, parent, parent_type );
809 // Push the cut onto the stack
810 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
812 mlt_properties_close( temp );
815 static void on_end_entry( deserialise_context context, const xmlChar *name )
817 // Get the entry from the stack
818 enum service_type entry_type = invalid_type;
819 mlt_service entry = context_pop_service( context, &entry_type );
821 if ( entry == NULL && entry_type != mlt_entry_type )
823 mlt_log_warning( entry, "Invalid state at end of entry\n" );
827 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
829 // use a dummy service to hold properties to allow arbitrary nesting
830 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
831 mlt_service_init( service, NULL );
833 // Push the dummy service onto the stack
834 context_push_service( context, service, mlt_entry_type );
836 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
838 for ( ; atts != NULL && *atts != NULL; atts += 2 )
840 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
842 // Look for the producer attribute
843 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
845 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
846 if ( producer != NULL )
847 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
852 static void on_end_track( deserialise_context context, const xmlChar *name )
854 // Get the track from the stack
855 enum service_type track_type;
856 mlt_service track = context_pop_service( context, &track_type );
858 if ( track != NULL && track_type == mlt_entry_type )
860 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
861 enum service_type parent_type = invalid_type;
862 mlt_service parent = context_pop_service( context, &parent_type );
863 mlt_multitrack multitrack = NULL;
865 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
866 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
868 if ( parent_type == mlt_tractor_type )
869 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
870 else if ( parent_type == mlt_multitrack_type )
871 multitrack = MLT_MULTITRACK( parent );
873 mlt_log_warning( track, "track contained in an invalid container\n" );
875 if ( multitrack != NULL )
877 // Set producer i/o if specified
878 if ( mlt_properties_get( track_props, "in" ) != NULL ||
879 mlt_properties_get( track_props, "out" ) != NULL )
881 mlt_position in = -1;
882 mlt_position out = -1;
883 if ( mlt_properties_get( track_props, "in" ) )
884 in = mlt_properties_get_position( track_props, "in" );
885 if ( mlt_properties_get( track_props, "out" ) )
886 out = mlt_properties_get_position( track_props, "out" );
887 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
888 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
889 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
890 track_props = MLT_PRODUCER_PROPERTIES( cut );
891 mlt_producer_close( cut );
895 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
898 // Set the hide state of the track producer
899 char *hide_s = mlt_properties_get( track_props, "hide" );
900 if ( hide_s != NULL )
902 if ( strcmp( hide_s, "video" ) == 0 )
903 mlt_properties_set_int( producer_props, "hide", 1 );
904 else if ( strcmp( hide_s, "audio" ) == 0 )
905 mlt_properties_set_int( producer_props, "hide", 2 );
906 else if ( strcmp( hide_s, "both" ) == 0 )
907 mlt_properties_set_int( producer_props, "hide", 3 );
911 if ( parent != NULL )
912 context_push_service( context, parent, parent_type );
914 mlt_service_close( track );
918 mlt_log_warning( track, "Invalid state at end of track\n" );
922 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
924 // use a dummy service to hold properties to allow arbitrary nesting
925 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
926 mlt_service_init( service, NULL );
928 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
930 context_push_service( context, service, mlt_dummy_filter_type );
932 // Set the properties
933 for ( ; atts != NULL && *atts != NULL; atts += 2 )
934 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
937 static void on_end_filter( deserialise_context context, const xmlChar *name )
939 enum service_type type;
940 mlt_service service = context_pop_service( context, &type );
941 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
943 enum service_type parent_type = invalid_type;
944 mlt_service parent = context_pop_service( context, &parent_type );
946 if ( service != NULL && type == mlt_dummy_filter_type )
948 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
949 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
950 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
954 mlt_log_warning( service, "failed to load filter \"%s\"\n", id );
956 context_push_service( context, parent, parent_type );
957 mlt_service_close( service );
961 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
962 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
964 // Propogate the properties
965 qualify_property( context, properties, "resource" );
966 qualify_property( context, properties, "luma" );
967 qualify_property( context, properties, "luma.resource" );
968 qualify_property( context, properties, "composite.luma" );
969 qualify_property( context, properties, "producer.resource" );
970 mlt_properties_inherit( filter_props, properties );
972 // Attach all filters from service onto filter
973 attach_filters( filter, service );
975 // Associate the filter with the parent
976 if ( parent != NULL )
978 if ( parent_type == mlt_tractor_type )
980 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
981 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
982 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
983 mlt_properties_get_int( properties, "in" ),
984 mlt_properties_get_int( properties, "out" ) );
988 mlt_service_attach( parent, MLT_FILTER( filter ) );
991 // Put the parent back on the stack
992 context_push_service( context, parent, parent_type );
996 mlt_log_warning( service, "filter closed with invalid parent...\n" );
999 // Close the dummy filter service
1000 mlt_service_close( service );
1004 mlt_log_warning( service, "Invalid top of stack on filter close\n" );
1008 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1010 // use a dummy service to hold properties to allow arbitrary nesting
1011 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
1012 mlt_service_init( service, NULL );
1014 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1016 context_push_service( context, service, mlt_dummy_transition_type );
1018 // Set the properties
1019 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1020 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1023 static void on_end_transition( deserialise_context context, const xmlChar *name )
1025 enum service_type type;
1026 mlt_service service = context_pop_service( context, &type );
1027 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1029 enum service_type parent_type = invalid_type;
1030 mlt_service parent = context_pop_service( context, &parent_type );
1032 if ( service != NULL && type == mlt_dummy_transition_type )
1034 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1035 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
1036 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
1040 mlt_log_warning( service, "failed to load transition \"%s\"\n", id );
1042 context_push_service( context, parent, parent_type );
1043 mlt_service_close( service );
1046 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1047 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1049 // Propogate the properties
1050 qualify_property( context, properties, "resource" );
1051 qualify_property( context, properties, "luma" );
1052 qualify_property( context, properties, "luma.resource" );
1053 qualify_property( context, properties, "composite.luma" );
1054 qualify_property( context, properties, "producer.resource" );
1055 mlt_properties_inherit( effect_props, properties );
1057 // Attach all filters from service onto effect
1058 attach_filters( effect, service );
1060 // Associate the filter with the parent
1061 if ( parent != NULL )
1063 if ( parent_type == mlt_tractor_type )
1065 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1066 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1067 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1068 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1069 mlt_properties_get_int( properties, "a_track" ),
1070 mlt_properties_get_int( properties, "b_track" ) );
1071 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1072 mlt_properties_get_int( properties, "in" ),
1073 mlt_properties_get_int( properties, "out" ) );
1077 mlt_log_warning( service, "Misplaced transition - ignoring\n" );
1080 // Put the parent back on the stack
1081 context_push_service( context, parent, parent_type );
1085 mlt_log_warning( service, "transition closed with invalid parent...\n" );
1088 // Close the dummy filter service
1089 mlt_service_close( service );
1093 mlt_log_warning( service, "Invalid top of stack on transition close\n" );
1097 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1099 if ( context->pass == 1 )
1101 mlt_consumer consumer = mlt_consumer_new( context->profile );
1102 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1104 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1105 context_push_service( context, MLT_CONSUMER_SERVICE(consumer), mlt_dummy_consumer_type );
1107 // Set the properties from attributes
1108 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1109 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1113 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1115 if ( context->pass == 1 )
1117 // Get the consumer from the stack
1118 enum service_type type;
1119 mlt_service service = context_pop_service( context, &type );
1121 if ( service && type == mlt_dummy_consumer_type )
1123 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1124 qualify_property( context, properties, "resource" );
1125 qualify_property( context, properties, "target" );
1126 char *resource = trim( mlt_properties_get( properties, "resource" ) );
1128 if ( context->multi_consumer > 1 )
1130 // Instantiate the multi consumer
1131 if ( !context->consumer )
1133 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1134 if ( context->consumer )
1136 // Track this consumer
1137 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1138 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1141 if ( context->consumer )
1143 // Set this service instance on multi consumer
1145 snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1146 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, service, 0,
1147 (mlt_destructor) mlt_service_close, NULL );
1152 // Instantiate the consumer
1153 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1154 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1155 if ( context->consumer )
1157 // Track this consumer
1158 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1159 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1161 // Inherit the properties
1162 mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1165 mlt_service_close( service );
1171 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1173 enum service_type type;
1174 mlt_service service = context_pop_service( context, &type );
1175 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1176 const char *value = NULL;
1178 if ( service != NULL )
1180 // Set the properties
1181 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1183 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1184 context->property = strdup( _s(atts[ 1 ]) );
1185 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1186 value = _s(atts[ 1 ]);
1189 if ( context->property != NULL )
1190 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1192 // Tell parser to collect any further nodes for serialisation
1193 context->is_value = 1;
1195 context_push_service( context, service, type );
1199 mlt_log_warning( service, "Property without a service '%s'?\n", ( const char * )name );
1203 static void on_end_property( deserialise_context context, const xmlChar *name )
1205 enum service_type type;
1206 mlt_service service = context_pop_service( context, &type );
1207 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1209 if ( service != NULL )
1211 // Tell parser to stop building a tree
1212 context->is_value = 0;
1214 // See if there is a xml tree for the value
1215 if ( context->property != NULL && context->value_doc != NULL )
1220 // Serialise the tree to get value
1221 xmlDocDumpMemory( context->value_doc, &value, &size );
1222 mlt_properties_set( properties, context->property, _s(value) );
1224 xmlFreeFunc xmlFree = NULL;
1225 xmlMemGet( &xmlFree, NULL, NULL, NULL);
1228 xmlFreeDoc( context->value_doc );
1229 context->value_doc = NULL;
1232 // Close this property handling
1233 free( context->property );
1234 context->property = NULL;
1236 context_push_service( context, service, type );
1240 mlt_log_warning( service, "Property without a service '%s'??\n", (const char *)name );
1244 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1246 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1247 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1249 //printf("on_start_element: %s\n", name );
1250 if ( context->pass == 0 )
1252 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1253 xmlStrcmp( name, _x("profile") ) == 0 ||
1254 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1255 on_start_profile( context, name, atts );
1256 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1257 context->multi_consumer++;
1260 context->branch[ context->depth ] ++;
1263 // Build a tree from nodes within a property value
1264 if ( context->is_value == 1 && context->pass == 1 )
1266 xmlNodePtr node = xmlNewNode( NULL, name );
1268 if ( context->value_doc == NULL )
1271 context->value_doc = xmlNewDoc( _x("1.0") );
1272 xmlDocSetRootElement( context->value_doc, node );
1276 // Append child to tree
1277 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1279 context_push_node( context, node );
1281 // Set the attributes
1282 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1283 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1285 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1286 on_start_tractor( context, name, atts );
1287 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1288 on_start_multitrack( context, name, atts );
1289 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1290 on_start_playlist( context, name, atts );
1291 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1292 on_start_producer( context, name, atts );
1293 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1294 on_start_blank( context, name, atts );
1295 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1296 on_start_entry( context, name, atts );
1297 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1298 on_start_track( context, name, atts );
1299 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1300 on_start_filter( context, name, atts );
1301 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1302 on_start_transition( context, name, atts );
1303 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1304 on_start_property( context, name, atts );
1305 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1306 on_start_consumer( context, name, atts );
1307 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1309 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1311 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1312 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1313 else if ( !context->lc_numeric )
1314 context->lc_numeric = strdup( _s( atts[1] ) );
1319 static void on_end_element( void *ctx, const xmlChar *name )
1321 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1322 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1324 //printf("on_end_element: %s\n", name );
1325 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1326 context_pop_node( context );
1327 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1328 on_end_multitrack( context, name );
1329 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1330 on_end_playlist( context, name );
1331 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1332 on_end_track( context, name );
1333 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1334 on_end_entry( context, name );
1335 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1336 on_end_tractor( context, name );
1337 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1338 on_end_property( context, name );
1339 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1340 on_end_producer( context, name );
1341 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1342 on_end_filter( context, name );
1343 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1344 on_end_transition( context, name );
1345 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1346 on_end_consumer( context, name );
1348 context->branch[ context->depth ] = 0;
1352 static void on_characters( void *ctx, const xmlChar *ch, int len )
1354 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1355 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1356 char *value = calloc( len + 1, 1 );
1357 enum service_type type;
1358 mlt_service service = context_pop_service( context, &type );
1359 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1361 if ( service != NULL )
1362 context_push_service( context, service, type );
1365 strncpy( value, (const char*) ch, len );
1367 if ( context->stack_node_size > 0 )
1368 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1370 // libxml2 generates an on_characters immediately after a get_entity within
1371 // an element value, and we ignore it because it is called again during
1372 // actual substitution.
1373 else if ( context->property != NULL && context->entity_is_replace == 0 )
1375 char *s = mlt_properties_get( properties, context->property );
1378 // Append new text to existing content
1379 char *new = calloc( strlen( s ) + len + 1, 1 );
1381 strcat( new, value );
1382 mlt_properties_set( properties, context->property, new );
1386 mlt_properties_set( properties, context->property, value );
1388 context->entity_is_replace = 0;
1393 /** Convert parameters parsed from resource into entity declarations.
1395 static void params_to_entities( deserialise_context context )
1397 if ( context->params != NULL )
1401 // Add our params as entitiy declarations
1402 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1404 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1405 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1406 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1410 mlt_properties_close( context->params );
1411 context->params = NULL;
1415 // The following 3 facilitate entity substitution in the SAX parser
1416 static void on_internal_subset( void *ctx, const xmlChar* name,
1417 const xmlChar* publicId, const xmlChar* systemId )
1419 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1420 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1422 context->publicId = publicId;
1423 context->systemId = systemId;
1424 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1426 // Override default entities with our parameters
1427 params_to_entities( context );
1430 // TODO: Check this with Dan... I think this is for parameterisation
1431 // but it's breaking standard escaped entities (like < etc).
1432 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1433 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1435 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1436 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1438 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1441 // TODO: Check this functionality (see on_entity_declaration)
1442 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1444 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1445 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1446 xmlEntityPtr e = NULL;
1448 // Setup for entity declarations if not ready
1449 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1451 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1452 context->publicId = _x("");
1453 context->systemId = _x("");
1456 // Add our parameters if not already
1457 params_to_entities( context );
1459 e = xmlGetPredefinedEntity( name );
1461 // Send signal to on_characters that an entity substitutin is pending
1464 e = xmlGetDocEntity( context->entity_doc, name );
1466 context->entity_is_replace = 1;
1472 /** Handle error messages from the parser.
1474 static void on_error( void * ctx, const char * msg, ...)
1476 struct _xmlError* err_ptr = xmlCtxtGetLastError(ctx);
1477 mlt_log_error( NULL, "XML producer parse error: %s\trow: %d\tcol: %d\n",
1478 err_ptr->message, err_ptr->line, err_ptr->int2 );
1481 /** Convert a hexadecimal character to its value.
1483 static int tohex( char p )
1485 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1488 /** Decode a url-encoded string containing hexadecimal character sequences.
1490 static char *url_decode( char *dest, char *src )
1498 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1511 /** Extract the filename from a URL attaching parameters to a properties list.
1513 static void parse_url( mlt_properties properties, char *url )
1516 int n = strlen( url );
1520 for ( i = 0; i < n; i++ )
1537 if ( name != NULL && value != NULL )
1538 mlt_properties_set( properties, name, value );
1544 if ( name != NULL && value != NULL )
1545 mlt_properties_set( properties, name, value );
1548 // Quick workaround to avoid unecessary libxml2 warnings
1549 static int file_exists( char *file )
1551 char *name = strdup( file );
1553 if ( name != NULL && strchr( name, '?' ) )
1554 *( strchr( name, '?' ) ) = '\0';
1557 FILE *f = fopen( name, "r" );
1559 if ( exists ) fclose( f );
1565 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1567 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1568 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1569 mlt_properties properties = NULL;
1571 struct _xmlParserCtxt *xmlcontext;
1572 int well_formed = 0;
1573 char *filename = NULL;
1574 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1576 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1579 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1580 if ( context == NULL )
1583 context->producer_map = mlt_properties_new();
1584 context->destructors = mlt_properties_new();
1585 context->params = mlt_properties_new();
1586 context->profile = profile;
1588 // Decode URL and parse parameters
1589 mlt_properties_set( context->producer_map, "root", "" );
1592 filename = strdup( data );
1593 parse_url( context->params, url_decode( filename, data ) );
1595 // We need the directory prefix which was used for the xml
1596 if ( strchr( filename, '/' ) )
1599 mlt_properties_set( context->producer_map, "root", filename );
1600 root = mlt_properties_get( context->producer_map, "root" );
1601 *( strrchr( root, '/' ) ) = '\0';
1603 // If we don't have an absolute path here, we're heading for disaster...
1604 if ( root[ 0 ] != '/' )
1606 char *cwd = getcwd( NULL, 0 );
1607 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1608 sprintf( real, "%s/%s", cwd, root );
1609 mlt_properties_set( context->producer_map, "root", real );
1616 // We need to track the number of registered filters
1617 mlt_properties_set_int( context->destructors, "registered", 0 );
1619 // Setup libxml2 SAX parsing
1621 xmlSubstituteEntitiesDefault( 1 );
1622 // This is used to facilitate entity substitution in the SAX parser
1623 context->entity_doc = xmlNewDoc( _x("1.0") );
1625 xmlcontext = xmlCreateFileParserCtxt( filename );
1627 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1629 // Invalid context - clean up and return NULL
1630 if ( xmlcontext == NULL )
1632 mlt_properties_close( context->producer_map );
1633 mlt_properties_close( context->destructors );
1634 mlt_properties_close( context->params );
1642 xmlcontext->sax = sax;
1643 xmlcontext->_private = ( void* )context;
1644 xmlParseDocument( xmlcontext );
1646 // Cleanup after parsing
1647 xmlcontext->sax = NULL;
1648 xmlcontext->_private = NULL;
1649 xmlFreeParserCtxt( xmlcontext );
1650 context->stack_node_size = 0;
1651 context->stack_service_size = 0;
1653 // Setup the second pass
1656 xmlcontext = xmlCreateFileParserCtxt( filename );
1658 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1660 // Invalid context - clean up and return NULL
1661 if ( xmlcontext == NULL )
1663 mlt_properties_close( context->producer_map );
1664 mlt_properties_close( context->destructors );
1665 mlt_properties_close( context->params );
1666 xmlFreeDoc( context->entity_doc );
1673 // Setup SAX callbacks
1674 sax->startElement = on_start_element;
1675 sax->endElement = on_end_element;
1676 sax->characters = on_characters;
1677 sax->cdataBlock = on_characters;
1678 sax->internalSubset = on_internal_subset;
1679 sax->entityDecl = on_entity_declaration;
1680 sax->getEntity = on_get_entity;
1681 sax->error = on_error;
1682 sax->fatalError = on_error;
1685 xmlcontext->sax = sax;
1686 xmlcontext->_private = ( void* )context;
1687 xmlParseDocument( xmlcontext );
1688 well_formed = xmlcontext->wellFormed;
1690 // Cleanup after parsing
1691 xmlFreeDoc( context->entity_doc );
1693 xmlMemoryDump( ); // for debugging
1695 // Get the last producer on the stack
1696 enum service_type type;
1697 mlt_service service = context_pop_service( context, &type );
1698 if ( well_formed && service != NULL )
1700 // Verify it is a producer service (mlt_type="mlt_producer")
1701 // (producer, playlist, multitrack)
1702 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1703 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1708 xmlDocPtr doc = xml_make_doc( service );
1709 xmlDocFormatDump( stdout, doc, 1 );
1714 if ( well_formed && service != NULL )
1716 char *title = mlt_properties_get( context->producer_map, "title" );
1718 // Need the complete producer list for various reasons
1719 properties = context->destructors;
1721 // Now make sure we don't have a reference to the service in the properties
1722 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1724 char *name = mlt_properties_get_name( properties, i );
1725 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1727 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1732 // We are done referencing destructor property list
1733 // Set this var to service properties for convenience
1734 properties = MLT_SERVICE_PROPERTIES( service );
1737 mlt_properties_set( properties, "title", title );
1739 // Optimise for overlapping producers
1740 mlt_producer_optimise( MLT_PRODUCER( service ) );
1742 // Handle deep copies
1743 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1745 // Now assign additional properties
1747 mlt_properties_set( properties, "resource", data );
1749 // This tells consumer_xml not to deep copy
1750 mlt_properties_set( properties, "xml", "was here" );
1754 // Allow the project to be edited
1755 mlt_properties_set( properties, "_xml", "was here" );
1756 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1759 // Make consumer available
1760 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1761 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1762 (mlt_destructor) mlt_consumer_close, NULL );
1766 // Return null if not well formed
1771 mlt_properties_close( context->producer_map );
1772 if ( context->params != NULL )
1773 mlt_properties_close( context->params );
1774 mlt_properties_close( context->destructors );
1775 if ( context->lc_numeric )
1776 free( context->lc_numeric );
1780 return MLT_PRODUCER( service );