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 context->seekable &= mlt_properties_get_int( MLT_SERVICE_PROPERTIES( producer ), "seekable" );
595 // Propagate the properties
596 qualify_property( context, properties, "resource" );
597 qualify_property( context, properties, "luma" );
598 qualify_property( context, properties, "luma.resource" );
599 qualify_property( context, properties, "composite.luma" );
600 qualify_property( context, properties, "producer.resource" );
602 // Handle in/out properties separately
603 mlt_position in = -1;
604 mlt_position out = -1;
607 if ( mlt_properties_get( properties, "in" ) )
608 in = mlt_properties_get_position( properties, "in" );
609 // Let Kino-SMIL clipBegin be a synonym for in
610 else if ( mlt_properties_get( properties, "clipBegin" ) )
611 in = mlt_properties_get_position( properties, "clipBegin" );
613 if ( mlt_properties_get( properties, "out" ) )
614 out = mlt_properties_get_position( properties, "out" );
615 // Let Kino-SMIL clipEnd be a synonym for out
616 else if ( mlt_properties_get( properties, "clipEnd" ) )
617 out = mlt_properties_get_position( properties, "clipEnd" );
619 mlt_properties_set( properties, "in", NULL );
620 mlt_properties_set( properties, "out", NULL );
622 // Inherit the properties
623 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
625 // Attach all filters from service onto producer
626 attach_filters( producer, service );
628 // Add the producer to the producer map
629 if ( mlt_properties_get( properties, "id" ) != NULL )
630 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
632 // See if the producer should be added to a playlist or multitrack
633 if ( add_producer( context, producer, in, out ) == 0 )
635 // Otherwise, set in and out on...
636 if ( in != -1 || out != -1 )
638 // Get the parent service
639 enum service_type type;
640 mlt_service parent = context_pop_service( context, &type );
641 if ( parent != NULL )
643 // Get the parent properties
644 properties = MLT_SERVICE_PROPERTIES( parent );
646 char *resource = mlt_properties_get( properties, "resource" );
648 // Put the parent producer back
649 context_push_service( context, parent, type );
651 // If the parent is a track or entry
652 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
654 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
655 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
659 // Otherwise, set in and out on producer directly
660 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
665 // Otherwise, set in and out on producer directly
666 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
670 // Push the producer onto the stack
671 context_push_service( context, producer, mlt_producer_type );
677 mlt_service_close( service );
682 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
684 // Get the playlist from the stack
685 enum service_type type;
686 mlt_service service = context_pop_service( context, &type );
688 if ( type == mlt_playlist_type && service != NULL )
690 // Look for the length attribute
691 for ( ; atts != NULL && *atts != NULL; atts += 2 )
693 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
695 // Append a blank to the playlist
696 mlt_playlist_blank_time( MLT_PLAYLIST( service ), _s(atts[1]) );
701 // Push the playlist back onto the stack
702 context_push_service( context, service, type );
706 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
710 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
712 mlt_producer entry = NULL;
713 mlt_properties temp = mlt_properties_new( );
714 mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
715 mlt_properties_set_lcnumeric( temp, context->lc_numeric );
717 for ( ; atts != NULL && *atts != NULL; atts += 2 )
719 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
721 // Look for the producer attribute
722 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
724 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
725 if ( producer != NULL )
726 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
730 // If we have a valid entry
731 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
733 mlt_playlist_clip_info info;
734 enum service_type parent_type = mlt_invalid_type;
735 mlt_service parent = context_pop_service( context, &parent_type );
736 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
738 if ( parent_type == mlt_playlist_type )
740 // Append the producer to the playlist
741 mlt_position in = -1;
742 mlt_position out = -1;
743 if ( mlt_properties_get( temp, "in" ) )
744 in = mlt_properties_get_position( temp, "in" );
745 if ( mlt_properties_get( temp, "out" ) )
746 out = mlt_properties_get_position( temp, "out" );
747 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
749 // Handle the repeat property
750 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
752 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
753 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
754 mlt_properties_get_int( temp, "repeat" ) );
757 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
762 mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
765 context_push_service( context, parent, parent_type );
768 // Push the cut onto the stack
769 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
771 mlt_properties_close( temp );
774 static void on_end_entry( deserialise_context context, const xmlChar *name )
776 // Get the entry from the stack
777 enum service_type entry_type = mlt_invalid_type;
778 mlt_service entry = context_pop_service( context, &entry_type );
780 if ( entry == NULL && entry_type != mlt_entry_type )
782 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
786 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
788 // use a dummy service to hold properties to allow arbitrary nesting
789 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
790 mlt_service_init( service, NULL );
792 // Push the dummy service onto the stack
793 context_push_service( context, service, mlt_entry_type );
795 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
797 for ( ; atts != NULL && *atts != NULL; atts += 2 )
799 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
801 // Look for the producer attribute
802 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
804 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
805 if ( producer != NULL )
806 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
811 static void on_end_track( deserialise_context context, const xmlChar *name )
813 // Get the track from the stack
814 enum service_type track_type;
815 mlt_service track = context_pop_service( context, &track_type );
817 if ( track != NULL && track_type == mlt_entry_type )
819 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
820 enum service_type parent_type = mlt_invalid_type;
821 mlt_service parent = context_pop_service( context, &parent_type );
822 mlt_multitrack multitrack = NULL;
824 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
825 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
827 if ( parent_type == mlt_tractor_type )
828 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
829 else if ( parent_type == mlt_multitrack_type )
830 multitrack = MLT_MULTITRACK( parent );
832 mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
834 if ( multitrack != NULL )
836 // Set producer i/o if specified
837 if ( mlt_properties_get( track_props, "in" ) != NULL ||
838 mlt_properties_get( track_props, "out" ) != NULL )
840 mlt_position in = -1;
841 mlt_position out = -1;
842 if ( mlt_properties_get( track_props, "in" ) )
843 in = mlt_properties_get_position( track_props, "in" );
844 if ( mlt_properties_get( track_props, "out" ) )
845 out = mlt_properties_get_position( track_props, "out" );
846 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
847 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
848 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
849 track_props = MLT_PRODUCER_PROPERTIES( cut );
850 mlt_producer_close( cut );
854 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
857 // Set the hide state of the track producer
858 char *hide_s = mlt_properties_get( track_props, "hide" );
859 if ( hide_s != NULL )
861 if ( strcmp( hide_s, "video" ) == 0 )
862 mlt_properties_set_int( producer_props, "hide", 1 );
863 else if ( strcmp( hide_s, "audio" ) == 0 )
864 mlt_properties_set_int( producer_props, "hide", 2 );
865 else if ( strcmp( hide_s, "both" ) == 0 )
866 mlt_properties_set_int( producer_props, "hide", 3 );
870 if ( parent != NULL )
871 context_push_service( context, parent, parent_type );
875 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
880 mlt_service_close( track );
885 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
887 // use a dummy service to hold properties to allow arbitrary nesting
888 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
889 mlt_service_init( service, NULL );
891 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
893 context_push_service( context, service, mlt_dummy_filter_type );
895 // Set the properties
896 for ( ; atts != NULL && *atts != NULL; atts += 2 )
897 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
900 static void on_end_filter( deserialise_context context, const xmlChar *name )
902 enum service_type type;
903 mlt_service service = context_pop_service( context, &type );
904 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
906 enum service_type parent_type = mlt_invalid_type;
907 mlt_service parent = context_pop_service( context, &parent_type );
909 if ( service != NULL && type == mlt_dummy_filter_type )
911 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
912 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
913 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
917 mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
919 context_push_service( context, parent, parent_type );
920 mlt_service_close( service );
925 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
926 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
928 // Propogate the properties
929 qualify_property( context, properties, "resource" );
930 qualify_property( context, properties, "luma" );
931 qualify_property( context, properties, "luma.resource" );
932 qualify_property( context, properties, "composite.luma" );
933 qualify_property( context, properties, "producer.resource" );
934 mlt_properties_inherit( filter_props, properties );
936 // Attach all filters from service onto filter
937 attach_filters( filter, service );
939 // Associate the filter with the parent
940 if ( parent != NULL )
942 if ( parent_type == mlt_tractor_type )
944 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
945 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
946 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
947 mlt_properties_get_int( properties, "in" ),
948 mlt_properties_get_int( properties, "out" ) );
952 mlt_service_attach( parent, MLT_FILTER( filter ) );
955 // Put the parent back on the stack
956 context_push_service( context, parent, parent_type );
960 mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
965 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
970 mlt_service_close( service );
975 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
977 // use a dummy service to hold properties to allow arbitrary nesting
978 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
979 mlt_service_init( service, NULL );
981 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
983 context_push_service( context, service, mlt_dummy_transition_type );
985 // Set the properties
986 for ( ; atts != NULL && *atts != NULL; atts += 2 )
987 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
990 static void on_end_transition( deserialise_context context, const xmlChar *name )
992 enum service_type type;
993 mlt_service service = context_pop_service( context, &type );
994 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
996 enum service_type parent_type = mlt_invalid_type;
997 mlt_service parent = context_pop_service( context, &parent_type );
999 if ( service != NULL && type == mlt_dummy_transition_type )
1001 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1002 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
1003 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
1007 mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
1009 context_push_service( context, parent, parent_type );
1010 mlt_service_close( service );
1014 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1015 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1017 // Propogate the properties
1018 qualify_property( context, properties, "resource" );
1019 qualify_property( context, properties, "luma" );
1020 qualify_property( context, properties, "luma.resource" );
1021 qualify_property( context, properties, "composite.luma" );
1022 qualify_property( context, properties, "producer.resource" );
1023 mlt_properties_inherit( effect_props, properties );
1025 // Attach all filters from service onto effect
1026 attach_filters( effect, service );
1028 // Associate the filter with the parent
1029 if ( parent != NULL )
1031 if ( parent_type == mlt_tractor_type )
1033 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1034 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1035 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1036 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1037 mlt_properties_get_int( properties, "a_track" ),
1038 mlt_properties_get_int( properties, "b_track" ) );
1039 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1040 mlt_properties_get_int( properties, "in" ),
1041 mlt_properties_get_int( properties, "out" ) );
1045 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1048 // Put the parent back on the stack
1049 context_push_service( context, parent, parent_type );
1053 mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1059 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1064 mlt_service_close( service );
1069 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1071 if ( context->pass == 1 )
1073 mlt_properties properties = mlt_properties_new();
1075 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1076 context_push_service( context, (mlt_service) properties, mlt_dummy_consumer_type );
1078 // Set the properties from attributes
1079 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1080 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1084 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1086 if ( context->pass == 1 )
1088 // Get the consumer from the stack
1089 enum service_type type;
1090 mlt_properties properties = (mlt_properties) context_pop_service( context, &type );
1092 if ( properties && type == mlt_dummy_consumer_type )
1094 qualify_property( context, properties, "resource" );
1095 qualify_property( context, properties, "target" );
1096 char *resource = mlt_properties_get( properties, "resource" );
1098 if ( context->multi_consumer > 1 || context->qglsl )
1100 // Instantiate the multi consumer
1101 if ( !context->consumer )
1103 if ( context->qglsl )
1104 context->consumer = context->qglsl;
1106 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1107 if ( context->consumer )
1109 // Track this consumer
1110 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1111 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1114 if ( context->consumer )
1116 // Set this properties object on multi consumer
1118 snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1119 mlt_properties_inc_ref( properties );
1120 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, properties, 0,
1121 (mlt_destructor) mlt_properties_close, NULL );
1126 // Instantiate the consumer
1127 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1128 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1129 if ( context->consumer )
1131 // Track this consumer
1132 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1133 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1135 // Inherit the properties
1136 mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1142 mlt_properties_close( properties );
1146 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1148 enum service_type type;
1149 mlt_service service = context_pop_service( context, &type );
1150 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1151 const char *value = NULL;
1153 if ( service != NULL )
1155 // Set the properties
1156 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1158 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1159 context->property = strdup( _s(atts[ 1 ]) );
1160 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1161 value = _s(atts[ 1 ]);
1164 if ( context->property != NULL )
1165 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1167 // Tell parser to collect any further nodes for serialisation
1168 context->is_value = 1;
1170 context_push_service( context, service, type );
1174 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1178 static void on_end_property( deserialise_context context, const xmlChar *name )
1180 enum service_type type;
1181 mlt_service service = context_pop_service( context, &type );
1182 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1184 if ( service != NULL )
1186 // Tell parser to stop building a tree
1187 context->is_value = 0;
1189 // See if there is a xml tree for the value
1190 if ( context->property != NULL && context->value_doc != NULL )
1195 // Serialise the tree to get value
1196 xmlDocDumpMemory( context->value_doc, &value, &size );
1197 mlt_properties_set( properties, context->property, _s(value) );
1199 xmlFreeFunc xmlFree = NULL;
1200 xmlMemGet( &xmlFree, NULL, NULL, NULL);
1203 xmlFreeDoc( context->value_doc );
1204 context->value_doc = NULL;
1207 // Close this property handling
1208 free( context->property );
1209 context->property = NULL;
1211 context_push_service( context, service, type );
1215 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1219 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1221 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1222 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1224 if ( context->pass == 0 )
1226 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1227 xmlStrcmp( name, _x("profile") ) == 0 ||
1228 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1229 on_start_profile( context, name, atts );
1230 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1231 context->multi_consumer++;
1233 // Check for a service beginning with glsl. or movit.
1234 for ( ; atts != NULL && *atts != NULL; atts += 2 ) {
1235 if ( !xmlStrncmp( atts[1], _x("glsl."), 5 ) || !xmlStrncmp( atts[1], _x("movit."), 6 ) ) {
1236 mlt_properties_set_int( context->params, "qglsl", 1 );
1242 context->branch[ context->depth ] ++;
1245 // Build a tree from nodes within a property value
1246 if ( context->is_value == 1 && context->pass == 1 )
1248 xmlNodePtr node = xmlNewNode( NULL, name );
1250 if ( context->value_doc == NULL )
1253 context->value_doc = xmlNewDoc( _x("1.0") );
1254 xmlDocSetRootElement( context->value_doc, node );
1258 // Append child to tree
1259 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1261 context_push_node( context, node );
1263 // Set the attributes
1264 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1265 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1267 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1268 on_start_tractor( context, name, atts );
1269 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1270 on_start_multitrack( context, name, atts );
1271 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1272 on_start_playlist( context, name, atts );
1273 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1274 on_start_producer( context, name, atts );
1275 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1276 on_start_blank( context, name, atts );
1277 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1278 on_start_entry( context, name, atts );
1279 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1280 on_start_track( context, name, atts );
1281 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1282 on_start_filter( context, name, atts );
1283 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1284 on_start_transition( context, name, atts );
1285 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1286 on_start_property( context, name, atts );
1287 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1288 on_start_consumer( context, name, atts );
1289 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1291 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1293 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1294 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1295 else if ( !context->lc_numeric )
1296 context->lc_numeric = strdup( _s( atts[1] ) );
1301 static void on_end_element( void *ctx, const xmlChar *name )
1303 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1304 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1306 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1307 context_pop_node( context );
1308 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1309 on_end_multitrack( context, name );
1310 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1311 on_end_playlist( context, name );
1312 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1313 on_end_track( context, name );
1314 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1315 on_end_entry( context, name );
1316 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1317 on_end_tractor( context, name );
1318 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1319 on_end_property( context, name );
1320 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1321 on_end_producer( context, name );
1322 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1323 on_end_filter( context, name );
1324 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1325 on_end_transition( context, name );
1326 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1327 on_end_consumer( context, name );
1329 context->branch[ context->depth ] = 0;
1333 static void on_characters( void *ctx, const xmlChar *ch, int len )
1335 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1336 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1337 char *value = calloc( 1, len + 1 );
1338 enum service_type type;
1339 mlt_service service = context_pop_service( context, &type );
1340 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1342 if ( service != NULL )
1343 context_push_service( context, service, type );
1346 strncpy( value, (const char*) ch, len );
1348 if ( context->stack_node_size > 0 )
1349 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1351 // libxml2 generates an on_characters immediately after a get_entity within
1352 // an element value, and we ignore it because it is called again during
1353 // actual substitution.
1354 else if ( context->property != NULL && context->entity_is_replace == 0 )
1356 char *s = mlt_properties_get( properties, context->property );
1359 // Append new text to existing content
1360 char *new = calloc( 1, strlen( s ) + len + 1 );
1362 strcat( new, value );
1363 mlt_properties_set( properties, context->property, new );
1367 mlt_properties_set( properties, context->property, value );
1369 context->entity_is_replace = 0;
1371 // Check for a service beginning with glsl. or movit.
1372 if ( !strncmp( value, "glsl.", 5 ) || !strncmp( value, "movit.", 6 ) )
1373 mlt_properties_set_int( context->params, "qglsl", 1 );
1378 /** Convert parameters parsed from resource into entity declarations.
1380 static void params_to_entities( deserialise_context context )
1382 if ( context->params != NULL )
1386 // Add our params as entitiy declarations
1387 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1389 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1390 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1391 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1395 mlt_properties_close( context->params );
1396 context->params = NULL;
1400 // The following 3 facilitate entity substitution in the SAX parser
1401 static void on_internal_subset( void *ctx, const xmlChar* name,
1402 const xmlChar* publicId, const xmlChar* systemId )
1404 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1405 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1407 context->publicId = publicId;
1408 context->systemId = systemId;
1409 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1411 // Override default entities with our parameters
1412 params_to_entities( context );
1415 // TODO: Check this with Dan... I think this is for parameterisation
1416 // but it's breaking standard escaped entities (like < etc).
1417 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1418 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1420 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1421 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1423 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1426 // TODO: Check this functionality (see on_entity_declaration)
1427 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1429 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1430 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1431 xmlEntityPtr e = NULL;
1433 // Setup for entity declarations if not ready
1434 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1436 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1437 context->publicId = _x("");
1438 context->systemId = _x("");
1441 // Add our parameters if not already
1442 params_to_entities( context );
1444 e = xmlGetPredefinedEntity( name );
1446 // Send signal to on_characters that an entity substitutin is pending
1449 e = xmlGetDocEntity( context->entity_doc, name );
1451 context->entity_is_replace = 1;
1457 static void on_error( void * ctx, const char * msg, ... )
1459 struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1461 switch( err_ptr->level )
1463 case XML_ERR_WARNING:
1464 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1465 err_ptr->message, err_ptr->line, err_ptr->int2 );
1468 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1469 err_ptr->message, err_ptr->line, err_ptr->int2 );
1473 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1474 err_ptr->message, err_ptr->line, err_ptr->int2 );
1479 /** Convert a hexadecimal character to its value.
1481 static int tohex( char p )
1483 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1486 /** Decode a url-encoded string containing hexadecimal character sequences.
1488 static char *url_decode( char *dest, char *src )
1496 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1509 /** Extract the filename from a URL attaching parameters to a properties list.
1511 static void parse_url( mlt_properties properties, char *url )
1514 int n = strlen( url );
1518 for ( i = 0; i < n; i++ )
1530 if ( url[i] == ':' && url[i + 1] != '/' )
1542 if ( name != NULL && value != NULL )
1543 mlt_properties_set( properties, name, value );
1549 if ( name != NULL && value != NULL )
1550 mlt_properties_set( properties, name, value );
1553 // Quick workaround to avoid unecessary libxml2 warnings
1554 static int file_exists( char *file )
1556 char *name = strdup( file );
1558 if ( name != NULL && strchr( name, '?' ) )
1559 *( strchr( name, '?' ) ) = '\0';
1562 FILE *f = fopen( name, "r" );
1564 if ( exists ) fclose( f );
1570 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1572 xmlSAXHandler *sax, *sax_orig;
1573 struct deserialise_context_s *context;
1574 mlt_properties properties = NULL;
1576 struct _xmlParserCtxt *xmlcontext;
1577 int well_formed = 0;
1578 char *filename = NULL;
1579 int is_filename = strcmp( id, "xml-string" );
1581 // Strip file:// prefix
1582 if ( data && strlen( data ) >= 7 && strncmp( data, "file://", 7 ) == 0 )
1585 if ( data == NULL || !strcmp( data, "" ) || ( is_filename && !file_exists( data ) ) )
1588 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1589 if ( context == NULL )
1592 context->producer_map = mlt_properties_new();
1593 context->destructors = mlt_properties_new();
1594 context->params = mlt_properties_new();
1595 context->profile = profile;
1596 context->seekable = 1;
1598 // Decode URL and parse parameters
1599 mlt_properties_set( context->producer_map, "root", "" );
1602 filename = strdup( data );
1603 parse_url( context->params, url_decode( filename, data ) );
1605 // We need the directory prefix which was used for the xml
1606 if ( strchr( filename, '/' ) )
1609 mlt_properties_set( context->producer_map, "root", filename );
1610 root = mlt_properties_get( context->producer_map, "root" );
1611 *( strrchr( root, '/' ) ) = '\0';
1613 // If we don't have an absolute path here, we're heading for disaster...
1614 if ( root[ 0 ] != '/' )
1616 char *cwd = getcwd( NULL, 0 );
1617 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1618 sprintf( real, "%s/%s", cwd, root );
1619 mlt_properties_set( context->producer_map, "root", real );
1626 // We need to track the number of registered filters
1627 mlt_properties_set_int( context->destructors, "registered", 0 );
1629 // Setup SAX callbacks for first pass
1630 sax = calloc( 1, sizeof( xmlSAXHandler ) );
1631 sax->startElement = on_start_element;
1632 sax->characters = on_characters;
1633 sax->warning = on_error;
1634 sax->error = on_error;
1635 sax->fatalError = on_error;
1637 // Setup libxml2 SAX parsing
1639 xmlSubstituteEntitiesDefault( 1 );
1640 // This is used to facilitate entity substitution in the SAX parser
1641 context->entity_doc = xmlNewDoc( _x("1.0") );
1643 xmlcontext = xmlCreateFileParserCtxt( filename );
1645 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1647 // Invalid context - clean up and return NULL
1648 if ( xmlcontext == NULL )
1650 mlt_properties_close( context->producer_map );
1651 mlt_properties_close( context->destructors );
1652 mlt_properties_close( context->params );
1660 sax_orig = xmlcontext->sax;
1661 xmlcontext->sax = sax;
1662 xmlcontext->_private = ( void* )context;
1663 xmlParseDocument( xmlcontext );
1664 well_formed = xmlcontext->wellFormed;
1666 // Cleanup after parsing
1667 xmlcontext->sax = sax_orig;
1668 xmlcontext->_private = NULL;
1669 if ( xmlcontext->myDoc )
1670 xmlFreeDoc( xmlcontext->myDoc );
1671 xmlFreeParserCtxt( xmlcontext );
1672 context->stack_node_size = 0;
1673 context->stack_service_size = 0;
1675 // Bad xml - clean up and return NULL
1678 mlt_properties_close( context->producer_map );
1679 mlt_properties_close( context->destructors );
1680 mlt_properties_close( context->params );
1681 xmlFreeDoc( context->entity_doc );
1688 // Setup the second pass
1691 xmlcontext = xmlCreateFileParserCtxt( filename );
1693 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1695 // Invalid context - clean up and return NULL
1696 if ( xmlcontext == NULL )
1698 mlt_properties_close( context->producer_map );
1699 mlt_properties_close( context->destructors );
1700 mlt_properties_close( context->params );
1701 xmlFreeDoc( context->entity_doc );
1708 // Create the qglsl consumer now, if requested, so that glsl.manager
1709 // may exist when trying to load glsl. or movit. services.
1710 // The "if requested" part can come from query string qglsl=1 or when
1711 // a service beginning with glsl. or movit. appears in the XML.
1712 if ( mlt_properties_get_int( context->params, "qglsl" ) )
1713 context->qglsl = mlt_factory_consumer( profile, "qglsl", NULL );
1715 // Setup SAX callbacks for second pass
1716 sax->endElement = on_end_element;
1717 sax->cdataBlock = on_characters;
1718 sax->internalSubset = on_internal_subset;
1719 sax->entityDecl = on_entity_declaration;
1720 sax->getEntity = on_get_entity;
1723 sax_orig = xmlcontext->sax;
1724 xmlcontext->sax = sax;
1725 xmlcontext->_private = ( void* )context;
1726 xmlParseDocument( xmlcontext );
1727 well_formed = xmlcontext->wellFormed;
1729 // Cleanup after parsing
1730 xmlFreeDoc( context->entity_doc );
1732 xmlMemoryDump( ); // for debugging
1733 xmlcontext->sax = sax_orig;
1734 xmlcontext->_private = NULL;
1735 if ( xmlcontext->myDoc )
1736 xmlFreeDoc( xmlcontext->myDoc );
1737 xmlFreeParserCtxt( xmlcontext );
1739 // Get the last producer on the stack
1740 enum service_type type;
1741 mlt_service service = context_pop_service( context, &type );
1742 if ( well_formed && service != NULL )
1744 // Verify it is a producer service (mlt_type="mlt_producer")
1745 // (producer, playlist, multitrack)
1746 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1747 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1752 xmlDocPtr doc = xml_make_doc( service );
1753 xmlDocFormatDump( stdout, doc, 1 );
1758 if ( well_formed && service != NULL )
1760 char *title = mlt_properties_get( context->producer_map, "title" );
1762 // Need the complete producer list for various reasons
1763 properties = context->destructors;
1765 // Now make sure we don't have a reference to the service in the properties
1766 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1768 char *name = mlt_properties_get_name( properties, i );
1769 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1771 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1776 // We are done referencing destructor property list
1777 // Set this var to service properties for convenience
1778 properties = MLT_SERVICE_PROPERTIES( service );
1781 mlt_properties_set( properties, "title", title );
1783 // Optimise for overlapping producers
1784 mlt_producer_optimise( MLT_PRODUCER( service ) );
1786 // Handle deep copies
1787 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1789 // Now assign additional properties
1790 if ( is_filename && (
1791 mlt_service_identify( service ) == tractor_type ||
1792 mlt_service_identify( service ) == playlist_type ||
1793 mlt_service_identify( service ) == multitrack_type ) )
1795 mlt_properties_set_int( properties, "_original_type",
1796 mlt_service_identify( service ) );
1797 mlt_properties_set( properties, "_original_resource",
1798 mlt_properties_get( properties, "resource" ) );
1799 mlt_properties_set( properties, "resource", data );
1802 // This tells consumer_xml not to deep copy
1803 mlt_properties_set( properties, "xml", "was here" );
1807 // Allow the project to be edited
1808 mlt_properties_set( properties, "_xml", "was here" );
1809 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1812 // Make consumer available
1813 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1814 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1815 (mlt_destructor) mlt_consumer_close, NULL );
1817 mlt_properties_set_int( properties, "seekable", context->seekable );
1821 // Return null if not well formed
1826 if ( context->qglsl && context->consumer != context->qglsl )
1827 mlt_consumer_close( context->qglsl );
1828 mlt_properties_close( context->producer_map );
1829 if ( context->params != NULL )
1830 mlt_properties_close( context->params );
1831 mlt_properties_close( context->destructors );
1832 if ( context->lc_numeric )
1833 free( context->lc_numeric );
1837 return MLT_PRODUCER( service );