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( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
999 // Close the dummy filter service
1000 mlt_service_close( service );
1004 mlt_log_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1080 // Put the parent back on the stack
1081 context_push_service( context, parent, parent_type );
1085 mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1088 // Close the dummy filter service
1089 mlt_service_close( service );
1093 mlt_log_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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_error( NULL, "[producer_xml] 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 if ( context->pass == 0 )
1251 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1252 xmlStrcmp( name, _x("profile") ) == 0 ||
1253 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1254 on_start_profile( context, name, atts );
1255 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1256 context->multi_consumer++;
1259 context->branch[ context->depth ] ++;
1262 // Build a tree from nodes within a property value
1263 if ( context->is_value == 1 && context->pass == 1 )
1265 xmlNodePtr node = xmlNewNode( NULL, name );
1267 if ( context->value_doc == NULL )
1270 context->value_doc = xmlNewDoc( _x("1.0") );
1271 xmlDocSetRootElement( context->value_doc, node );
1275 // Append child to tree
1276 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1278 context_push_node( context, node );
1280 // Set the attributes
1281 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1282 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1284 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1285 on_start_tractor( context, name, atts );
1286 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1287 on_start_multitrack( context, name, atts );
1288 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1289 on_start_playlist( context, name, atts );
1290 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1291 on_start_producer( context, name, atts );
1292 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1293 on_start_blank( context, name, atts );
1294 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1295 on_start_entry( context, name, atts );
1296 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1297 on_start_track( context, name, atts );
1298 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1299 on_start_filter( context, name, atts );
1300 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1301 on_start_transition( context, name, atts );
1302 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1303 on_start_property( context, name, atts );
1304 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1305 on_start_consumer( context, name, atts );
1306 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1308 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1310 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1311 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1312 else if ( !context->lc_numeric )
1313 context->lc_numeric = strdup( _s( atts[1] ) );
1318 static void on_end_element( void *ctx, const xmlChar *name )
1320 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1321 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1323 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1324 context_pop_node( context );
1325 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1326 on_end_multitrack( context, name );
1327 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1328 on_end_playlist( context, name );
1329 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1330 on_end_track( context, name );
1331 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1332 on_end_entry( context, name );
1333 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1334 on_end_tractor( context, name );
1335 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1336 on_end_property( context, name );
1337 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1338 on_end_producer( context, name );
1339 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1340 on_end_filter( context, name );
1341 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1342 on_end_transition( context, name );
1343 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1344 on_end_consumer( context, name );
1346 context->branch[ context->depth ] = 0;
1350 static void on_characters( void *ctx, const xmlChar *ch, int len )
1352 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1353 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1354 char *value = calloc( len + 1, 1 );
1355 enum service_type type;
1356 mlt_service service = context_pop_service( context, &type );
1357 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1359 if ( service != NULL )
1360 context_push_service( context, service, type );
1363 strncpy( value, (const char*) ch, len );
1365 if ( context->stack_node_size > 0 )
1366 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1368 // libxml2 generates an on_characters immediately after a get_entity within
1369 // an element value, and we ignore it because it is called again during
1370 // actual substitution.
1371 else if ( context->property != NULL && context->entity_is_replace == 0 )
1373 char *s = mlt_properties_get( properties, context->property );
1376 // Append new text to existing content
1377 char *new = calloc( strlen( s ) + len + 1, 1 );
1379 strcat( new, value );
1380 mlt_properties_set( properties, context->property, new );
1384 mlt_properties_set( properties, context->property, value );
1386 context->entity_is_replace = 0;
1391 /** Convert parameters parsed from resource into entity declarations.
1393 static void params_to_entities( deserialise_context context )
1395 if ( context->params != NULL )
1399 // Add our params as entitiy declarations
1400 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1402 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1403 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1404 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1408 mlt_properties_close( context->params );
1409 context->params = NULL;
1413 // The following 3 facilitate entity substitution in the SAX parser
1414 static void on_internal_subset( void *ctx, const xmlChar* name,
1415 const xmlChar* publicId, const xmlChar* systemId )
1417 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1418 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1420 context->publicId = publicId;
1421 context->systemId = systemId;
1422 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1424 // Override default entities with our parameters
1425 params_to_entities( context );
1428 // TODO: Check this with Dan... I think this is for parameterisation
1429 // but it's breaking standard escaped entities (like < etc).
1430 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1431 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1433 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1434 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1436 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1439 // TODO: Check this functionality (see on_entity_declaration)
1440 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1442 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1443 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1444 xmlEntityPtr e = NULL;
1446 // Setup for entity declarations if not ready
1447 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1449 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1450 context->publicId = _x("");
1451 context->systemId = _x("");
1454 // Add our parameters if not already
1455 params_to_entities( context );
1457 e = xmlGetPredefinedEntity( name );
1459 // Send signal to on_characters that an entity substitutin is pending
1462 e = xmlGetDocEntity( context->entity_doc, name );
1464 context->entity_is_replace = 1;
1470 static void on_error( void * ctx, const char * msg, ... )
1472 struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1474 switch( err_ptr->level )
1476 case XML_ERR_WARNING:
1477 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1478 err_ptr->message, err_ptr->line, err_ptr->int2 );
1481 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1482 err_ptr->message, err_ptr->line, err_ptr->int2 );
1486 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1487 err_ptr->message, err_ptr->line, err_ptr->int2 );
1492 /** Convert a hexadecimal character to its value.
1494 static int tohex( char p )
1496 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1499 /** Decode a url-encoded string containing hexadecimal character sequences.
1501 static char *url_decode( char *dest, char *src )
1509 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1522 /** Extract the filename from a URL attaching parameters to a properties list.
1524 static void parse_url( mlt_properties properties, char *url )
1527 int n = strlen( url );
1531 for ( i = 0; i < n; i++ )
1548 if ( name != NULL && value != NULL )
1549 mlt_properties_set( properties, name, value );
1555 if ( name != NULL && value != NULL )
1556 mlt_properties_set( properties, name, value );
1559 // Quick workaround to avoid unecessary libxml2 warnings
1560 static int file_exists( char *file )
1562 char *name = strdup( file );
1564 if ( name != NULL && strchr( name, '?' ) )
1565 *( strchr( name, '?' ) ) = '\0';
1568 FILE *f = fopen( name, "r" );
1570 if ( exists ) fclose( f );
1576 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1578 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1579 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1580 mlt_properties properties = NULL;
1582 struct _xmlParserCtxt *xmlcontext;
1583 int well_formed = 0;
1584 char *filename = NULL;
1585 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1587 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1590 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1591 if ( context == NULL )
1594 context->producer_map = mlt_properties_new();
1595 context->destructors = mlt_properties_new();
1596 context->params = mlt_properties_new();
1597 context->profile = profile;
1599 // Decode URL and parse parameters
1600 mlt_properties_set( context->producer_map, "root", "" );
1603 filename = strdup( data );
1604 parse_url( context->params, url_decode( filename, data ) );
1606 // We need the directory prefix which was used for the xml
1607 if ( strchr( filename, '/' ) )
1610 mlt_properties_set( context->producer_map, "root", filename );
1611 root = mlt_properties_get( context->producer_map, "root" );
1612 *( strrchr( root, '/' ) ) = '\0';
1614 // If we don't have an absolute path here, we're heading for disaster...
1615 if ( root[ 0 ] != '/' )
1617 char *cwd = getcwd( NULL, 0 );
1618 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1619 sprintf( real, "%s/%s", cwd, root );
1620 mlt_properties_set( context->producer_map, "root", real );
1627 // We need to track the number of registered filters
1628 mlt_properties_set_int( context->destructors, "registered", 0 );
1630 // Setup SAX callbacks for first pass
1631 sax->startElement = on_start_element;
1632 sax->warning = on_error;
1633 sax->error = on_error;
1634 sax->fatalError = on_error;
1636 // Setup libxml2 SAX parsing
1638 xmlSubstituteEntitiesDefault( 1 );
1639 // This is used to facilitate entity substitution in the SAX parser
1640 context->entity_doc = xmlNewDoc( _x("1.0") );
1642 xmlcontext = xmlCreateFileParserCtxt( filename );
1644 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1646 // Invalid context - clean up and return NULL
1647 if ( xmlcontext == NULL )
1649 mlt_properties_close( context->producer_map );
1650 mlt_properties_close( context->destructors );
1651 mlt_properties_close( context->params );
1659 xmlcontext->sax = sax;
1660 xmlcontext->_private = ( void* )context;
1661 xmlParseDocument( xmlcontext );
1662 well_formed = xmlcontext->wellFormed;
1664 // Cleanup after parsing
1665 xmlcontext->sax = NULL;
1666 xmlcontext->_private = NULL;
1667 xmlFreeParserCtxt( xmlcontext );
1668 context->stack_node_size = 0;
1669 context->stack_service_size = 0;
1671 // Bad xml - clean up and return NULL
1674 mlt_properties_close( context->producer_map );
1675 mlt_properties_close( context->destructors );
1676 mlt_properties_close( context->params );
1677 xmlFreeDoc( context->entity_doc );
1684 // Setup the second pass
1687 xmlcontext = xmlCreateFileParserCtxt( filename );
1689 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1691 // Invalid context - clean up and return NULL
1692 if ( xmlcontext == NULL )
1694 mlt_properties_close( context->producer_map );
1695 mlt_properties_close( context->destructors );
1696 mlt_properties_close( context->params );
1697 xmlFreeDoc( context->entity_doc );
1704 // Setup SAX callbacks for second pass
1705 sax->endElement = on_end_element;
1706 sax->characters = on_characters;
1707 sax->cdataBlock = on_characters;
1708 sax->internalSubset = on_internal_subset;
1709 sax->entityDecl = on_entity_declaration;
1710 sax->getEntity = on_get_entity;
1713 xmlcontext->sax = sax;
1714 xmlcontext->_private = ( void* )context;
1715 xmlParseDocument( xmlcontext );
1716 well_formed = xmlcontext->wellFormed;
1718 // Cleanup after parsing
1719 xmlFreeDoc( context->entity_doc );
1721 xmlMemoryDump( ); // for debugging
1723 // Get the last producer on the stack
1724 enum service_type type;
1725 mlt_service service = context_pop_service( context, &type );
1726 if ( well_formed && service != NULL )
1728 // Verify it is a producer service (mlt_type="mlt_producer")
1729 // (producer, playlist, multitrack)
1730 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1731 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1736 xmlDocPtr doc = xml_make_doc( service );
1737 xmlDocFormatDump( stdout, doc, 1 );
1742 if ( well_formed && service != NULL )
1744 char *title = mlt_properties_get( context->producer_map, "title" );
1746 // Need the complete producer list for various reasons
1747 properties = context->destructors;
1749 // Now make sure we don't have a reference to the service in the properties
1750 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1752 char *name = mlt_properties_get_name( properties, i );
1753 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1755 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1760 // We are done referencing destructor property list
1761 // Set this var to service properties for convenience
1762 properties = MLT_SERVICE_PROPERTIES( service );
1765 mlt_properties_set( properties, "title", title );
1767 // Optimise for overlapping producers
1768 mlt_producer_optimise( MLT_PRODUCER( service ) );
1770 // Handle deep copies
1771 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1773 // Now assign additional properties
1775 mlt_properties_set( properties, "resource", data );
1777 // This tells consumer_xml not to deep copy
1778 mlt_properties_set( properties, "xml", "was here" );
1782 // Allow the project to be edited
1783 mlt_properties_set( properties, "_xml", "was here" );
1784 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1787 // Make consumer available
1788 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1789 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1790 (mlt_destructor) mlt_consumer_close, NULL );
1794 // Return null if not well formed
1799 mlt_properties_close( context->producer_map );
1800 if ( context->params != NULL )
1801 mlt_properties_close( context->params );
1802 mlt_properties_close( context->destructors );
1803 if ( context->lc_numeric )
1804 free( context->lc_numeric );
1808 return MLT_PRODUCER( service );