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;
94 typedef struct deserialise_context_s *deserialise_context;
96 /** Trim the leading and trailing whitespace from a string in-place.
98 static char* trim( char *s )
101 if ( s && ( n = strlen( s ) ) )
104 while ( i < n && isspace( s[i] ) ) i++;
105 while ( --n && isspace( s[n] ) );
108 memmove( s, s + i, n );
114 /** Convert the numerical current branch address to a dot-delimited string.
116 static char *serialise_branch( deserialise_context context, char *s )
121 for ( i = 0; i < context->depth; i++ )
123 int len = strlen( s );
124 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
132 static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
134 int ret = context->stack_service_size >= STACK_SIZE - 1;
137 context->stack_service[ context->stack_service_size ] = that;
138 context->stack_types[ context->stack_service_size++ ] = type;
140 // Record the tree branch on which this service lives
141 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
143 char s[ BRANCH_SIG_LEN ];
144 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
153 static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
155 mlt_service result = NULL;
157 if ( type ) *type = mlt_invalid_type;
158 if ( context->stack_service_size > 0 )
160 result = context->stack_service[ -- context->stack_service_size ];
162 *type = context->stack_types[ context->stack_service_size ];
163 // Set the service's profile and locale so mlt_property time-to-position conversions can get fps
166 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( result ), "_profile", context->profile, 0, NULL, NULL );
167 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( result ), context->lc_numeric );
176 static int context_push_node( deserialise_context context, xmlNodePtr node )
178 int ret = context->stack_node_size >= STACK_SIZE - 1;
180 context->stack_node[ context->stack_node_size ++ ] = node;
187 static xmlNodePtr context_pop_node( deserialise_context context )
189 xmlNodePtr result = NULL;
190 if ( context->stack_node_size > 0 )
191 result = context->stack_node[ -- context->stack_node_size ];
196 // Set the destructor on a new service
197 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
199 int registered = mlt_properties_get_int( properties, "registered" );
200 char *key = mlt_properties_get( properties, "registered" );
201 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
202 mlt_properties_set_int( properties, "registered", ++ registered );
206 // Prepend the property value with the document root
207 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
209 char *resource = mlt_properties_get( properties, name );
210 if ( resource != NULL && resource[0] )
212 // Qualify file name properties
213 char *root = mlt_properties_get( context->producer_map, "root" );
214 if ( root != NULL && strcmp( root, "" ) )
216 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
217 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
219 strcpy( full_resource, root );
220 strcat( full_resource, "/" );
221 strcat( full_resource, resource );
225 strcpy( full_resource, resource );
227 mlt_properties_set( properties, name, full_resource );
228 free( full_resource );
234 /** This function adds a producer to a playlist or multitrack when
235 there is no entry or track element.
238 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
240 // Return value (0 = service remains top of stack, 1 means it can be removed)
243 // Get the parent producer
244 enum service_type type = mlt_invalid_type;
245 mlt_service container = context_pop_service( context, &type );
248 if ( service != NULL && container != NULL )
250 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
251 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
252 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
257 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
258 char *hide_s = mlt_properties_get( properties, "hide" );
260 // Indicate that this service is no longer top of stack
265 case mlt_tractor_type:
267 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
268 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
271 case mlt_multitrack_type:
273 mlt_multitrack_connect( MLT_MULTITRACK( container ),
274 MLT_PRODUCER( service ),
275 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
278 case mlt_playlist_type:
280 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
285 mlt_log_warning( NULL, "[producer_xml] Producer defined inside something that isn't a container\n" );
289 // Set the hide state of the track producer
290 if ( hide_s != NULL )
292 if ( strcmp( hide_s, "video" ) == 0 )
293 mlt_properties_set_int( properties, "hide", 1 );
294 else if ( strcmp( hide_s, "audio" ) == 0 )
295 mlt_properties_set_int( properties, "hide", 2 );
296 else if ( strcmp( hide_s, "both" ) == 0 )
297 mlt_properties_set_int( properties, "hide", 3 );
301 // Put the parent producer back
302 if ( container != NULL )
303 context_push_service( context, container, type );
308 /** Attach filters defined on that to this.
311 static void attach_filters( mlt_service service, mlt_service that )
316 mlt_filter filter = NULL;
317 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
319 mlt_service_attach( service, filter );
320 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
325 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
327 mlt_profile p = context->profile;
328 for ( ; atts != NULL && *atts != NULL; atts += 2 )
330 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
332 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
335 p->description = strdup( my_profile->description );
336 p->display_aspect_den = my_profile->display_aspect_den;
337 p->display_aspect_num = my_profile->display_aspect_num;
338 p->frame_rate_den = my_profile->frame_rate_den;
339 p->frame_rate_num = my_profile->frame_rate_num;
340 p->width = my_profile->width;
341 p->height = my_profile->height;
342 p->progressive = my_profile->progressive;
343 p->sample_aspect_den = my_profile->sample_aspect_den;
344 p->sample_aspect_num = my_profile->sample_aspect_num;
345 p->colorspace = my_profile->colorspace;
347 mlt_profile_close( my_profile );
350 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
352 if ( p->description )
353 free( p->description );
354 p->description = strdup( _s(atts[ 1 ]) );
357 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
358 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
359 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
360 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
361 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
362 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
363 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
364 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
365 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
366 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
367 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
368 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
369 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
370 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
371 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
372 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
373 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
374 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
375 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
376 p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
380 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
382 mlt_tractor tractor = mlt_tractor_new( );
383 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
384 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
386 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
387 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );
389 for ( ; atts != NULL && *atts != NULL; atts += 2 )
390 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
392 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
394 if ( mlt_properties_get( properties, "id" ) != NULL )
395 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
397 context_push_service( context, service, mlt_tractor_type );
400 static void on_end_tractor( deserialise_context context, const xmlChar *name )
403 enum service_type type;
404 mlt_service tractor = context_pop_service( context, &type );
406 if ( tractor != NULL && type == mlt_tractor_type )
408 // See if the tractor should be added to a playlist or multitrack
409 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
410 context_push_service( context, tractor, type );
414 mlt_log_error( NULL, "[producer_xml] Invalid state for tractor\n" );
418 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
420 enum service_type type;
421 mlt_service parent = context_pop_service( context, &type );
423 // If we don't have a parent, then create one now, providing we're in a state where we can
424 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
426 mlt_tractor tractor = NULL;
427 // Push the parent back
428 if ( parent != NULL )
429 context_push_service( context, parent, type );
431 // Create a tractor to contain the multitrack
432 tractor = mlt_tractor_new( );
433 parent = MLT_TRACTOR_SERVICE( tractor );
434 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
435 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
436 type = mlt_tractor_type;
438 // Flag it as a synthesised tractor for clean up later
439 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
442 if ( type == mlt_tractor_type )
444 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
445 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
446 for ( ; atts != NULL && *atts != NULL; atts += 2 )
447 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
449 if ( mlt_properties_get( properties, "id" ) != NULL )
450 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
452 context_push_service( context, parent, type );
453 context_push_service( context, service, mlt_multitrack_type );
457 mlt_log_error( NULL, "[producer_xml] Invalid multitrack position\n" );
461 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
463 // Get the multitrack from the stack
464 enum service_type type;
465 mlt_service service = context_pop_service( context, &type );
467 if ( service == NULL || type != mlt_multitrack_type )
468 mlt_log_error( NULL, "[producer_xml] End multitrack in the wrong state...\n" );
471 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
473 mlt_playlist playlist = mlt_playlist_new( context->profile );
474 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
475 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
477 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
479 for ( ; atts != NULL && *atts != NULL; atts += 2 )
481 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
483 // Out will be overwritten later as we append, so we need to save it
484 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
485 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
488 if ( mlt_properties_get( properties, "id" ) != NULL )
489 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
491 context_push_service( context, service, mlt_playlist_type );
494 static void on_end_playlist( deserialise_context context, const xmlChar *name )
496 // Get the playlist from the stack
497 enum service_type type;
498 mlt_service service = context_pop_service( context, &type );
500 if ( service != NULL && type == mlt_playlist_type )
502 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
503 mlt_position in = -1;
504 mlt_position out = -1;
506 if ( mlt_properties_get( properties, "in" ) )
507 in = mlt_properties_get_position( properties, "in" );
508 if ( mlt_properties_get( properties, "out" ) )
509 out = mlt_properties_get_position( properties, "out" );
511 // See if the playlist should be added to a playlist or multitrack
512 if ( add_producer( context, service, in, out ) == 0 )
513 context_push_service( context, service, type );
517 mlt_log_error( NULL, "[producer_xml] Invalid state of playlist end %d\n", type );
521 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
523 // use a dummy service to hold properties to allow arbitrary nesting
524 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
525 mlt_service_init( service, NULL );
527 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
529 context_push_service( context, service, mlt_dummy_producer_type );
531 for ( ; atts != NULL && *atts != NULL; atts += 2 )
532 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
535 static void on_end_producer( deserialise_context context, const xmlChar *name )
537 enum service_type type;
538 mlt_service service = context_pop_service( context, &type );
539 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
541 if ( service != NULL && type == mlt_dummy_producer_type )
543 mlt_service producer = NULL;
545 qualify_property( context, properties, "resource" );
546 char *resource = mlt_properties_get( properties, "resource" );
548 // Let Kino-SMIL src be a synonym for resource
549 if ( resource == NULL )
551 qualify_property( context, properties, "src" );
552 resource = mlt_properties_get( properties, "src" );
555 // Instantiate the producer
556 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
558 char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
561 char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
562 strcat( temp, service_name );
564 strcat( temp, resource );
565 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
570 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
574 // Just in case the plugin requested doesn't exist...
575 if ( !producer && resource )
576 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
578 mlt_log_error( NULL, "[producer_xml] failed to load producer \"%s\"\n", resource );
580 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
582 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
585 mlt_service_close( service );
590 // Track this producer
591 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
592 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );
593 if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( producer ), "seekable" ) )
594 context->seekable &= mlt_properties_get_int( MLT_SERVICE_PROPERTIES( producer ), "seekable" );
596 // Propagate the properties
597 qualify_property( context, properties, "resource" );
598 qualify_property( context, properties, "luma" );
599 qualify_property( context, properties, "luma.resource" );
600 qualify_property( context, properties, "composite.luma" );
601 qualify_property( context, properties, "producer.resource" );
603 // Handle in/out properties separately
604 mlt_position in = -1;
605 mlt_position out = -1;
608 if ( mlt_properties_get( properties, "in" ) )
609 in = mlt_properties_get_position( properties, "in" );
610 // Let Kino-SMIL clipBegin be a synonym for in
611 else if ( mlt_properties_get( properties, "clipBegin" ) )
612 in = mlt_properties_get_position( properties, "clipBegin" );
614 if ( mlt_properties_get( properties, "out" ) )
615 out = mlt_properties_get_position( properties, "out" );
616 // Let Kino-SMIL clipEnd be a synonym for out
617 else if ( mlt_properties_get( properties, "clipEnd" ) )
618 out = mlt_properties_get_position( properties, "clipEnd" );
620 mlt_properties_set( properties, "in", NULL );
621 mlt_properties_set( properties, "out", NULL );
623 // Inherit the properties
624 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
626 // Attach all filters from service onto producer
627 attach_filters( producer, service );
629 // Add the producer to the producer map
630 if ( mlt_properties_get( properties, "id" ) != NULL )
631 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
633 // See if the producer should be added to a playlist or multitrack
634 if ( add_producer( context, producer, in, out ) == 0 )
636 // Otherwise, set in and out on...
637 if ( in != -1 || out != -1 )
639 // Get the parent service
640 enum service_type type;
641 mlt_service parent = context_pop_service( context, &type );
642 if ( parent != NULL )
644 // Get the parent properties
645 properties = MLT_SERVICE_PROPERTIES( parent );
647 char *resource = mlt_properties_get( properties, "resource" );
649 // Put the parent producer back
650 context_push_service( context, parent, type );
652 // If the parent is a track or entry
653 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
655 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
656 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
660 // Otherwise, set in and out on producer directly
661 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
666 // Otherwise, set in and out on producer directly
667 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
671 // Push the producer onto the stack
672 context_push_service( context, producer, mlt_producer_type );
678 mlt_service_close( service );
683 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
685 // Get the playlist from the stack
686 enum service_type type;
687 mlt_service service = context_pop_service( context, &type );
689 if ( type == mlt_playlist_type && service != NULL )
691 // Look for the length attribute
692 for ( ; atts != NULL && *atts != NULL; atts += 2 )
694 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
696 // Append a blank to the playlist
697 mlt_playlist_blank_time( MLT_PLAYLIST( service ), _s(atts[1]) );
702 // Push the playlist back onto the stack
703 context_push_service( context, service, type );
707 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
711 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
713 mlt_producer entry = NULL;
714 mlt_properties temp = mlt_properties_new( );
715 mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
716 mlt_properties_set_lcnumeric( temp, context->lc_numeric );
718 for ( ; atts != NULL && *atts != NULL; atts += 2 )
720 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
722 // Look for the producer attribute
723 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
725 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
726 if ( producer != NULL )
727 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
731 // If we have a valid entry
732 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
734 mlt_playlist_clip_info info;
735 enum service_type parent_type = mlt_invalid_type;
736 mlt_service parent = context_pop_service( context, &parent_type );
737 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
739 if ( parent_type == mlt_playlist_type )
741 // Append the producer to the playlist
742 mlt_position in = -1;
743 mlt_position out = -1;
744 if ( mlt_properties_get( temp, "in" ) )
745 in = mlt_properties_get_position( temp, "in" );
746 if ( mlt_properties_get( temp, "out" ) )
747 out = mlt_properties_get_position( temp, "out" );
748 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
750 // Handle the repeat property
751 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
753 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
754 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
755 mlt_properties_get_int( temp, "repeat" ) );
758 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
763 mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
766 context_push_service( context, parent, parent_type );
769 // Push the cut onto the stack
770 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
772 mlt_properties_close( temp );
775 static void on_end_entry( deserialise_context context, const xmlChar *name )
777 // Get the entry from the stack
778 enum service_type entry_type = mlt_invalid_type;
779 mlt_service entry = context_pop_service( context, &entry_type );
781 if ( entry == NULL && entry_type != mlt_entry_type )
783 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
787 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
789 // use a dummy service to hold properties to allow arbitrary nesting
790 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
791 mlt_service_init( service, NULL );
793 // Push the dummy service onto the stack
794 context_push_service( context, service, mlt_entry_type );
796 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
798 for ( ; atts != NULL && *atts != NULL; atts += 2 )
800 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
802 // Look for the producer attribute
803 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
805 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
806 if ( producer != NULL )
807 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
812 static void on_end_track( deserialise_context context, const xmlChar *name )
814 // Get the track from the stack
815 enum service_type track_type;
816 mlt_service track = context_pop_service( context, &track_type );
818 if ( track != NULL && track_type == mlt_entry_type )
820 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
821 enum service_type parent_type = mlt_invalid_type;
822 mlt_service parent = context_pop_service( context, &parent_type );
823 mlt_multitrack multitrack = NULL;
825 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
826 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
828 if ( parent_type == mlt_tractor_type )
829 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
830 else if ( parent_type == mlt_multitrack_type )
831 multitrack = MLT_MULTITRACK( parent );
833 mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
835 if ( multitrack != NULL )
837 // Set producer i/o if specified
838 if ( mlt_properties_get( track_props, "in" ) != NULL ||
839 mlt_properties_get( track_props, "out" ) != NULL )
841 mlt_position in = -1;
842 mlt_position out = -1;
843 if ( mlt_properties_get( track_props, "in" ) )
844 in = mlt_properties_get_position( track_props, "in" );
845 if ( mlt_properties_get( track_props, "out" ) )
846 out = mlt_properties_get_position( track_props, "out" );
847 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
848 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
849 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
850 track_props = MLT_PRODUCER_PROPERTIES( cut );
851 mlt_producer_close( cut );
855 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
858 // Set the hide state of the track producer
859 char *hide_s = mlt_properties_get( track_props, "hide" );
860 if ( hide_s != NULL )
862 if ( strcmp( hide_s, "video" ) == 0 )
863 mlt_properties_set_int( producer_props, "hide", 1 );
864 else if ( strcmp( hide_s, "audio" ) == 0 )
865 mlt_properties_set_int( producer_props, "hide", 2 );
866 else if ( strcmp( hide_s, "both" ) == 0 )
867 mlt_properties_set_int( producer_props, "hide", 3 );
871 if ( parent != NULL )
872 context_push_service( context, parent, parent_type );
876 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
881 mlt_service_close( track );
886 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
888 // use a dummy service to hold properties to allow arbitrary nesting
889 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
890 mlt_service_init( service, NULL );
892 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
894 context_push_service( context, service, mlt_dummy_filter_type );
896 // Set the properties
897 for ( ; atts != NULL && *atts != NULL; atts += 2 )
898 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
901 static void on_end_filter( deserialise_context context, const xmlChar *name )
903 enum service_type type;
904 mlt_service service = context_pop_service( context, &type );
905 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
907 enum service_type parent_type = mlt_invalid_type;
908 mlt_service parent = context_pop_service( context, &parent_type );
910 if ( service != NULL && type == mlt_dummy_filter_type )
912 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
913 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
914 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
918 mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
920 context_push_service( context, parent, parent_type );
921 mlt_service_close( service );
926 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
927 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
929 // Propogate the properties
930 qualify_property( context, properties, "resource" );
931 qualify_property( context, properties, "luma" );
932 qualify_property( context, properties, "luma.resource" );
933 qualify_property( context, properties, "composite.luma" );
934 qualify_property( context, properties, "producer.resource" );
935 mlt_properties_inherit( filter_props, properties );
937 // Attach all filters from service onto filter
938 attach_filters( filter, service );
940 // Associate the filter with the parent
941 if ( parent != NULL )
943 if ( parent_type == mlt_tractor_type )
945 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
946 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
947 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
948 mlt_properties_get_int( properties, "in" ),
949 mlt_properties_get_int( properties, "out" ) );
953 mlt_service_attach( parent, MLT_FILTER( filter ) );
956 // Put the parent back on the stack
957 context_push_service( context, parent, parent_type );
961 mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
966 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
971 mlt_service_close( service );
976 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
978 // use a dummy service to hold properties to allow arbitrary nesting
979 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
980 mlt_service_init( service, NULL );
982 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
984 context_push_service( context, service, mlt_dummy_transition_type );
986 // Set the properties
987 for ( ; atts != NULL && *atts != NULL; atts += 2 )
988 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
991 static void on_end_transition( deserialise_context context, const xmlChar *name )
993 enum service_type type;
994 mlt_service service = context_pop_service( context, &type );
995 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
997 enum service_type parent_type = mlt_invalid_type;
998 mlt_service parent = context_pop_service( context, &parent_type );
1000 if ( service != NULL && type == mlt_dummy_transition_type )
1002 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1003 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
1004 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
1008 mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
1010 context_push_service( context, parent, parent_type );
1011 mlt_service_close( service );
1015 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1016 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1018 // Propogate the properties
1019 qualify_property( context, properties, "resource" );
1020 qualify_property( context, properties, "luma" );
1021 qualify_property( context, properties, "luma.resource" );
1022 qualify_property( context, properties, "composite.luma" );
1023 qualify_property( context, properties, "producer.resource" );
1024 mlt_properties_inherit( effect_props, properties );
1026 // Attach all filters from service onto effect
1027 attach_filters( effect, service );
1029 // Associate the filter with the parent
1030 if ( parent != NULL )
1032 if ( parent_type == mlt_tractor_type )
1034 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1035 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1036 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1037 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1038 mlt_properties_get_int( properties, "a_track" ),
1039 mlt_properties_get_int( properties, "b_track" ) );
1040 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1041 mlt_properties_get_int( properties, "in" ),
1042 mlt_properties_get_int( properties, "out" ) );
1046 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1049 // Put the parent back on the stack
1050 context_push_service( context, parent, parent_type );
1054 mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1060 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1065 mlt_service_close( service );
1070 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1072 if ( context->pass == 1 )
1074 mlt_properties properties = mlt_properties_new();
1076 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1077 context_push_service( context, (mlt_service) properties, mlt_dummy_consumer_type );
1079 // Set the properties from attributes
1080 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1081 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1085 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1087 if ( context->pass == 1 )
1089 // Get the consumer from the stack
1090 enum service_type type;
1091 mlt_properties properties = (mlt_properties) context_pop_service( context, &type );
1093 if ( properties && type == mlt_dummy_consumer_type )
1095 qualify_property( context, properties, "resource" );
1096 qualify_property( context, properties, "target" );
1097 char *resource = mlt_properties_get( properties, "resource" );
1099 if ( context->multi_consumer > 1 || context->qglsl )
1101 // Instantiate the multi consumer
1102 if ( !context->consumer )
1104 if ( context->qglsl )
1105 context->consumer = context->qglsl;
1107 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1108 if ( context->consumer )
1110 // Track this consumer
1111 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1112 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1115 if ( context->consumer )
1117 // Set this properties object on multi consumer
1119 snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1120 mlt_properties_inc_ref( properties );
1121 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, properties, 0,
1122 (mlt_destructor) mlt_properties_close, NULL );
1127 // Instantiate the consumer
1128 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1129 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1130 if ( context->consumer )
1132 // Track this consumer
1133 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1134 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1136 // Inherit the properties
1137 mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1143 mlt_properties_close( properties );
1147 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1149 enum service_type type;
1150 mlt_service service = context_pop_service( context, &type );
1151 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1152 const char *value = NULL;
1154 if ( service != NULL )
1156 // Set the properties
1157 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1159 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1160 context->property = strdup( _s(atts[ 1 ]) );
1161 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1162 value = _s(atts[ 1 ]);
1165 if ( context->property != NULL )
1166 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1168 // Tell parser to collect any further nodes for serialisation
1169 context->is_value = 1;
1171 context_push_service( context, service, type );
1175 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1179 static void on_end_property( deserialise_context context, const xmlChar *name )
1181 enum service_type type;
1182 mlt_service service = context_pop_service( context, &type );
1183 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1185 if ( service != NULL )
1187 // Tell parser to stop building a tree
1188 context->is_value = 0;
1190 // See if there is a xml tree for the value
1191 if ( context->property != NULL && context->value_doc != NULL )
1196 // Serialise the tree to get value
1197 xmlDocDumpMemory( context->value_doc, &value, &size );
1198 mlt_properties_set( properties, context->property, _s(value) );
1200 xmlFreeFunc xmlFree = NULL;
1201 xmlMemGet( &xmlFree, NULL, NULL, NULL);
1204 xmlFreeDoc( context->value_doc );
1205 context->value_doc = NULL;
1208 // Close this property handling
1209 free( context->property );
1210 context->property = NULL;
1212 context_push_service( context, service, type );
1216 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1220 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1222 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1223 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1225 if ( context->pass == 0 )
1227 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1228 xmlStrcmp( name, _x("profile") ) == 0 ||
1229 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1230 on_start_profile( context, name, atts );
1231 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1232 context->multi_consumer++;
1234 // Check for a service beginning with glsl. or movit.
1235 for ( ; atts != NULL && *atts != NULL; atts += 2 ) {
1236 if ( !xmlStrncmp( atts[1], _x("glsl."), 5 ) || !xmlStrncmp( atts[1], _x("movit."), 6 ) ) {
1237 mlt_properties_set_int( context->params, "qglsl", 1 );
1243 context->branch[ context->depth ] ++;
1246 // Build a tree from nodes within a property value
1247 if ( context->is_value == 1 && context->pass == 1 )
1249 xmlNodePtr node = xmlNewNode( NULL, name );
1251 if ( context->value_doc == NULL )
1254 context->value_doc = xmlNewDoc( _x("1.0") );
1255 xmlDocSetRootElement( context->value_doc, node );
1259 // Append child to tree
1260 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1262 context_push_node( context, node );
1264 // Set the attributes
1265 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1266 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1268 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1269 on_start_tractor( context, name, atts );
1270 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1271 on_start_multitrack( context, name, atts );
1272 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1273 on_start_playlist( context, name, atts );
1274 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1275 on_start_producer( context, name, atts );
1276 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1277 on_start_blank( context, name, atts );
1278 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1279 on_start_entry( context, name, atts );
1280 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1281 on_start_track( context, name, atts );
1282 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1283 on_start_filter( context, name, atts );
1284 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1285 on_start_transition( context, name, atts );
1286 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1287 on_start_property( context, name, atts );
1288 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1289 on_start_consumer( context, name, atts );
1290 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1292 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1294 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1295 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1296 else if ( !context->lc_numeric )
1297 context->lc_numeric = strdup( _s( atts[1] ) );
1302 static void on_end_element( void *ctx, const xmlChar *name )
1304 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1305 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1307 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1308 context_pop_node( context );
1309 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1310 on_end_multitrack( context, name );
1311 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1312 on_end_playlist( context, name );
1313 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1314 on_end_track( context, name );
1315 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1316 on_end_entry( context, name );
1317 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1318 on_end_tractor( context, name );
1319 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1320 on_end_property( context, name );
1321 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1322 on_end_producer( context, name );
1323 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1324 on_end_filter( context, name );
1325 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1326 on_end_transition( context, name );
1327 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1328 on_end_consumer( context, name );
1330 context->branch[ context->depth ] = 0;
1334 static void on_characters( void *ctx, const xmlChar *ch, int len )
1336 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1337 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1338 char *value = calloc( 1, len + 1 );
1339 enum service_type type;
1340 mlt_service service = context_pop_service( context, &type );
1341 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1343 if ( service != NULL )
1344 context_push_service( context, service, type );
1347 strncpy( value, (const char*) ch, len );
1349 if ( context->stack_node_size > 0 )
1350 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1352 // libxml2 generates an on_characters immediately after a get_entity within
1353 // an element value, and we ignore it because it is called again during
1354 // actual substitution.
1355 else if ( context->property != NULL && context->entity_is_replace == 0 )
1357 char *s = mlt_properties_get( properties, context->property );
1360 // Append new text to existing content
1361 char *new = calloc( 1, strlen( s ) + len + 1 );
1363 strcat( new, value );
1364 mlt_properties_set( properties, context->property, new );
1368 mlt_properties_set( properties, context->property, value );
1370 context->entity_is_replace = 0;
1372 // Check for a service beginning with glsl. or movit.
1373 if ( !strncmp( value, "glsl.", 5 ) || !strncmp( value, "movit.", 6 ) )
1374 mlt_properties_set_int( context->params, "qglsl", 1 );
1379 /** Convert parameters parsed from resource into entity declarations.
1381 static void params_to_entities( deserialise_context context )
1383 if ( context->params != NULL )
1387 // Add our params as entitiy declarations
1388 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1390 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1391 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1392 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1396 mlt_properties_close( context->params );
1397 context->params = NULL;
1401 // The following 3 facilitate entity substitution in the SAX parser
1402 static void on_internal_subset( void *ctx, const xmlChar* name,
1403 const xmlChar* publicId, const xmlChar* systemId )
1405 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1406 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1408 context->publicId = publicId;
1409 context->systemId = systemId;
1410 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1412 // Override default entities with our parameters
1413 params_to_entities( context );
1416 // TODO: Check this with Dan... I think this is for parameterisation
1417 // but it's breaking standard escaped entities (like < etc).
1418 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1419 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1421 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1422 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1424 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1427 // TODO: Check this functionality (see on_entity_declaration)
1428 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1430 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1431 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1432 xmlEntityPtr e = NULL;
1434 // Setup for entity declarations if not ready
1435 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1437 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1438 context->publicId = _x("");
1439 context->systemId = _x("");
1442 // Add our parameters if not already
1443 params_to_entities( context );
1445 e = xmlGetPredefinedEntity( name );
1447 // Send signal to on_characters that an entity substitutin is pending
1450 e = xmlGetDocEntity( context->entity_doc, name );
1452 context->entity_is_replace = 1;
1458 static void on_error( void * ctx, const char * msg, ... )
1460 struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1462 switch( err_ptr->level )
1464 case XML_ERR_WARNING:
1465 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1466 err_ptr->message, err_ptr->line, err_ptr->int2 );
1469 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1470 err_ptr->message, err_ptr->line, err_ptr->int2 );
1474 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1475 err_ptr->message, err_ptr->line, err_ptr->int2 );
1480 /** Convert a hexadecimal character to its value.
1482 static int tohex( char p )
1484 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1487 /** Decode a url-encoded string containing hexadecimal character sequences.
1489 static char *url_decode( char *dest, char *src )
1497 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1510 /** Extract the filename from a URL attaching parameters to a properties list.
1512 static void parse_url( mlt_properties properties, char *url )
1515 int n = strlen( url );
1519 for ( i = 0; i < n; i++ )
1531 if ( url[i] == ':' && url[i + 1] != '/' )
1543 if ( name != NULL && value != NULL )
1544 mlt_properties_set( properties, name, value );
1550 if ( name != NULL && value != NULL )
1551 mlt_properties_set( properties, name, value );
1554 // Quick workaround to avoid unecessary libxml2 warnings
1555 static int file_exists( char *file )
1557 char *name = strdup( file );
1559 if ( name != NULL && strchr( name, '?' ) )
1560 *( strchr( name, '?' ) ) = '\0';
1563 FILE *f = fopen( name, "r" );
1565 if ( exists ) fclose( f );
1571 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1573 xmlSAXHandler *sax, *sax_orig;
1574 struct deserialise_context_s *context;
1575 mlt_properties properties = NULL;
1577 struct _xmlParserCtxt *xmlcontext;
1578 int well_formed = 0;
1579 char *filename = NULL;
1580 int is_filename = strcmp( id, "xml-string" );
1582 // Strip file:// prefix
1583 if ( data && strlen( data ) >= 7 && strncmp( data, "file://", 7 ) == 0 )
1586 if ( data == NULL || !strcmp( data, "" ) || ( is_filename && !file_exists( data ) ) )
1589 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1590 if ( context == NULL )
1593 context->producer_map = mlt_properties_new();
1594 context->destructors = mlt_properties_new();
1595 context->params = mlt_properties_new();
1596 context->profile = profile;
1597 context->seekable = 1;
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 = calloc( 1, sizeof( xmlSAXHandler ) );
1632 sax->startElement = on_start_element;
1633 sax->characters = on_characters;
1634 sax->warning = on_error;
1635 sax->error = on_error;
1636 sax->fatalError = on_error;
1638 // Setup libxml2 SAX parsing
1640 xmlSubstituteEntitiesDefault( 1 );
1641 // This is used to facilitate entity substitution in the SAX parser
1642 context->entity_doc = xmlNewDoc( _x("1.0") );
1644 xmlcontext = xmlCreateFileParserCtxt( filename );
1646 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1648 // Invalid context - clean up and return NULL
1649 if ( xmlcontext == NULL )
1651 mlt_properties_close( context->producer_map );
1652 mlt_properties_close( context->destructors );
1653 mlt_properties_close( context->params );
1661 sax_orig = xmlcontext->sax;
1662 xmlcontext->sax = sax;
1663 xmlcontext->_private = ( void* )context;
1664 xmlParseDocument( xmlcontext );
1665 well_formed = xmlcontext->wellFormed;
1667 // Cleanup after parsing
1668 xmlcontext->sax = sax_orig;
1669 xmlcontext->_private = NULL;
1670 if ( xmlcontext->myDoc )
1671 xmlFreeDoc( xmlcontext->myDoc );
1672 xmlFreeParserCtxt( xmlcontext );
1673 context->stack_node_size = 0;
1674 context->stack_service_size = 0;
1676 // Bad xml - clean up and return NULL
1679 mlt_properties_close( context->producer_map );
1680 mlt_properties_close( context->destructors );
1681 mlt_properties_close( context->params );
1682 xmlFreeDoc( context->entity_doc );
1689 // Setup the second pass
1692 xmlcontext = xmlCreateFileParserCtxt( filename );
1694 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1696 // Invalid context - clean up and return NULL
1697 if ( xmlcontext == NULL )
1699 mlt_properties_close( context->producer_map );
1700 mlt_properties_close( context->destructors );
1701 mlt_properties_close( context->params );
1702 xmlFreeDoc( context->entity_doc );
1709 // Create the qglsl consumer now, if requested, so that glsl.manager
1710 // may exist when trying to load glsl. or movit. services.
1711 // The "if requested" part can come from query string qglsl=1 or when
1712 // a service beginning with glsl. or movit. appears in the XML.
1713 if ( mlt_properties_get_int( context->params, "qglsl" ) && strcmp( id, "xml-nogl" ) )
1714 context->qglsl = mlt_factory_consumer( profile, "qglsl", NULL );
1716 // Setup SAX callbacks for second pass
1717 sax->endElement = on_end_element;
1718 sax->cdataBlock = on_characters;
1719 sax->internalSubset = on_internal_subset;
1720 sax->entityDecl = on_entity_declaration;
1721 sax->getEntity = on_get_entity;
1724 sax_orig = xmlcontext->sax;
1725 xmlcontext->sax = sax;
1726 xmlcontext->_private = ( void* )context;
1727 xmlParseDocument( xmlcontext );
1728 well_formed = xmlcontext->wellFormed;
1730 // Cleanup after parsing
1731 xmlFreeDoc( context->entity_doc );
1733 xmlMemoryDump( ); // for debugging
1734 xmlcontext->sax = sax_orig;
1735 xmlcontext->_private = NULL;
1736 if ( xmlcontext->myDoc )
1737 xmlFreeDoc( xmlcontext->myDoc );
1738 xmlFreeParserCtxt( xmlcontext );
1740 // Get the last producer on the stack
1741 enum service_type type;
1742 mlt_service service = context_pop_service( context, &type );
1743 if ( well_formed && service != NULL )
1745 // Verify it is a producer service (mlt_type="mlt_producer")
1746 // (producer, playlist, multitrack)
1747 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1748 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1753 xmlDocPtr doc = xml_make_doc( service );
1754 xmlDocFormatDump( stdout, doc, 1 );
1759 if ( well_formed && service != NULL )
1761 char *title = mlt_properties_get( context->producer_map, "title" );
1763 // Need the complete producer list for various reasons
1764 properties = context->destructors;
1766 // Now make sure we don't have a reference to the service in the properties
1767 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1769 char *name = mlt_properties_get_name( properties, i );
1770 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1772 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1777 // We are done referencing destructor property list
1778 // Set this var to service properties for convenience
1779 properties = MLT_SERVICE_PROPERTIES( service );
1782 mlt_properties_set( properties, "title", title );
1784 // Optimise for overlapping producers
1785 mlt_producer_optimise( MLT_PRODUCER( service ) );
1787 // Handle deep copies
1788 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1790 // Now assign additional properties
1791 if ( is_filename && (
1792 mlt_service_identify( service ) == tractor_type ||
1793 mlt_service_identify( service ) == playlist_type ||
1794 mlt_service_identify( service ) == multitrack_type ) )
1796 mlt_properties_set_int( properties, "_original_type",
1797 mlt_service_identify( service ) );
1798 mlt_properties_set( properties, "_original_resource",
1799 mlt_properties_get( properties, "resource" ) );
1800 mlt_properties_set( properties, "resource", data );
1803 // This tells consumer_xml not to deep copy
1804 mlt_properties_set( properties, "xml", "was here" );
1808 // Allow the project to be edited
1809 mlt_properties_set( properties, "_xml", "was here" );
1810 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1813 // Make consumer available
1814 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1815 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1816 (mlt_destructor) mlt_consumer_close, NULL );
1818 mlt_properties_set_int( properties, "seekable", context->seekable );
1822 // Return null if not well formed
1827 if ( context->qglsl && context->consumer != context->qglsl )
1828 mlt_consumer_close( context->qglsl );
1829 mlt_properties_close( context->producer_map );
1830 if ( context->params != NULL )
1831 mlt_properties_close( context->params );
1832 mlt_properties_close( context->destructors );
1833 if ( context->lc_numeric )
1834 free( context->lc_numeric );
1838 return MLT_PRODUCER( service );