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;
93 typedef struct deserialise_context_s *deserialise_context;
95 /** Trim the leading and trailing whitespace from a string in-place.
97 static char* trim( char *s )
100 if ( s && ( n = strlen( s ) ) )
103 while ( i < n && isspace( s[i] ) ) i++;
104 while ( --n && isspace( s[n] ) );
107 memmove( s, s + i, n );
113 /** Convert the numerical current branch address to a dot-delimited string.
115 static char *serialise_branch( deserialise_context context, char *s )
120 for ( i = 0; i < context->depth; i++ )
122 int len = strlen( s );
123 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", context->branch[ i ] );
131 static int context_push_service( deserialise_context context, mlt_service that, enum service_type type )
133 int ret = context->stack_service_size >= STACK_SIZE - 1;
136 context->stack_service[ context->stack_service_size ] = that;
137 context->stack_types[ context->stack_service_size++ ] = type;
139 // Record the tree branch on which this service lives
140 if ( that != NULL && mlt_properties_get( MLT_SERVICE_PROPERTIES( that ), "_xml_branch" ) == NULL )
142 char s[ BRANCH_SIG_LEN ];
143 mlt_properties_set( MLT_SERVICE_PROPERTIES( that ), "_xml_branch", serialise_branch( context, s ) );
152 static mlt_service context_pop_service( deserialise_context context, enum service_type *type )
154 mlt_service result = NULL;
156 *type = invalid_type;
157 if ( context->stack_service_size > 0 )
159 result = context->stack_service[ -- context->stack_service_size ];
161 *type = context->stack_types[ context->stack_service_size ];
162 // Set the service's profile and locale so mlt_property time-to-position conversions can get fps
163 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( result ), "_profile", context->profile, 0, NULL, NULL );
164 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( result ), context->lc_numeric );
172 static int context_push_node( deserialise_context context, xmlNodePtr node )
174 int ret = context->stack_node_size >= STACK_SIZE - 1;
176 context->stack_node[ context->stack_node_size ++ ] = node;
183 static xmlNodePtr context_pop_node( deserialise_context context )
185 xmlNodePtr result = NULL;
186 if ( context->stack_node_size > 0 )
187 result = context->stack_node[ -- context->stack_node_size ];
192 // Set the destructor on a new service
193 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
195 int registered = mlt_properties_get_int( properties, "registered" );
196 char *key = mlt_properties_get( properties, "registered" );
197 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
198 mlt_properties_set_int( properties, "registered", ++ registered );
202 // Prepend the property value with the document root
203 static inline void qualify_property( deserialise_context context, mlt_properties properties, const char *name )
205 char *resource = mlt_properties_get( properties, name );
206 if ( resource != NULL && resource[0] )
208 // Qualify file name properties
209 char *root = mlt_properties_get( context->producer_map, "root" );
210 if ( root != NULL && strcmp( root, "" ) )
212 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 2 );
213 if ( resource[ 0 ] != '/' && strchr( resource, ':' ) == NULL )
215 strcpy( full_resource, root );
216 strcat( full_resource, "/" );
217 strcat( full_resource, resource );
221 strcpy( full_resource, resource );
223 mlt_properties_set( properties, name, full_resource );
224 free( full_resource );
230 /** This function adds a producer to a playlist or multitrack when
231 there is no entry or track element.
234 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
236 // Return value (0 = service remains top of stack, 1 means it can be removed)
239 // Get the parent producer
240 enum service_type type = mlt_invalid_type;
241 mlt_service container = context_pop_service( context, &type );
244 if ( service != NULL && container != NULL )
246 char *container_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( container ), "_xml_branch" );
247 char *service_branch = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "_xml_branch" );
248 contained = !strncmp( container_branch, service_branch, strlen( container_branch ) );
253 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
254 char *hide_s = mlt_properties_get( properties, "hide" );
256 // Indicate that this service is no longer top of stack
261 case mlt_tractor_type:
263 mlt_multitrack multitrack = mlt_tractor_multitrack( MLT_TRACTOR( container ) );
264 mlt_multitrack_connect( multitrack, MLT_PRODUCER( service ), mlt_multitrack_count( multitrack ) );
267 case mlt_multitrack_type:
269 mlt_multitrack_connect( MLT_MULTITRACK( container ),
270 MLT_PRODUCER( service ),
271 mlt_multitrack_count( MLT_MULTITRACK( container ) ) );
274 case mlt_playlist_type:
276 mlt_playlist_append_io( MLT_PLAYLIST( container ), MLT_PRODUCER( service ), in, out );
281 mlt_log_warning( NULL, "[producer_xml] Producer defined inside something that isn't a container\n" );
285 // Set the hide state of the track producer
286 if ( hide_s != NULL )
288 if ( strcmp( hide_s, "video" ) == 0 )
289 mlt_properties_set_int( properties, "hide", 1 );
290 else if ( strcmp( hide_s, "audio" ) == 0 )
291 mlt_properties_set_int( properties, "hide", 2 );
292 else if ( strcmp( hide_s, "both" ) == 0 )
293 mlt_properties_set_int( properties, "hide", 3 );
297 // Put the parent producer back
298 if ( container != NULL )
299 context_push_service( context, container, type );
304 /** Attach filters defined on that to this.
307 static void attach_filters( mlt_service service, mlt_service that )
312 mlt_filter filter = NULL;
313 for ( i = 0; ( filter = mlt_service_filter( that, i ) ) != NULL; i ++ )
315 mlt_service_attach( service, filter );
316 attach_filters( MLT_FILTER_SERVICE( filter ), MLT_FILTER_SERVICE( filter ) );
321 static void on_start_profile( deserialise_context context, const xmlChar *name, const xmlChar **atts)
323 mlt_profile p = context->profile;
324 for ( ; atts != NULL && *atts != NULL; atts += 2 )
326 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 || xmlStrcmp( atts[ 0 ], _x("profile") ) == 0 )
328 mlt_profile my_profile = mlt_profile_init( _s(atts[ 1 ]) );
331 p->description = strdup( my_profile->description );
332 p->display_aspect_den = my_profile->display_aspect_den;
333 p->display_aspect_num = my_profile->display_aspect_num;
334 p->frame_rate_den = my_profile->frame_rate_den;
335 p->frame_rate_num = my_profile->frame_rate_num;
336 p->width = my_profile->width;
337 p->height = my_profile->height;
338 p->progressive = my_profile->progressive;
339 p->sample_aspect_den = my_profile->sample_aspect_den;
340 p->sample_aspect_num = my_profile->sample_aspect_num;
341 p->colorspace = my_profile->colorspace;
343 mlt_profile_close( my_profile );
346 else if ( xmlStrcmp( atts[ 0 ], _x("description") ) == 0 )
348 if ( p->description )
349 free( p->description );
350 p->description = strdup( _s(atts[ 1 ]) );
353 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_den") ) == 0 )
354 p->display_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
355 else if ( xmlStrcmp( atts[ 0 ], _x("display_aspect_num") ) == 0 )
356 p->display_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
357 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_num") ) == 0 )
358 p->sample_aspect_num = strtol( _s(atts[ 1 ]), NULL, 0 );
359 else if ( xmlStrcmp( atts[ 0 ], _x("sample_aspect_den") ) == 0 )
360 p->sample_aspect_den = strtol( _s(atts[ 1 ]), NULL, 0 );
361 else if ( xmlStrcmp( atts[ 0 ], _x("width") ) == 0 )
362 p->width = strtol( _s(atts[ 1 ]), NULL, 0 );
363 else if ( xmlStrcmp( atts[ 0 ], _x("height") ) == 0 )
364 p->height = strtol( _s(atts[ 1 ]), NULL, 0 );
365 else if ( xmlStrcmp( atts[ 0 ], _x("progressive") ) == 0 )
366 p->progressive = strtol( _s(atts[ 1 ]), NULL, 0 );
367 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_num") ) == 0 )
368 p->frame_rate_num = strtol( _s(atts[ 1 ]), NULL, 0 );
369 else if ( xmlStrcmp( atts[ 0 ], _x("frame_rate_den") ) == 0 )
370 p->frame_rate_den = strtol( _s(atts[ 1 ]), NULL, 0 );
371 else if ( xmlStrcmp( atts[ 0 ], _x("colorspace") ) == 0 )
372 p->colorspace = strtol( _s(atts[ 1 ]), NULL, 0 );
376 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
378 mlt_tractor tractor = mlt_tractor_new( );
379 mlt_service service = MLT_TRACTOR_SERVICE( tractor );
380 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
382 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
383 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( service ), context->lc_numeric );
385 for ( ; atts != NULL && *atts != NULL; atts += 2 )
386 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
388 mlt_properties_set_int( MLT_TRACTOR_PROPERTIES( tractor ), "global_feed", 1 );
390 if ( mlt_properties_get( properties, "id" ) != NULL )
391 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
393 context_push_service( context, service, mlt_tractor_type );
396 static void on_end_tractor( deserialise_context context, const xmlChar *name )
399 enum service_type type;
400 mlt_service tractor = context_pop_service( context, &type );
402 if ( tractor != NULL && type == mlt_tractor_type )
404 // See if the tractor should be added to a playlist or multitrack
405 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
406 context_push_service( context, tractor, type );
410 mlt_log_error( NULL, "[producer_xml] Invalid state for tractor\n" );
414 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
416 enum service_type type;
417 mlt_service parent = context_pop_service( context, &type );
419 // If we don't have a parent, then create one now, providing we're in a state where we can
420 if ( parent == NULL || ( type == mlt_playlist_type || type == mlt_multitrack_type ) )
422 mlt_tractor tractor = NULL;
423 // Push the parent back
424 if ( parent != NULL )
425 context_push_service( context, parent, type );
427 // Create a tractor to contain the multitrack
428 tractor = mlt_tractor_new( );
429 parent = MLT_TRACTOR_SERVICE( tractor );
430 track_service( context->destructors, parent, (mlt_destructor) mlt_tractor_close );
431 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( parent ), context->lc_numeric );
432 type = mlt_tractor_type;
434 // Flag it as a synthesised tractor for clean up later
435 mlt_properties_set_int( MLT_SERVICE_PROPERTIES( parent ), "loader_synth", 1 );
438 if ( type == mlt_tractor_type )
440 mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
441 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
442 for ( ; atts != NULL && *atts != NULL; atts += 2 )
443 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
445 if ( mlt_properties_get( properties, "id" ) != NULL )
446 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties,"id" ), service, 0, NULL, NULL );
448 context_push_service( context, parent, type );
449 context_push_service( context, service, mlt_multitrack_type );
453 mlt_log_error( NULL, "[producer_xml] Invalid multitrack position\n" );
457 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
459 // Get the multitrack from the stack
460 enum service_type type;
461 mlt_service service = context_pop_service( context, &type );
463 if ( service == NULL || type != mlt_multitrack_type )
464 mlt_log_error( NULL, "[producer_xml] End multitrack in the wrong state...\n" );
467 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
469 mlt_playlist playlist = mlt_playlist_init( );
470 mlt_service service = MLT_PLAYLIST_SERVICE( playlist );
471 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
473 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
475 for ( ; atts != NULL && *atts != NULL; atts += 2 )
477 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
479 // Out will be overwritten later as we append, so we need to save it
480 if ( xmlStrcmp( atts[ 0 ], _x("out") ) == 0 )
481 mlt_properties_set( properties, "_xml.out", ( const char* )atts[ 1 ] );
484 if ( mlt_properties_get( properties, "id" ) != NULL )
485 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
487 context_push_service( context, service, mlt_playlist_type );
490 static void on_end_playlist( deserialise_context context, const xmlChar *name )
492 // Get the playlist from the stack
493 enum service_type type;
494 mlt_service service = context_pop_service( context, &type );
496 if ( service != NULL && type == mlt_playlist_type )
498 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
499 mlt_position in = -1;
500 mlt_position out = -1;
502 if ( mlt_properties_get( properties, "in" ) )
503 in = mlt_properties_get_position( properties, "in" );
504 if ( mlt_properties_get( properties, "out" ) )
505 out = mlt_properties_get_position( properties, "out" );
507 // See if the playlist should be added to a playlist or multitrack
508 if ( add_producer( context, service, in, out ) == 0 )
509 context_push_service( context, service, type );
513 mlt_log_error( NULL, "[producer_xml] Invalid state of playlist end %d\n", type );
517 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
519 // use a dummy service to hold properties to allow arbitrary nesting
520 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
521 mlt_service_init( service, NULL );
523 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
525 context_push_service( context, service, mlt_dummy_producer_type );
527 for ( ; atts != NULL && *atts != NULL; atts += 2 )
528 mlt_properties_set( properties, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
531 static void on_end_producer( deserialise_context context, const xmlChar *name )
533 enum service_type type;
534 mlt_service service = context_pop_service( context, &type );
535 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
537 if ( service != NULL && type == mlt_dummy_producer_type )
539 mlt_service producer = NULL;
541 qualify_property( context, properties, "resource" );
542 char *resource = trim( mlt_properties_get( properties, "resource" ) );
544 // Let Kino-SMIL src be a synonym for resource
545 if ( resource == NULL )
547 qualify_property( context, properties, "src" );
548 resource = trim( mlt_properties_get( properties, "src" ) );
551 // Instantiate the producer
552 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
554 char *service_name = trim( mlt_properties_get( properties, "mlt_service" ) );
557 char *temp = calloc( 1, strlen( service_name ) + strlen( resource ) + 2 );
558 strcat( temp, service_name );
560 strcat( temp, resource );
561 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, temp ) );
566 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, service_name ) );
570 // Just in case the plugin requested doesn't exist...
571 if ( !producer && resource )
572 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, resource ) );
574 mlt_log_error( NULL, "[producer_xml] failed to load producer \"%s\"\n", resource );
576 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "+INVALID.txt" ) );
578 producer = MLT_SERVICE( mlt_factory_producer( context->profile, NULL, "colour:red" ) );
581 mlt_service_close( service );
585 // Track this producer
586 track_service( context->destructors, producer, (mlt_destructor) mlt_producer_close );
587 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( producer ), context->lc_numeric );
588 context->seekable &= mlt_properties_get_int( MLT_SERVICE_PROPERTIES( producer ), "seekable" );
590 // Propagate the properties
591 qualify_property( context, properties, "resource" );
592 qualify_property( context, properties, "luma" );
593 qualify_property( context, properties, "luma.resource" );
594 qualify_property( context, properties, "composite.luma" );
595 qualify_property( context, properties, "producer.resource" );
597 // Handle in/out properties separately
598 mlt_position in = -1;
599 mlt_position out = -1;
602 if ( mlt_properties_get( properties, "in" ) )
603 in = mlt_properties_get_position( properties, "in" );
604 // Let Kino-SMIL clipBegin be a synonym for in
605 else if ( mlt_properties_get( properties, "clipBegin" ) )
606 in = mlt_properties_get_position( properties, "clipBegin" );
608 if ( mlt_properties_get( properties, "out" ) )
609 out = mlt_properties_get_position( properties, "out" );
610 // Let Kino-SMIL clipEnd be a synonym for out
611 else if ( mlt_properties_get( properties, "clipEnd" ) )
612 out = mlt_properties_get_position( properties, "clipEnd" );
614 mlt_properties_set( properties, "in", NULL );
615 mlt_properties_set( properties, "out", NULL );
617 // Inherit the properties
618 mlt_properties_inherit( MLT_SERVICE_PROPERTIES( producer ), properties );
620 // Attach all filters from service onto producer
621 attach_filters( producer, service );
623 // Add the producer to the producer map
624 if ( mlt_properties_get( properties, "id" ) != NULL )
625 mlt_properties_set_data( context->producer_map, mlt_properties_get(properties, "id"), producer, 0, NULL, NULL );
627 // See if the producer should be added to a playlist or multitrack
628 if ( add_producer( context, producer, in, out ) == 0 )
630 // Otherwise, set in and out on...
631 if ( in != -1 || out != -1 )
633 // Get the parent service
634 enum service_type type;
635 mlt_service parent = context_pop_service( context, &type );
636 if ( parent != NULL )
638 // Get the parent properties
639 properties = MLT_SERVICE_PROPERTIES( parent );
641 char *resource = trim( mlt_properties_get( properties, "resource" ) );
643 // Put the parent producer back
644 context_push_service( context, parent, type );
646 // If the parent is a track or entry
647 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
649 if ( in > -1 ) mlt_properties_set_position( properties, "in", in );
650 if ( out > -1 ) mlt_properties_set_position( properties, "out", out );
654 // Otherwise, set in and out on producer directly
655 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
660 // Otherwise, set in and out on producer directly
661 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
665 // Push the producer onto the stack
666 context_push_service( context, producer, mlt_producer_type );
669 mlt_service_close( service );
673 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
675 // Get the playlist from the stack
676 enum service_type type;
677 mlt_service service = context_pop_service( context, &type );
678 mlt_position length = 0;
680 if ( type == mlt_playlist_type && service != NULL )
682 // Look for the length attribute
683 for ( ; atts != NULL && *atts != NULL; atts += 2 )
685 if ( xmlStrcmp( atts[0], _x("length") ) == 0 )
687 length = atoll( _s(atts[1]) );
692 // Append a blank to the playlist
693 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
695 // Push the playlist back onto the stack
696 context_push_service( context, service, type );
700 mlt_log_error( NULL, "[producer_xml] blank without a playlist - a definite no no\n" );
704 static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
706 mlt_producer entry = NULL;
707 mlt_properties temp = mlt_properties_new( );
708 mlt_properties_set_data( temp, "_profile", context->profile, 0, NULL, NULL );
709 mlt_properties_set_lcnumeric( temp, context->lc_numeric );
711 for ( ; atts != NULL && *atts != NULL; atts += 2 )
713 mlt_properties_set( temp, (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
715 // Look for the producer attribute
716 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
718 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
719 if ( producer != NULL )
720 mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
724 // If we have a valid entry
725 if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
727 mlt_playlist_clip_info info;
728 enum service_type parent_type = invalid_type;
729 mlt_service parent = context_pop_service( context, &parent_type );
730 mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
732 if ( parent_type == mlt_playlist_type )
734 // Append the producer to the playlist
735 mlt_position in = -1;
736 mlt_position out = -1;
737 if ( mlt_properties_get( temp, "in" ) )
738 in = mlt_properties_get_position( temp, "in" );
739 if ( mlt_properties_get( temp, "out" ) )
740 out = mlt_properties_get_position( temp, "out" );
741 mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer, in, out );
743 // Handle the repeat property
744 if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
746 mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
747 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
748 mlt_properties_get_int( temp, "repeat" ) );
751 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
756 mlt_log_error( NULL, "[producer_xml] Entry not part of a playlist...\n" );
759 context_push_service( context, parent, parent_type );
762 // Push the cut onto the stack
763 context_push_service( context, MLT_PRODUCER_SERVICE( entry ), mlt_entry_type );
765 mlt_properties_close( temp );
768 static void on_end_entry( deserialise_context context, const xmlChar *name )
770 // Get the entry from the stack
771 enum service_type entry_type = invalid_type;
772 mlt_service entry = context_pop_service( context, &entry_type );
774 if ( entry == NULL && entry_type != mlt_entry_type )
776 mlt_log_error( NULL, "[producer_xml] Invalid state at end of entry\n" );
780 static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
782 // use a dummy service to hold properties to allow arbitrary nesting
783 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
784 mlt_service_init( service, NULL );
786 // Push the dummy service onto the stack
787 context_push_service( context, service, mlt_entry_type );
789 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "resource", "<track>" );
791 for ( ; atts != NULL && *atts != NULL; atts += 2 )
793 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), (const char*) atts[0], atts[1] == NULL ? "" : (const char*) atts[1] );
795 // Look for the producer attribute
796 if ( xmlStrcmp( atts[ 0 ], _x("producer") ) == 0 )
798 mlt_producer producer = mlt_properties_get_data( context->producer_map, (const char*) atts[1], NULL );
799 if ( producer != NULL )
800 mlt_properties_set_data( MLT_SERVICE_PROPERTIES( service ), "producer", producer, 0, NULL, NULL );
805 static void on_end_track( deserialise_context context, const xmlChar *name )
807 // Get the track from the stack
808 enum service_type track_type;
809 mlt_service track = context_pop_service( context, &track_type );
811 if ( track != NULL && track_type == mlt_entry_type )
813 mlt_properties track_props = MLT_SERVICE_PROPERTIES( track );
814 enum service_type parent_type = invalid_type;
815 mlt_service parent = context_pop_service( context, &parent_type );
816 mlt_multitrack multitrack = NULL;
818 mlt_producer producer = mlt_properties_get_data( track_props, "producer", NULL );
819 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
821 if ( parent_type == mlt_tractor_type )
822 multitrack = mlt_tractor_multitrack( MLT_TRACTOR( parent ) );
823 else if ( parent_type == mlt_multitrack_type )
824 multitrack = MLT_MULTITRACK( parent );
826 mlt_log_error( NULL, "[producer_xml] track contained in an invalid container\n" );
828 if ( multitrack != NULL )
830 // Set producer i/o if specified
831 if ( mlt_properties_get( track_props, "in" ) != NULL ||
832 mlt_properties_get( track_props, "out" ) != NULL )
834 mlt_position in = -1;
835 mlt_position out = -1;
836 if ( mlt_properties_get( track_props, "in" ) )
837 in = mlt_properties_get_position( track_props, "in" );
838 if ( mlt_properties_get( track_props, "out" ) )
839 out = mlt_properties_get_position( track_props, "out" );
840 mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ), in, out );
841 mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
842 mlt_properties_inherit( MLT_PRODUCER_PROPERTIES( cut ), track_props );
843 track_props = MLT_PRODUCER_PROPERTIES( cut );
844 mlt_producer_close( cut );
848 mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
851 // Set the hide state of the track producer
852 char *hide_s = mlt_properties_get( track_props, "hide" );
853 if ( hide_s != NULL )
855 if ( strcmp( hide_s, "video" ) == 0 )
856 mlt_properties_set_int( producer_props, "hide", 1 );
857 else if ( strcmp( hide_s, "audio" ) == 0 )
858 mlt_properties_set_int( producer_props, "hide", 2 );
859 else if ( strcmp( hide_s, "both" ) == 0 )
860 mlt_properties_set_int( producer_props, "hide", 3 );
864 if ( parent != NULL )
865 context_push_service( context, parent, parent_type );
867 mlt_service_close( track );
871 mlt_log_error( NULL, "[producer_xml] Invalid state at end of track\n" );
875 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
877 // use a dummy service to hold properties to allow arbitrary nesting
878 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
879 mlt_service_init( service, NULL );
881 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
883 context_push_service( context, service, mlt_dummy_filter_type );
885 // Set the properties
886 for ( ; atts != NULL && *atts != NULL; atts += 2 )
887 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
890 static void on_end_filter( deserialise_context context, const xmlChar *name )
892 enum service_type type;
893 mlt_service service = context_pop_service( context, &type );
894 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
896 enum service_type parent_type = invalid_type;
897 mlt_service parent = context_pop_service( context, &parent_type );
899 if ( service != NULL && type == mlt_dummy_filter_type )
901 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
902 mlt_service filter = MLT_SERVICE( mlt_factory_filter( context->profile, id, NULL ) );
903 mlt_properties filter_props = MLT_SERVICE_PROPERTIES( filter );
907 mlt_log_error( NULL, "[producer_xml] failed to load filter \"%s\"\n", id );
909 context_push_service( context, parent, parent_type );
910 mlt_service_close( service );
914 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
915 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( filter ), context->lc_numeric );
917 // Propogate the properties
918 qualify_property( context, properties, "resource" );
919 qualify_property( context, properties, "luma" );
920 qualify_property( context, properties, "luma.resource" );
921 qualify_property( context, properties, "composite.luma" );
922 qualify_property( context, properties, "producer.resource" );
923 mlt_properties_inherit( filter_props, properties );
925 // Attach all filters from service onto filter
926 attach_filters( filter, service );
928 // Associate the filter with the parent
929 if ( parent != NULL )
931 if ( parent_type == mlt_tractor_type )
933 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
934 mlt_field_plant_filter( field, MLT_FILTER( filter ), mlt_properties_get_int( properties, "track" ) );
935 mlt_filter_set_in_and_out( MLT_FILTER( filter ),
936 mlt_properties_get_int( properties, "in" ),
937 mlt_properties_get_int( properties, "out" ) );
941 mlt_service_attach( parent, MLT_FILTER( filter ) );
944 // Put the parent back on the stack
945 context_push_service( context, parent, parent_type );
949 mlt_log_error( NULL, "[producer_xml] filter closed with invalid parent...\n" );
952 // Close the dummy filter service
953 mlt_service_close( service );
957 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on filter close\n" );
961 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
963 // use a dummy service to hold properties to allow arbitrary nesting
964 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
965 mlt_service_init( service, NULL );
967 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
969 context_push_service( context, service, mlt_dummy_transition_type );
971 // Set the properties
972 for ( ; atts != NULL && *atts != NULL; atts += 2 )
973 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
976 static void on_end_transition( deserialise_context context, const xmlChar *name )
978 enum service_type type;
979 mlt_service service = context_pop_service( context, &type );
980 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
982 enum service_type parent_type = invalid_type;
983 mlt_service parent = context_pop_service( context, &parent_type );
985 if ( service != NULL && type == mlt_dummy_transition_type )
987 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
988 mlt_service effect = MLT_SERVICE( mlt_factory_transition( context->profile, id, NULL ) );
989 mlt_properties effect_props = MLT_SERVICE_PROPERTIES( effect );
993 mlt_log_error( NULL, "[producer_xml] failed to load transition \"%s\"\n", id );
995 context_push_service( context, parent, parent_type );
996 mlt_service_close( service );
999 track_service( context->destructors, effect, (mlt_destructor) mlt_transition_close );
1000 mlt_properties_set_lcnumeric( MLT_SERVICE_PROPERTIES( effect ), context->lc_numeric );
1002 // Propogate the properties
1003 qualify_property( context, properties, "resource" );
1004 qualify_property( context, properties, "luma" );
1005 qualify_property( context, properties, "luma.resource" );
1006 qualify_property( context, properties, "composite.luma" );
1007 qualify_property( context, properties, "producer.resource" );
1008 mlt_properties_inherit( effect_props, properties );
1010 // Attach all filters from service onto effect
1011 attach_filters( effect, service );
1013 // Associate the filter with the parent
1014 if ( parent != NULL )
1016 if ( parent_type == mlt_tractor_type )
1018 mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
1019 if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
1020 mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
1021 mlt_field_plant_transition( field, MLT_TRANSITION( effect ),
1022 mlt_properties_get_int( properties, "a_track" ),
1023 mlt_properties_get_int( properties, "b_track" ) );
1024 mlt_transition_set_in_and_out( MLT_TRANSITION( effect ),
1025 mlt_properties_get_int( properties, "in" ),
1026 mlt_properties_get_int( properties, "out" ) );
1030 mlt_log_warning( NULL, "[producer_xml] Misplaced transition - ignoring\n" );
1033 // Put the parent back on the stack
1034 context_push_service( context, parent, parent_type );
1038 mlt_log_error( NULL, "[producer_xml] transition closed with invalid parent...\n" );
1041 // Close the dummy filter service
1042 mlt_service_close( service );
1046 mlt_log_error( NULL, "[producer_xml] Invalid top of stack on transition close\n" );
1050 static void on_start_consumer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1052 if ( context->pass == 1 )
1054 mlt_consumer consumer = mlt_consumer_new( context->profile );
1055 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
1057 mlt_properties_set_lcnumeric( properties, context->lc_numeric );
1058 context_push_service( context, MLT_CONSUMER_SERVICE(consumer), mlt_dummy_consumer_type );
1060 // Set the properties from attributes
1061 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1062 mlt_properties_set( properties, (const char*) atts[0], (const char*) atts[1] );
1066 static void on_end_consumer( deserialise_context context, const xmlChar *name )
1068 if ( context->pass == 1 )
1070 // Get the consumer from the stack
1071 enum service_type type;
1072 mlt_service service = context_pop_service( context, &type );
1074 if ( service && type == mlt_dummy_consumer_type )
1076 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1077 qualify_property( context, properties, "resource" );
1078 qualify_property( context, properties, "target" );
1079 char *resource = trim( mlt_properties_get( properties, "resource" ) );
1081 if ( context->multi_consumer > 1 )
1083 // Instantiate the multi consumer
1084 if ( !context->consumer )
1086 context->consumer = mlt_factory_consumer( context->profile, "multi", NULL );
1087 if ( context->consumer )
1089 // Track this consumer
1090 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1091 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1094 if ( context->consumer )
1096 // Set this service instance on multi consumer
1098 snprintf( key, sizeof(key), "%d", context->consumer_count++ );
1099 mlt_properties_set_data( MLT_CONSUMER_PROPERTIES(context->consumer), key, service, 0,
1100 (mlt_destructor) mlt_service_close, NULL );
1105 // Instantiate the consumer
1106 char *id = trim( mlt_properties_get( properties, "mlt_service" ) );
1107 context->consumer = mlt_factory_consumer( context->profile, id, resource );
1108 if ( context->consumer )
1110 // Track this consumer
1111 track_service( context->destructors, MLT_CONSUMER_SERVICE(context->consumer), (mlt_destructor) mlt_consumer_close );
1112 mlt_properties_set_lcnumeric( MLT_CONSUMER_PROPERTIES(context->consumer), context->lc_numeric );
1114 // Inherit the properties
1115 mlt_properties_inherit( MLT_CONSUMER_PROPERTIES(context->consumer), properties );
1118 mlt_service_close( service );
1124 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
1126 enum service_type type;
1127 mlt_service service = context_pop_service( context, &type );
1128 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1129 const char *value = NULL;
1131 if ( service != NULL )
1133 // Set the properties
1134 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1136 if ( xmlStrcmp( atts[ 0 ], _x("name") ) == 0 )
1137 context->property = strdup( _s(atts[ 1 ]) );
1138 else if ( xmlStrcmp( atts[ 0 ], _x("value") ) == 0 )
1139 value = _s(atts[ 1 ]);
1142 if ( context->property != NULL )
1143 mlt_properties_set( properties, context->property, value == NULL ? "" : value );
1145 // Tell parser to collect any further nodes for serialisation
1146 context->is_value = 1;
1148 context_push_service( context, service, type );
1152 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'?\n", ( const char * )name );
1156 static void on_end_property( deserialise_context context, const xmlChar *name )
1158 enum service_type type;
1159 mlt_service service = context_pop_service( context, &type );
1160 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1162 if ( service != NULL )
1164 // Tell parser to stop building a tree
1165 context->is_value = 0;
1167 // See if there is a xml tree for the value
1168 if ( context->property != NULL && context->value_doc != NULL )
1173 // Serialise the tree to get value
1174 xmlDocDumpMemory( context->value_doc, &value, &size );
1175 mlt_properties_set( properties, context->property, _s(value) );
1177 xmlFreeFunc xmlFree = NULL;
1178 xmlMemGet( &xmlFree, NULL, NULL, NULL);
1181 xmlFreeDoc( context->value_doc );
1182 context->value_doc = NULL;
1185 // Close this property handling
1186 free( context->property );
1187 context->property = NULL;
1189 context_push_service( context, service, type );
1193 mlt_log_error( NULL, "[producer_xml] Property without a service '%s'??\n", (const char *)name );
1197 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
1199 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1200 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1202 if ( context->pass == 0 )
1204 if ( xmlStrcmp( name, _x("mlt") ) == 0 ||
1205 xmlStrcmp( name, _x("profile") ) == 0 ||
1206 xmlStrcmp( name, _x("profileinfo") ) == 0 )
1207 on_start_profile( context, name, atts );
1208 if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1209 context->multi_consumer++;
1212 context->branch[ context->depth ] ++;
1215 // Build a tree from nodes within a property value
1216 if ( context->is_value == 1 && context->pass == 1 )
1218 xmlNodePtr node = xmlNewNode( NULL, name );
1220 if ( context->value_doc == NULL )
1223 context->value_doc = xmlNewDoc( _x("1.0") );
1224 xmlDocSetRootElement( context->value_doc, node );
1228 // Append child to tree
1229 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
1231 context_push_node( context, node );
1233 // Set the attributes
1234 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1235 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
1237 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1238 on_start_tractor( context, name, atts );
1239 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1240 on_start_multitrack( context, name, atts );
1241 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1242 on_start_playlist( context, name, atts );
1243 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1244 on_start_producer( context, name, atts );
1245 else if ( xmlStrcmp( name, _x("blank") ) == 0 )
1246 on_start_blank( context, name, atts );
1247 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1248 on_start_entry( context, name, atts );
1249 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1250 on_start_track( context, name, atts );
1251 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1252 on_start_filter( context, name, atts );
1253 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1254 on_start_transition( context, name, atts );
1255 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1256 on_start_property( context, name, atts );
1257 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1258 on_start_consumer( context, name, atts );
1259 else if ( xmlStrcmp( name, _x("westley") ) == 0 || xmlStrcmp( name, _x("mlt") ) == 0 )
1261 for ( ; atts != NULL && *atts != NULL; atts += 2 )
1263 if ( xmlStrcmp( atts[0], _x("LC_NUMERIC") ) )
1264 mlt_properties_set( context->producer_map, _s( atts[0] ), _s(atts[1] ) );
1265 else if ( !context->lc_numeric )
1266 context->lc_numeric = strdup( _s( atts[1] ) );
1271 static void on_end_element( void *ctx, const xmlChar *name )
1273 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1274 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1276 if ( context->is_value == 1 && context->pass == 1 && xmlStrcmp( name, _x("property") ) != 0 )
1277 context_pop_node( context );
1278 else if ( xmlStrcmp( name, _x("multitrack") ) == 0 )
1279 on_end_multitrack( context, name );
1280 else if ( xmlStrcmp( name, _x("playlist") ) == 0 || xmlStrcmp( name, _x("seq") ) == 0 || xmlStrcmp( name, _x("smil") ) == 0 )
1281 on_end_playlist( context, name );
1282 else if ( xmlStrcmp( name, _x("track") ) == 0 )
1283 on_end_track( context, name );
1284 else if ( xmlStrcmp( name, _x("entry") ) == 0 )
1285 on_end_entry( context, name );
1286 else if ( xmlStrcmp( name, _x("tractor") ) == 0 )
1287 on_end_tractor( context, name );
1288 else if ( xmlStrcmp( name, _x("property") ) == 0 )
1289 on_end_property( context, name );
1290 else if ( xmlStrcmp( name, _x("producer") ) == 0 || xmlStrcmp( name, _x("video") ) == 0 )
1291 on_end_producer( context, name );
1292 else if ( xmlStrcmp( name, _x("filter") ) == 0 )
1293 on_end_filter( context, name );
1294 else if ( xmlStrcmp( name, _x("transition") ) == 0 )
1295 on_end_transition( context, name );
1296 else if ( xmlStrcmp( name, _x("consumer") ) == 0 )
1297 on_end_consumer( context, name );
1299 context->branch[ context->depth ] = 0;
1303 static void on_characters( void *ctx, const xmlChar *ch, int len )
1305 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1306 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1307 char *value = calloc( len + 1, 1 );
1308 enum service_type type;
1309 mlt_service service = context_pop_service( context, &type );
1310 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
1312 if ( service != NULL )
1313 context_push_service( context, service, type );
1316 strncpy( value, (const char*) ch, len );
1318 if ( context->stack_node_size > 0 )
1319 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1321 // libxml2 generates an on_characters immediately after a get_entity within
1322 // an element value, and we ignore it because it is called again during
1323 // actual substitution.
1324 else if ( context->property != NULL && context->entity_is_replace == 0 )
1326 char *s = mlt_properties_get( properties, context->property );
1329 // Append new text to existing content
1330 char *new = calloc( strlen( s ) + len + 1, 1 );
1332 strcat( new, value );
1333 mlt_properties_set( properties, context->property, new );
1337 mlt_properties_set( properties, context->property, value );
1339 context->entity_is_replace = 0;
1344 /** Convert parameters parsed from resource into entity declarations.
1346 static void params_to_entities( deserialise_context context )
1348 if ( context->params != NULL )
1352 // Add our params as entitiy declarations
1353 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1355 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1356 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1357 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, _s(name) ) );
1361 mlt_properties_close( context->params );
1362 context->params = NULL;
1366 // The following 3 facilitate entity substitution in the SAX parser
1367 static void on_internal_subset( void *ctx, const xmlChar* name,
1368 const xmlChar* publicId, const xmlChar* systemId )
1370 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1371 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1373 context->publicId = publicId;
1374 context->systemId = systemId;
1375 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1377 // Override default entities with our parameters
1378 params_to_entities( context );
1381 // TODO: Check this with Dan... I think this is for parameterisation
1382 // but it's breaking standard escaped entities (like < etc).
1383 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1384 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1386 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1387 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1389 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1392 // TODO: Check this functionality (see on_entity_declaration)
1393 static xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1395 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1396 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1397 xmlEntityPtr e = NULL;
1399 // Setup for entity declarations if not ready
1400 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1402 xmlCreateIntSubset( context->entity_doc, _x("mlt"), _x(""), _x("") );
1403 context->publicId = _x("");
1404 context->systemId = _x("");
1407 // Add our parameters if not already
1408 params_to_entities( context );
1410 e = xmlGetPredefinedEntity( name );
1412 // Send signal to on_characters that an entity substitutin is pending
1415 e = xmlGetDocEntity( context->entity_doc, name );
1417 context->entity_is_replace = 1;
1423 static void on_error( void * ctx, const char * msg, ... )
1425 struct _xmlError* err_ptr = xmlCtxtGetLastError( ctx );
1427 switch( err_ptr->level )
1429 case XML_ERR_WARNING:
1430 mlt_log_warning( NULL, "[producer_xml] parse warning: %s\trow: %d\tcol: %d\n",
1431 err_ptr->message, err_ptr->line, err_ptr->int2 );
1434 mlt_log_error( NULL, "[producer_xml] parse error: %s\trow: %d\tcol: %d\n",
1435 err_ptr->message, err_ptr->line, err_ptr->int2 );
1439 mlt_log_fatal( NULL, "[producer_xml] parse fatal: %s\trow: %d\tcol: %d\n",
1440 err_ptr->message, err_ptr->line, err_ptr->int2 );
1445 /** Convert a hexadecimal character to its value.
1447 static int tohex( char p )
1449 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1452 /** Decode a url-encoded string containing hexadecimal character sequences.
1454 static char *url_decode( char *dest, char *src )
1462 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1475 /** Extract the filename from a URL attaching parameters to a properties list.
1477 static void parse_url( mlt_properties properties, char *url )
1480 int n = strlen( url );
1484 for ( i = 0; i < n; i++ )
1501 if ( name != NULL && value != NULL )
1502 mlt_properties_set( properties, name, value );
1508 if ( name != NULL && value != NULL )
1509 mlt_properties_set( properties, name, value );
1512 // Quick workaround to avoid unecessary libxml2 warnings
1513 static int file_exists( char *file )
1515 char *name = strdup( file );
1517 if ( name != NULL && strchr( name, '?' ) )
1518 *( strchr( name, '?' ) ) = '\0';
1521 FILE *f = fopen( name, "r" );
1523 if ( exists ) fclose( f );
1529 mlt_producer producer_xml_init( mlt_profile profile, mlt_service_type servtype, const char *id, char *data )
1531 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1532 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1533 mlt_properties properties = NULL;
1535 struct _xmlParserCtxt *xmlcontext;
1536 int well_formed = 0;
1537 char *filename = NULL;
1538 int info = strcmp( id, "xml-string" ) ? 0 : 1;
1540 if ( data == NULL || !strcmp( data, "" ) || ( info == 0 && !file_exists( data ) ) )
1543 context = calloc( 1, sizeof( struct deserialise_context_s ) );
1544 if ( context == NULL )
1547 context->producer_map = mlt_properties_new();
1548 context->destructors = mlt_properties_new();
1549 context->params = mlt_properties_new();
1550 context->profile = profile;
1551 context->seekable = 1;
1553 // Decode URL and parse parameters
1554 mlt_properties_set( context->producer_map, "root", "" );
1557 filename = strdup( data );
1558 parse_url( context->params, url_decode( filename, data ) );
1560 // We need the directory prefix which was used for the xml
1561 if ( strchr( filename, '/' ) )
1564 mlt_properties_set( context->producer_map, "root", filename );
1565 root = mlt_properties_get( context->producer_map, "root" );
1566 *( strrchr( root, '/' ) ) = '\0';
1568 // If we don't have an absolute path here, we're heading for disaster...
1569 if ( root[ 0 ] != '/' )
1571 char *cwd = getcwd( NULL, 0 );
1572 char *real = malloc( strlen( cwd ) + strlen( root ) + 2 );
1573 sprintf( real, "%s/%s", cwd, root );
1574 mlt_properties_set( context->producer_map, "root", real );
1581 // We need to track the number of registered filters
1582 mlt_properties_set_int( context->destructors, "registered", 0 );
1584 // Setup SAX callbacks for first pass
1585 sax->startElement = on_start_element;
1586 sax->warning = on_error;
1587 sax->error = on_error;
1588 sax->fatalError = on_error;
1590 // Setup libxml2 SAX parsing
1592 xmlSubstituteEntitiesDefault( 1 );
1593 // This is used to facilitate entity substitution in the SAX parser
1594 context->entity_doc = xmlNewDoc( _x("1.0") );
1596 xmlcontext = xmlCreateFileParserCtxt( filename );
1598 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1600 // Invalid context - clean up and return NULL
1601 if ( xmlcontext == NULL )
1603 mlt_properties_close( context->producer_map );
1604 mlt_properties_close( context->destructors );
1605 mlt_properties_close( context->params );
1613 xmlcontext->sax = sax;
1614 xmlcontext->_private = ( void* )context;
1615 xmlParseDocument( xmlcontext );
1616 well_formed = xmlcontext->wellFormed;
1618 // Cleanup after parsing
1619 xmlcontext->sax = NULL;
1620 xmlcontext->_private = NULL;
1621 xmlFreeParserCtxt( xmlcontext );
1622 context->stack_node_size = 0;
1623 context->stack_service_size = 0;
1625 // Bad xml - clean up and return NULL
1628 mlt_properties_close( context->producer_map );
1629 mlt_properties_close( context->destructors );
1630 mlt_properties_close( context->params );
1631 xmlFreeDoc( context->entity_doc );
1638 // Setup the second pass
1641 xmlcontext = xmlCreateFileParserCtxt( filename );
1643 xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
1645 // Invalid context - clean up and return NULL
1646 if ( xmlcontext == NULL )
1648 mlt_properties_close( context->producer_map );
1649 mlt_properties_close( context->destructors );
1650 mlt_properties_close( context->params );
1651 xmlFreeDoc( context->entity_doc );
1658 // Setup SAX callbacks for second pass
1659 sax->endElement = on_end_element;
1660 sax->characters = on_characters;
1661 sax->cdataBlock = on_characters;
1662 sax->internalSubset = on_internal_subset;
1663 sax->entityDecl = on_entity_declaration;
1664 sax->getEntity = on_get_entity;
1667 xmlcontext->sax = sax;
1668 xmlcontext->_private = ( void* )context;
1669 xmlParseDocument( xmlcontext );
1670 well_formed = xmlcontext->wellFormed;
1672 // Cleanup after parsing
1673 xmlFreeDoc( context->entity_doc );
1675 xmlMemoryDump( ); // for debugging
1677 // Get the last producer on the stack
1678 enum service_type type;
1679 mlt_service service = context_pop_service( context, &type );
1680 if ( well_formed && service != NULL )
1682 // Verify it is a producer service (mlt_type="mlt_producer")
1683 // (producer, playlist, multitrack)
1684 char *type = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "mlt_type" );
1685 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1690 xmlDocPtr doc = xml_make_doc( service );
1691 xmlDocFormatDump( stdout, doc, 1 );
1696 if ( well_formed && service != NULL )
1698 char *title = mlt_properties_get( context->producer_map, "title" );
1700 // Need the complete producer list for various reasons
1701 properties = context->destructors;
1703 // Now make sure we don't have a reference to the service in the properties
1704 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1706 char *name = mlt_properties_get_name( properties, i );
1707 if ( mlt_properties_get_data_at( properties, i, NULL ) == service )
1709 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1714 // We are done referencing destructor property list
1715 // Set this var to service properties for convenience
1716 properties = MLT_SERVICE_PROPERTIES( service );
1719 mlt_properties_set( properties, "title", title );
1721 // Optimise for overlapping producers
1722 mlt_producer_optimise( MLT_PRODUCER( service ) );
1724 // Handle deep copies
1725 if ( getenv( "MLT_XML_DEEP" ) == NULL )
1727 // Now assign additional properties
1729 mlt_properties_set( properties, "resource", data );
1731 // This tells consumer_xml not to deep copy
1732 mlt_properties_set( properties, "xml", "was here" );
1736 // Allow the project to be edited
1737 mlt_properties_set( properties, "_xml", "was here" );
1738 mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
1741 // Make consumer available
1742 mlt_properties_inc_ref( MLT_CONSUMER_PROPERTIES( context->consumer ) );
1743 mlt_properties_set_data( properties, "consumer", context->consumer, 0,
1744 (mlt_destructor) mlt_consumer_close, NULL );
1746 mlt_properties_set_int( properties, "seekable", context->seekable );
1750 // Return null if not well formed
1755 mlt_properties_close( context->producer_map );
1756 if ( context->params != NULL )
1757 mlt_properties_close( context->params );
1758 mlt_properties_close( context->destructors );
1759 if ( context->lc_numeric )
1760 free( context->lc_numeric );
1764 return MLT_PRODUCER( service );