2 * producer_xml.c -- a libxml2 parser of mlt service networks
3 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 // when the returned producer is closed).
24 #include <framework/mlt.h>
25 #include <framework/mlt_log.h>
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
38 #define _x (const xmlChar*)
39 #define _s (const char*)
43 extern xmlDocPtr xml_make_doc( mlt_service service );
60 mlt_dummy_filter_type,
61 mlt_dummy_transition_type,
62 mlt_dummy_producer_type,
63 mlt_dummy_consumer_type
66 struct deserialise_context_s
68 enum service_type stack_types[ STACK_SIZE ];
69 mlt_service stack_service[ STACK_SIZE ];
70 int stack_service_size;
71 mlt_properties producer_map;
72 mlt_properties destructors;
76 xmlNodePtr stack_node[ STACK_SIZE ];
79 int entity_is_replace;
81 int branch[ STACK_SIZE ];
82 const xmlChar *publicId;
83 const xmlChar *systemId;
84 mlt_properties params;
88 mlt_consumer consumer;
92 typedef struct deserialise_context_s *deserialise_context;
94 /** Trim the leading and trailing whitespace from a string in-place.
96 static char* trim( char *s )
99 if ( s && ( n = strlen( s ) ) )
102 while ( i < n && isspace( s[i] ) ) i++;
103 while ( --n && isspace( s[n] ) );
106 memmove( s, s + i, n );
112 /** Convert the numerical current branch address to a dot-delimited string.
114 static char *serialise_branch( deserialise_context context, char *s )
119 for ( i = 0; i < context->depth; i++ )
121 int len = strlen( s );
122 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
130 static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
132 int ret = context->stack_service_size >= STACK_SIZE - 1;
135 context->stack_service[ context->stack_service_size ] = that;
136 context->stack_types[ context->stack_service_size++ ] = type;
138 // Record the tree branch on which this service lives
139 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
141 char s[ BRANCH_SIG_LEN ];
142 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
151 static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
153 mlt_service result = NULL;
155 *type = invalid_type;
156 if ( context->stack_service_size > 0 )
158 result = context->stack_service[ -- context->stack_service_size ];
160 *type = context->stack_types[ context->stack_service_size ];
161 // Set the service's profile and locale so mlt_property time-to-position conversions can get fps
162 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( result ), "_profile", context->profile, 0, NULL, NULL );
163 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( result ), context->lc_numeric );
171 static int context_push_node( deserialise_context context, xmlNodePtr node )
173 int ret = context->stack_node_size >= STACK_SIZE - 1;
175 context->stack_node[ context->stack_node_size ++ ] = node;
182 static xmlNodePtr context_pop_node( deserialise_context context )
184 xmlNodePtr result = NULL;
185 if ( context->stack_node_size > 0 )
186 result = context->stack_node[ -- context->stack_node_size ];
191 // Set the destructor on a new service
192 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
194 int registered = mlt_properties_get_int( properties, "registered" );
195 char *key = mlt_properties_get( properties, "registered" );
196 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
197 mlt_properties_set_int( properties, "registered", ++ registered );
201 // Prepend the property value with the document root
202 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
204 char *resource = mlt_properties_get( properties, name );
205 if ( resource != NULL && resource[0] )
207 // Qualify file name properties
208 char *root = mlt_properties_get( context->producer_map, "root" );
209 if ( root != NULL && strcmp( root, "" ) )
211 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
212 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
214 strcpy( full_resource, root );
215 strcat( full_resource, "/" );
216 strcat( full_resource, resource );
220 strcpy( full_resource, resource );
222 mlt_properties_set( properties, name, full_resource );
223 free( full_resource );
229 /** This function adds a producer to a playlist or multitrack when
230 there is no entry or track element.
233 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
235 // Return value (0 = service remains top of stack, 1 means it can be removed)
238 // Get the parent producer
239 enum service_type type = mlt_invalid_type;
240 mlt_service container = context_pop_service( context, &type );
243 if ( service != NULL && container != NULL )
245 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
246 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
247 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
252 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
253 char *hide_s = mlt_properties_get( properties, "hide" );
255 // Indicate that this service is no longer top of stack
260 case mlt_tractor_type:
262 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
263 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
266 case mlt_multitrack_type:
268 mlt_multitrack_connect( MLT_MULTITRACK( container ),
269 MLT_PRODUCER( service ),
270 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
273 case mlt_playlist_type:
275 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
280 mlt_log_warning( NULL, "[producer_xml] Producer defined inside something that isn't a container\n" );
284 // Set the hide state of the track producer
285 if ( hide_s != NULL )
287 if ( strcmp( hide_s, "video" ) == 0 )
288 mlt_properties_set_int( properties, "hide", 1 );
289 else if ( strcmp( hide_s, "audio" ) == 0 )
290 mlt_properties_set_int( properties, "hide", 2 );
291 else if ( strcmp( hide_s, "both" ) == 0 )
292 mlt_properties_set_int( properties, "hide", 3 );
296 // Put the parent producer back
297 if ( container != NULL )
298 context_push_service( context, container, type );
303 /** Attach filters defined on that to this.
306 static void attach_filters( mlt_service service, mlt_service that )
311 mlt_filter filter = NULL;
312 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
314 mlt_service_attach( service, filter );
315 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
320 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
322 mlt_profile p = context->profile;
323 for ( ; atts != NULL && *atts != NULL; atts += 2 )
325 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
327 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
330 p->description = strdup( my_profile->description );
331 p->display_aspect_den = my_profile->display_aspect_den;
332 p->display_aspect_num = my_profile->display_aspect_num;
333 p->frame_rate_den = my_profile->frame_rate_den;
334 p->frame_rate_num = my_profile->frame_rate_num;
335 p->width = my_profile->width;
336 p->height = my_profile->height;
337 p->progressive = my_profile->progressive;
338 p->sample_aspect_den = my_profile->sample_aspect_den;
339 p->sample_aspect_num = my_profile->sample_aspect_num;
340 p->colorspace = my_profile->colorspace;
342 mlt_profile_close( my_profile );
345 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
347 if ( p->description )
348 free( p->description );
349 p->description = strdup( _s(atts[ 1 ]) );
352 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
353 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
354 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
355 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
356 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
357 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
358 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
359 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
360 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
361 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
362 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
363 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
364 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
365 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
366 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
367 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
368 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
369 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
370 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
371 p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
375 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
377 mlt_tractor tractor = mlt_tractor_new( );
378 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
379 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
381 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
382 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );
384 for ( ; atts != NULL && *atts != NULL; atts += 2 )
385 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
387 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
389 if ( mlt_properties_get( properties, "id" ) != NULL )
390 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
392 context_push_service( context, service, mlt_tractor_type );
395 static void on_end_tractor( deserialise_context context, const xmlChar *name )
398 enum service_type type;
399 mlt_service tractor = context_pop_service( context, &type );
401 if ( tractor != NULL && type == mlt_tractor_type )
403 // See if the tractor should be added to a playlist or multitrack
404 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
405 context_push_service( context, tractor, type );
409 mlt_log_error( NULL, "[producer_xml] Invalid state for tractor\n" );
413 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
415 enum service_type type;
416 mlt_service parent = context_pop_service( context, &type );
418 // If we don't have a parent, then create one now, providing we're in a state where we can
419 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
421 mlt_tractor tractor = NULL;
422 // Push the parent back
423 if ( parent != NULL )
424 context_push_service( context, parent, type );
426 // Create a tractor to contain the multitrack
427 tractor = mlt_tractor_new( );
428 parent = MLT_TRACTOR_SERVICE( tractor );
429 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
430 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
431 type = mlt_tractor_type;
433 // Flag it as a synthesised tractor for clean up later
434 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
437 if ( type == mlt_tractor_type )
439 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
440 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
441 for ( ; atts != NULL && *atts != NULL; atts += 2 )
442 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
444 if ( mlt_properties_get( properties, "id" ) != NULL )
445 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
447 context_push_service( context, parent, type );
448 context_push_service( context, service, mlt_multitrack_type );
452 mlt_log_error( NULL, "[producer_xml] Invalid multitrack position\n" );
456 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
458 // Get the multitrack from the stack
459 enum service_type type;
460 mlt_service service = context_pop_service( context, &type );
462 if ( service == NULL || type != mlt_multitrack_type )
463 mlt_log_error( NULL, "[producer_xml] End multitrack in the wrong state...\n" );
466 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
468 mlt_playlist playlist = mlt_playlist_init( );
469 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
470 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
472 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
474 for ( ; atts != NULL && *atts != NULL; atts += 2 )
476 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
478 // Out will be overwritten later as we append, so we need to save it
479 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
480 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
483 if ( mlt_properties_get( properties, "id" ) != NULL )
484 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
486 context_push_service( context, service, mlt_playlist_type );
489 static void on_end_playlist( deserialise_context context, const xmlChar *name )
491 // Get the playlist from the stack
492 enum service_type type;
493 mlt_service service = context_pop_service( context, &type );
495 if ( service != NULL && type == mlt_playlist_type )
497 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
498 mlt_position in = -1;
499 mlt_position out = -1;
501 if ( mlt_properties_get( properties, "in" ) )
502 in = mlt_properties_get_position( properties, "in" );
503 if ( mlt_properties_get( properties, "out" ) )
504 out = mlt_properties_get_position( properties, "out" );
506 // See if the playlist should be added to a playlist or multitrack
507 if ( add_producer( context, service, in, out ) == 0 )
508 context_push_service( context, service, type );
512 mlt_log_error( NULL, "[producer_xml] Invalid state of playlist end %d\n", type );
516 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
518 // use a dummy service to hold properties to allow arbitrary nesting
519 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
520 mlt_service_init( service, NULL );
522 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
524 context_push_service( context, service, mlt_dummy_producer_type );
526 for ( ; atts != NULL && *atts != NULL; atts += 2 )
527 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
530 static void on_end_producer( deserialise_context context, const xmlChar *name )
532 enum service_type type;
533 mlt_service service = context_pop_service( context, &type );
534 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
536 if ( service != NULL && type == mlt_dummy_producer_type )
538 mlt_service producer = NULL;
540 qualify_property( context, properties, "resource" );
541 char *resource = trim( mlt_properties_get( properties, "resource" ) );
543 // Let Kino-SMIL src be a synonym for resource
544 if ( resource == NULL )
546 qualify_property( context, properties, "src" );
547 resource = trim( mlt_properties_get( properties, "src" ) );
550 // Instantiate the producer
551 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
553 char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
556 char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
557 strcat( temp, service_name );
559 strcat( temp, resource );
560 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
565 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
569 // Just in case the plugin requested doesn't exist...
570 if ( !producer && resource )
571 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
573 mlt_log_error( NULL, "[producer_xml] failed to load producer \"%s\"\n", resource );
575 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
577 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
580 mlt_service_close( service );
584 // Track this producer
585 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
586 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );
588 // Propagate the properties
589 qualify_property( context, properties, "resource" );
590 qualify_property( context, properties, "luma" );
591 qualify_property( context, properties, "luma.resource" );
592 qualify_property( context, properties, "composite.luma" );
593 qualify_property( context, properties, "producer.resource" );
595 // Handle in/out properties separately
596 mlt_position in = -1;
597 mlt_position out = -1;
600 if ( mlt_properties_get( properties, "in" ) )
601 in = mlt_properties_get_position( properties, "in" );
602 // Let Kino-SMIL clipBegin be a synonym for in
603 else if ( mlt_properties_get( properties, "clipBegin" ) )
604 in = mlt_properties_get_position( properties, "clipBegin" );
606 if ( mlt_properties_get( properties, "out" ) )
607 out = mlt_properties_get_position( properties, "out" );
608 // Let Kino-SMIL clipEnd be a synonym for out
609 else if ( mlt_properties_get( properties, "clipEnd" ) )
610 out = mlt_properties_get_position( properties, "clipEnd" );
612 mlt_properties_set( properties, "in", NULL );
613 mlt_properties_set( properties, "out", NULL );
615 // Inherit the properties
616 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
618 // Attach all filters from service onto producer
619 attach_filters( producer, service );
621 // Add the producer to the producer map
622 if ( mlt_properties_get( properties, "id" ) != NULL )
623 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
625 // See if the producer should be added to a playlist or multitrack
626 if ( add_producer( context, producer, in, out ) == 0 )
628 // Otherwise, set in and out on...
629 if ( in != -1 || out != -1 )
631 // Get the parent service
632 enum service_type type;
633 mlt_service parent = context_pop_service( context, &type );
634 if ( parent != NULL )
636 // Get the parent properties
637 properties = MLT_SERVICE_PROPERTIES( parent );
639 char *resource = trim( mlt_properties_get( properties, "resource" ) );
641 // Put the parent producer back
642 context_push_service( context, parent, type );
644 // If the parent is a track or entry
645 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
647 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
648 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
652 // Otherwise, set in and out on producer directly
653 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
658 // Otherwise, set in and out on producer directly
659 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
663 // Push the producer onto the stack
664 context_push_service( context, producer, mlt_producer_type );
667 mlt_service_close( service );
671 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
673 // Get the playlist from the stack
674 enum service_type type;
675 mlt_service service = context_pop_service( context, &type );
676 mlt_position length = 0;
678 if ( type == mlt_playlist_type && service != NULL )
680 // Look for the length attribute
681 for ( ; atts != NULL && *atts != NULL; atts += 2 )
683 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
685 length = atoll( _s(atts[1]) );
690 // Append a blank to the playlist
691 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
693 // Push the playlist back onto the stack
694 context_push_service( context, service, type );
698 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
702 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
704 mlt_producer entry = NULL;
705 mlt_properties temp = mlt_properties_new( );
706 mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
707 mlt_properties_set_lcnumeric( temp, context->lc_numeric );
709 for ( ; atts != NULL && *atts != NULL; atts += 2 )
711 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
713 // Look for the producer attribute
714 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
716 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
717 if ( producer != NULL )
718 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
722 // If we have a valid entry
723 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
725 mlt_playlist_clip_info info;
726 enum service_type parent_type = invalid_type;
727 mlt_service parent = context_pop_service( context, &parent_type );
728 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
730 if ( parent_type == mlt_playlist_type )
732 // Append the producer to the playlist
733 mlt_position in = -1;
734 mlt_position out = -1;
735 if ( mlt_properties_get( temp, "in" ) )
736 in = mlt_properties_get_position( temp, "in" );
737 if ( mlt_properties_get( temp, "out" ) )
738 out = mlt_properties_get_position( temp, "out" );
739 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
741 // Handle the repeat property
742 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
744 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
745 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
746 mlt_properties_get_int( temp, "repeat" ) );
749 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
754 mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
757 context_push_service( context, parent, parent_type );
760 // Push the cut onto the stack
761 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
763 mlt_properties_close( temp );
766 static void on_end_entry( deserialise_context context, const xmlChar *name )
768 // Get the entry from the stack
769 enum service_type entry_type = invalid_type;
770 mlt_service entry = context_pop_service( context, &entry_type );
772 if ( entry == NULL && entry_type != mlt_entry_type )
774 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
778 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
780 // use a dummy service to hold properties to allow arbitrary nesting
781 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
782 mlt_service_init( service, NULL );
784 // Push the dummy service onto the stack
785 context_push_service( context, service, mlt_entry_type );
787 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
789 for ( ; atts != NULL && *atts != NULL; atts += 2 )
791 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
793 // Look for the producer attribute
794 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
796 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
797 if ( producer != NULL )
798 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
803 static void on_end_track( deserialise_context context, const xmlChar *name )
805 // Get the track from the stack
806 enum service_type track_type;
807 mlt_service track = context_pop_service( context, &track_type );
809 if ( track != NULL && track_type == mlt_entry_type )
811 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
812 enum service_type parent_type = invalid_type;
813 mlt_service parent = context_pop_service( context, &parent_type );
814 mlt_multitrack multitrack = NULL;
816 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
817 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
819 if ( parent_type == mlt_tractor_type )
820 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
821 else if ( parent_type == mlt_multitrack_type )
822 multitrack = MLT_MULTITRACK( parent );
824 mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
826 if ( multitrack != NULL )
828 // Set producer i/o if specified
829 if ( mlt_properties_get( track_props, "in" ) != NULL ||
830 mlt_properties_get( track_props, "out" ) != NULL )
832 mlt_position in = -1;
833 mlt_position out = -1;
834 if ( mlt_properties_get( track_props, "in" ) )
835 in = mlt_properties_get_position( track_props, "in" );
836 if ( mlt_properties_get( track_props, "out" ) )
837 out = mlt_properties_get_position( track_props, "out" );
838 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
839 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
840 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
841 track_props = MLT_PRODUCER_PROPERTIES( cut );
842 mlt_producer_close( cut );
846 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
849 // Set the hide state of the track producer
850 char *hide_s = mlt_properties_get( track_props, "hide" );
851 if ( hide_s != NULL )
853 if ( strcmp( hide_s, "video" ) == 0 )
854 mlt_properties_set_int( producer_props, "hide", 1 );
855 else if ( strcmp( hide_s, "audio" ) == 0 )
856 mlt_properties_set_int( producer_props, "hide", 2 );
857 else if ( strcmp( hide_s, "both" ) == 0 )
858 mlt_properties_set_int( producer_props, "hide", 3 );
862 if ( parent != NULL )
863 context_push_service( context, parent, parent_type );
865 mlt_service_close( track );
869 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
873 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
875 // use a dummy service to hold properties to allow arbitrary nesting
876 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
877 mlt_service_init( service, NULL );
879 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
881 context_push_service( context, service, mlt_dummy_filter_type );
883 // Set the properties
884 for ( ; atts != NULL && *atts != NULL; atts += 2 )
885 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
888 static void on_end_filter( deserialise_context context, const xmlChar *name )
890 enum service_type type;
891 mlt_service service = context_pop_service( context, &type );
892 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
894 enum service_type parent_type = invalid_type;
895 mlt_service parent = context_pop_service( context, &parent_type );
897 if ( service != NULL && type == mlt_dummy_filter_type )
899 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
900 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
901 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
905 mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
907 context_push_service( context, parent, parent_type );
908 mlt_service_close( service );
912 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
913 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
915 // Propogate the properties
916 qualify_property( context, properties, "resource" );
917 qualify_property( context, properties, "luma" );
918 qualify_property( context, properties, "luma.resource" );
919 qualify_property( context, properties, "composite.luma" );
920 qualify_property( context, properties, "producer.resource" );
921 mlt_properties_inherit( filter_props, properties );
923 // Attach all filters from service onto filter
924 attach_filters( filter, service );
926 // Associate the filter with the parent
927 if ( parent != NULL )
929 if ( parent_type == mlt_tractor_type )
931 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
932 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
933 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
934 mlt_properties_get_int( properties, "in" ),
935 mlt_properties_get_int( properties, "out" ) );
939 mlt_service_attach( parent, MLT_FILTER( filter ) );
942 // Put the parent back on the stack
943 context_push_service( context, parent, parent_type );
947 mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
950 // Close the dummy filter service
951 mlt_service_close( service );
955 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
959 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
961 // use a dummy service to hold properties to allow arbitrary nesting
962 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
963 mlt_service_init( service, NULL );
965 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
967 context_push_service( context, service, mlt_dummy_transition_type );
969 // Set the properties
970 for ( ; atts != NULL && *atts != NULL; atts += 2 )
971 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
974 static void on_end_transition( deserialise_context context, const xmlChar *name )
976 enum service_type type;
977 mlt_service service = context_pop_service( context, &type );
978 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
980 enum service_type parent_type = invalid_type;
981 mlt_service parent = context_pop_service( context, &parent_type );
983 if ( service != NULL && type == mlt_dummy_transition_type )
985 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
986 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
987 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
991 mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
993 context_push_service( context, parent, parent_type );
994 mlt_service_close( service );
997 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
998 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1000 // Propogate the properties
1001 qualify_property( context, properties, "resource" );
1002 qualify_property( context, properties, "luma" );
1003 qualify_property( context, properties, "luma.resource" );
1004 qualify_property( context, properties, "composite.luma" );
1005 qualify_property( context, properties, "producer.resource" );
1006 mlt_properties_inherit( effect_props, properties );
1008 // Attach all filters from service onto effect
1009 attach_filters( effect, service );
1011 // Associate the filter with the parent
1012 if ( parent != NULL )
1014 if ( parent_type == mlt_tractor_type )
1016 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1017 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1018 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1019 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1020 mlt_properties_get_int( properties, "a_track" ),
1021 mlt_properties_get_int( properties, "b_track" ) );
1022 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1023 mlt_properties_get_int( properties, "in" ),
1024 mlt_properties_get_int( properties, "out" ) );
1028 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1031 // Put the parent back on the stack
1032 context_push_service( context, parent, parent_type );
1036 mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1039 // Close the dummy filter service
1040 mlt_service_close( service );
1044 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1048 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1050 if ( context->pass == 1 )
1052 mlt_consumer consumer = mlt_consumer_new( context->profile );
1053 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1055 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1056 context_push_service( context, MLT_CONSUMER_SERVICE(consumer), mlt_dummy_consumer_type );
1058 // Set the properties from attributes
1059 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1060 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1064 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1066 if ( context->pass == 1 )
1068 // Get the consumer from the stack
1069 enum service_type type;
1070 mlt_service service = context_pop_service( context, &type );
1072 if ( service && type == mlt_dummy_consumer_type )
1074 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1075 qualify_property( context, properties, "resource" );
1076 qualify_property( context, properties, "target" );
1077 char *resource = trim( mlt_properties_get( properties, "resource" ) );
1079 if ( context->multi_consumer > 1 )
1081 // Instantiate the multi consumer
1082 if ( !context->consumer )
1084 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1085 if ( context->consumer )
1087 // Track this consumer
1088 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1089 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1092 if ( context->consumer )
1094 // Set this service instance on multi consumer
1096 snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1097 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, service, 0,
1098 (mlt_destructor) mlt_service_close, NULL );
1103 // Instantiate the consumer
1104 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1105 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1106 if ( context->consumer )
1108 // Track this consumer
1109 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1110 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1112 // Inherit the properties
1113 mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1116 mlt_service_close( service );
1122 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1124 enum service_type type;
1125 mlt_service service = context_pop_service( context, &type );
1126 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1127 const char *value = NULL;
1129 if ( service != NULL )
1131 // Set the properties
1132 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1134 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1135 context->property = strdup( _s(atts[ 1 ]) );
1136 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1137 value = _s(atts[ 1 ]);
1140 if ( context->property != NULL )
1141 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1143 // Tell parser to collect any further nodes for serialisation
1144 context->is_value = 1;
1146 context_push_service( context, service, type );
1150 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1154 static void on_end_property( deserialise_context context, const xmlChar *name )
1156 enum service_type type;
1157 mlt_service service = context_pop_service( context, &type );
1158 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1160 if ( service != NULL )
1162 // Tell parser to stop building a tree
1163 context->is_value = 0;
1165 // See if there is a xml tree for the value
1166 if ( context->property != NULL && context->value_doc != NULL )
1171 // Serialise the tree to get value
1172 xmlDocDumpMemory( context->value_doc, &value, &size );
1173 mlt_properties_set( properties, context->property, _s(value) );
1175 xmlFreeFunc xmlFree = NULL;
1176 xmlMemGet( &xmlFree, NULL, NULL, NULL);
1179 xmlFreeDoc( context->value_doc );
1180 context->value_doc = NULL;
1183 // Close this property handling
1184 free( context->property );
1185 context->property = NULL;
1187 context_push_service( context, service, type );
1191 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1195 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1197 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1198 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1200 if ( context->pass == 0 )
1202 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1203 xmlStrcmp( name, _x("profile") ) == 0 ||
1204 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1205 on_start_profile( context, name, atts );
1206 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1207 context->multi_consumer++;
1210 context->branch[ context->depth ] ++;
1213 // Build a tree from nodes within a property value
1214 if ( context->is_value == 1 && context->pass == 1 )
1216 xmlNodePtr node = xmlNewNode( NULL, name );
1218 if ( context->value_doc == NULL )
1221 context->value_doc = xmlNewDoc( _x("1.0") );
1222 xmlDocSetRootElement( context->value_doc, node );
1226 // Append child to tree
1227 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1229 context_push_node( context, node );
1231 // Set the attributes
1232 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1233 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1235 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1236 on_start_tractor( context, name, atts );
1237 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1238 on_start_multitrack( context, name, atts );
1239 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1240 on_start_playlist( context, name, atts );
1241 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1242 on_start_producer( context, name, atts );
1243 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1244 on_start_blank( context, name, atts );
1245 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1246 on_start_entry( context, name, atts );
1247 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1248 on_start_track( context, name, atts );
1249 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1250 on_start_filter( context, name, atts );
1251 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1252 on_start_transition( context, name, atts );
1253 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1254 on_start_property( context, name, atts );
1255 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1256 on_start_consumer( context, name, atts );
1257 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1259 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1261 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1262 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1263 else if ( !context->lc_numeric )
1264 context->lc_numeric = strdup( _s( atts[1] ) );
1269 static void on_end_element( void *ctx, const xmlChar *name )
1271 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1272 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1274 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1275 context_pop_node( context );
1276 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1277 on_end_multitrack( context, name );
1278 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1279 on_end_playlist( context, name );
1280 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1281 on_end_track( context, name );
1282 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1283 on_end_entry( context, name );
1284 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1285 on_end_tractor( context, name );
1286 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1287 on_end_property( context, name );
1288 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1289 on_end_producer( context, name );
1290 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1291 on_end_filter( context, name );
1292 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1293 on_end_transition( context, name );
1294 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1295 on_end_consumer( context, name );
1297 context->branch[ context->depth ] = 0;
1301 static void on_characters( void *ctx, const xmlChar *ch, int len )
1303 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1304 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1305 char *value = calloc( len + 1, 1 );
1306 enum service_type type;
1307 mlt_service service = context_pop_service( context, &type );
1308 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1310 if ( service != NULL )
1311 context_push_service( context, service, type );
1314 strncpy( value, (const char*) ch, len );
1316 if ( context->stack_node_size > 0 )
1317 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1319 // libxml2 generates an on_characters immediately after a get_entity within
1320 // an element value, and we ignore it because it is called again during
1321 // actual substitution.
1322 else if ( context->property != NULL && context->entity_is_replace == 0 )
1324 char *s = mlt_properties_get( properties, context->property );
1327 // Append new text to existing content
1328 char *new = calloc( strlen( s ) + len + 1, 1 );
1330 strcat( new, value );
1331 mlt_properties_set( properties, context->property, new );
1335 mlt_properties_set( properties, context->property, value );
1337 context->entity_is_replace = 0;
1342 /** Convert parameters parsed from resource into entity declarations.
1344 static void params_to_entities( deserialise_context context )
1346 if ( context->params != NULL )
1350 // Add our params as entitiy declarations
1351 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1353 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1354 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1355 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1359 mlt_properties_close( context->params );
1360 context->params = NULL;
1364 // The following 3 facilitate entity substitution in the SAX parser
1365 static void on_internal_subset( void *ctx, const xmlChar* name,
1366 const xmlChar* publicId, const xmlChar* systemId )
1368 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1369 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1371 context->publicId = publicId;
1372 context->systemId = systemId;
1373 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1375 // Override default entities with our parameters
1376 params_to_entities( context );
1379 // TODO: Check this with Dan... I think this is for parameterisation
1380 // but it's breaking standard escaped entities (like < etc).
1381 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1382 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1384 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1385 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1387 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1390 // TODO: Check this functionality (see on_entity_declaration)
1391 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1393 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1394 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1395 xmlEntityPtr e = NULL;
1397 // Setup for entity declarations if not ready
1398 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1400 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1401 context->publicId = _x("");
1402 context->systemId = _x("");
1405 // Add our parameters if not already
1406 params_to_entities( context );
1408 e = xmlGetPredefinedEntity( name );
1410 // Send signal to on_characters that an entity substitutin is pending
1413 e = xmlGetDocEntity( context->entity_doc, name );
1415 context->entity_is_replace = 1;
1421 static void on_error( void * ctx, const char * msg, ... )
1423 struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1425 switch( err_ptr->level )
1427 case XML_ERR_WARNING:
1428 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1429 err_ptr->message, err_ptr->line, err_ptr->int2 );
1432 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1433 err_ptr->message, err_ptr->line, err_ptr->int2 );
1437 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1438 err_ptr->message, err_ptr->line, err_ptr->int2 );
1443 /** Convert a hexadecimal character to its value.
1445 static int tohex( char p )
1447 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1450 /** Decode a url-encoded string containing hexadecimal character sequences.
1452 static char *url_decode( char *dest, char *src )
1460 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1473 /** Extract the filename from a URL attaching parameters to a properties list.
1475 static void parse_url( mlt_properties properties, char *url )
1478 int n = strlen( url );
1482 for ( i = 0; i < n; i++ )
1499 if ( name != NULL && value != NULL )
1500 mlt_properties_set( properties, name, value );
1506 if ( name != NULL && value != NULL )
1507 mlt_properties_set( properties, name, value );
1510 // Quick workaround to avoid unecessary libxml2 warnings
1511 static int file_exists( char *file )
1513 char *name = strdup( file );
1515 if ( name != NULL && strchr( name, '?' ) )
1516 *( strchr( name, '?' ) ) = '\0';
1519 FILE *f = fopen( name, "r" );
1521 if ( exists ) fclose( f );
1527 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1529 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1530 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1531 mlt_properties properties = NULL;
1533 struct _xmlParserCtxt *xmlcontext;
1534 int well_formed = 0;
1535 char *filename = NULL;
1536 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1538 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1541 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1542 if ( context == NULL )
1545 context->producer_map = mlt_properties_new();
1546 context->destructors = mlt_properties_new();
1547 context->params = mlt_properties_new();
1548 context->profile = profile;
1550 // Decode URL and parse parameters
1551 mlt_properties_set( context->producer_map, "root", "" );
1554 filename = strdup( data );
1555 parse_url( context->params, url_decode( filename, data ) );
1557 // We need the directory prefix which was used for the xml
1558 if ( strchr( filename, '/' ) )
1561 mlt_properties_set( context->producer_map, "root", filename );
1562 root = mlt_properties_get( context->producer_map, "root" );
1563 *( strrchr( root, '/' ) ) = '\0';
1565 // If we don't have an absolute path here, we're heading for disaster...
1566 if ( root[ 0 ] != '/' )
1568 char *cwd = getcwd( NULL, 0 );
1569 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1570 sprintf( real, "%s/%s", cwd, root );
1571 mlt_properties_set( context->producer_map, "root", real );
1578 // We need to track the number of registered filters
1579 mlt_properties_set_int( context->destructors, "registered", 0 );
1581 // Setup SAX callbacks for first pass
1582 sax->startElement = on_start_element;
1583 sax->warning = on_error;
1584 sax->error = on_error;
1585 sax->fatalError = on_error;
1587 // Setup libxml2 SAX parsing
1589 xmlSubstituteEntitiesDefault( 1 );
1590 // This is used to facilitate entity substitution in the SAX parser
1591 context->entity_doc = xmlNewDoc( _x("1.0") );
1593 xmlcontext = xmlCreateFileParserCtxt( filename );
1595 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1597 // Invalid context - clean up and return NULL
1598 if ( xmlcontext == NULL )
1600 mlt_properties_close( context->producer_map );
1601 mlt_properties_close( context->destructors );
1602 mlt_properties_close( context->params );
1610 xmlcontext->sax = sax;
1611 xmlcontext->_private = ( void* )context;
1612 xmlParseDocument( xmlcontext );
1613 well_formed = xmlcontext->wellFormed;
1615 // Cleanup after parsing
1616 xmlcontext->sax = NULL;
1617 xmlcontext->_private = NULL;
1618 xmlFreeParserCtxt( xmlcontext );
1619 context->stack_node_size = 0;
1620 context->stack_service_size = 0;
1622 // Bad xml - clean up and return NULL
1625 mlt_properties_close( context->producer_map );
1626 mlt_properties_close( context->destructors );
1627 mlt_properties_close( context->params );
1628 xmlFreeDoc( context->entity_doc );
1635 // Setup the second pass
1638 xmlcontext = xmlCreateFileParserCtxt( filename );
1640 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1642 // Invalid context - clean up and return NULL
1643 if ( xmlcontext == NULL )
1645 mlt_properties_close( context->producer_map );
1646 mlt_properties_close( context->destructors );
1647 mlt_properties_close( context->params );
1648 xmlFreeDoc( context->entity_doc );
1655 // Setup SAX callbacks for second pass
1656 sax->endElement = on_end_element;
1657 sax->characters = on_characters;
1658 sax->cdataBlock = on_characters;
1659 sax->internalSubset = on_internal_subset;
1660 sax->entityDecl = on_entity_declaration;
1661 sax->getEntity = on_get_entity;
1664 xmlcontext->sax = sax;
1665 xmlcontext->_private = ( void* )context;
1666 xmlParseDocument( xmlcontext );
1667 well_formed = xmlcontext->wellFormed;
1669 // Cleanup after parsing
1670 xmlFreeDoc( context->entity_doc );
1672 xmlMemoryDump( ); // for debugging
1674 // Get the last producer on the stack
1675 enum service_type type;
1676 mlt_service service = context_pop_service( context, &type );
1677 if ( well_formed && service != NULL )
1679 // Verify it is a producer service (mlt_type="mlt_producer")
1680 // (producer, playlist, multitrack)
1681 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1682 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1687 xmlDocPtr doc = xml_make_doc( service );
1688 xmlDocFormatDump( stdout, doc, 1 );
1693 if ( well_formed && service != NULL )
1695 char *title = mlt_properties_get( context->producer_map, "title" );
1697 // Need the complete producer list for various reasons
1698 properties = context->destructors;
1700 // Now make sure we don't have a reference to the service in the properties
1701 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1703 char *name = mlt_properties_get_name( properties, i );
1704 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1706 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1711 // We are done referencing destructor property list
1712 // Set this var to service properties for convenience
1713 properties = MLT_SERVICE_PROPERTIES( service );
1716 mlt_properties_set( properties, "title", title );
1718 // Optimise for overlapping producers
1719 mlt_producer_optimise( MLT_PRODUCER( service ) );
1721 // Handle deep copies
1722 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1724 // Now assign additional properties
1726 mlt_properties_set( properties, "resource", data );
1728 // This tells consumer_xml not to deep copy
1729 mlt_properties_set( properties, "xml", "was here" );
1733 // Allow the project to be edited
1734 mlt_properties_set( properties, "_xml", "was here" );
1735 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1738 // Make consumer available
1739 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1740 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1741 (mlt_destructor) mlt_consumer_close, NULL );
1745 // Return null if not well formed
1750 mlt_properties_close( context->producer_map );
1751 if ( context->params != NULL )
1752 mlt_properties_close( context->params );
1753 mlt_properties_close( context->destructors );
1754 if ( context->lc_numeric )
1755 free( context->lc_numeric );
1759 return MLT_PRODUCER( service );