2 * producer_westley.c -- a libxml2 parser of mlt service networks
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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 "producer_westley.h"
25 #include <framework/mlt.h>
32 #include <libxml/parser.h>
33 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
34 #include <libxml/tree.h>
36 #define STACK_SIZE 1000
37 #define BRANCH_SIG_LEN 4000
41 extern xmlDocPtr
westley_make_doc( mlt_service service
);
58 mlt_dummy_filter_type
,
59 mlt_dummy_transition_type
,
60 mlt_dummy_producer_type
,
63 struct deserialise_context_s
65 enum service_type stack_types
[ STACK_SIZE
];
66 mlt_service stack_service
[ STACK_SIZE
];
67 int stack_service_size
;
68 mlt_properties producer_map
;
69 mlt_properties destructors
;
73 xmlNodePtr stack_node
[ STACK_SIZE
];
76 int entity_is_replace
;
78 int branch
[ STACK_SIZE
];
79 const xmlChar
*publicId
;
80 const xmlChar
*systemId
;
81 mlt_properties params
;
83 typedef struct deserialise_context_s
*deserialise_context
;
85 /** Convert the numerical current branch address to a dot-delimited string.
87 static char *serialise_branch( deserialise_context
this, char *s
)
92 for ( i
= 0; i
< this->depth
; i
++ )
94 int len
= strlen( s
);
95 snprintf( s
+ len
, BRANCH_SIG_LEN
- len
, "%d.", this->branch
[ i
] );
103 static int context_push_service( deserialise_context
this, mlt_service that
, enum service_type type
)
105 int ret
= this->stack_service_size
>= STACK_SIZE
- 1;
108 this->stack_service
[ this->stack_service_size
] = that
;
109 this->stack_types
[ this->stack_service_size
++ ] = type
;
111 // Record the tree branch on which this service lives
112 if ( that
!= NULL
&& mlt_properties_get( mlt_service_properties( that
), "_westley_branch" ) == NULL
)
114 char s
[ BRANCH_SIG_LEN
];
115 mlt_properties_set( mlt_service_properties( that
), "_westley_branch", serialise_branch( this, s
) );
124 static mlt_service
context_pop_service( deserialise_context
this, enum service_type
*type
)
126 mlt_service result
= NULL
;
127 if ( this->stack_service_size
> 0 )
129 result
= this->stack_service
[ -- this->stack_service_size
];
131 *type
= this->stack_types
[ this->stack_service_size
];
139 static int context_push_node( deserialise_context
this, xmlNodePtr node
)
141 int ret
= this->stack_node_size
>= STACK_SIZE
- 1;
143 this->stack_node
[ this->stack_node_size
++ ] = node
;
150 static xmlNodePtr
context_pop_node( deserialise_context
this )
152 xmlNodePtr result
= NULL
;
153 if ( this->stack_node_size
> 0 )
154 result
= this->stack_node
[ -- this->stack_node_size
];
159 // Set the destructor on a new service
160 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
162 int registered
= mlt_properties_get_int( properties
, "registered" );
163 char *key
= mlt_properties_get( properties
, "registered" );
164 mlt_properties_set_data( properties
, key
, service
, 0, destructor
, NULL
);
165 mlt_properties_set_int( properties
, "registered", ++ registered
);
169 // Prepend the property value with the document root
170 static inline void qualify_property( deserialise_context context
, mlt_properties properties
, char *name
)
172 char *resource
= mlt_properties_get( properties
, name
);
173 if ( resource
!= NULL
)
175 // Qualify file name properties
176 char *root
= mlt_properties_get( context
->producer_map
, "root" );
177 if ( root
!= NULL
&& strcmp( root
, "" ) )
179 char *full_resource
= malloc( strlen( root
) + strlen( resource
) + 2 );
180 if ( resource
[ 0 ] != '/' && strchr( resource
, ':' ) == NULL
)
182 strcpy( full_resource
, root
);
183 strcat( full_resource
, "/" );
184 strcat( full_resource
, resource
);
188 strcpy( full_resource
, resource
);
190 mlt_properties_set( properties
, name
, full_resource
);
191 free( full_resource
);
197 /** This function adds a producer to a playlist or multitrack when
198 there is no entry or track element.
201 static int add_producer( deserialise_context context
, mlt_service service
, mlt_position in
, mlt_position out
)
203 // Return value (0 = service remains top of stack, 1 means it can be removed)
206 // Get the parent producer
207 enum service_type type
;
208 mlt_service container
= context_pop_service( context
, &type
);
211 if ( service
!= NULL
&& container
!= NULL
)
213 char *container_branch
= mlt_properties_get( mlt_service_properties( container
), "_westley_branch" );
214 char *service_branch
= mlt_properties_get( mlt_service_properties( service
), "_westley_branch" );
215 contained
= !strncmp( container_branch
, service_branch
, strlen( container_branch
) );
220 mlt_properties properties
= mlt_service_properties( service
);
221 char *hide_s
= mlt_properties_get( properties
, "hide" );
223 // Indicate that this service is no longer top of stack
228 case mlt_tractor_type
:
230 mlt_multitrack multitrack
= mlt_tractor_multitrack( MLT_TRACTOR( container
) );
231 mlt_multitrack_connect( multitrack
, MLT_PRODUCER( service
), mlt_multitrack_count( multitrack
) );
234 case mlt_multitrack_type
:
236 mlt_multitrack_connect( MLT_MULTITRACK( container
),
237 MLT_PRODUCER( service
),
238 mlt_multitrack_count( MLT_MULTITRACK( container
) ) );
241 case mlt_playlist_type
:
243 mlt_playlist_append_io( MLT_PLAYLIST( container
), MLT_PRODUCER( service
), in
, out
);
248 fprintf( stderr
, "Producer defined inside something that isn't a container\n" );
252 // Set the hide state of the track producer
253 if ( hide_s
!= NULL
)
255 if ( strcmp( hide_s
, "video" ) == 0 )
256 mlt_properties_set_int( properties
, "hide", 1 );
257 else if ( strcmp( hide_s
, "audio" ) == 0 )
258 mlt_properties_set_int( properties
, "hide", 2 );
259 else if ( strcmp( hide_s
, "both" ) == 0 )
260 mlt_properties_set_int( properties
, "hide", 3 );
264 // Put the parent producer back
265 if ( container
!= NULL
)
266 context_push_service( context
, container
, type
);
271 /** Attach filters defined on that to this.
274 static void attach_filters( mlt_service
this, mlt_service that
)
279 mlt_filter filter
= NULL
;
280 for ( i
= 0; ( filter
= mlt_service_filter( that
, i
) ) != NULL
; i
++ )
282 mlt_service_attach( this, filter
);
283 attach_filters( mlt_filter_service( filter
), mlt_filter_service( filter
) );
288 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
290 mlt_service service
= mlt_tractor_service( mlt_tractor_new( ) );
291 mlt_properties properties
= mlt_service_properties( service
);
293 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
295 mlt_properties_set_position( properties
, "length", 0 );
297 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
298 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
300 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
302 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
303 mlt_properties_set_position( properties
, "length", length
);
306 if ( mlt_properties_get( properties
, "id" ) != NULL
)
307 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
309 context_push_service( context
, service
, mlt_tractor_type
);
312 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
315 enum service_type type
;
316 mlt_service tractor
= context_pop_service( context
, &type
);
318 if ( tractor
!= NULL
&& type
== mlt_tractor_type
)
320 // See if the tractor should be added to a playlist or multitrack
321 if ( add_producer( context
, tractor
, 0, mlt_producer_get_out( MLT_PRODUCER( tractor
) ) ) == 0 )
322 context_push_service( context
, tractor
, type
);
326 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
328 enum service_type type
;
329 mlt_service parent
= context_pop_service( context
, &type
);
331 // If we don't have a parent, then create one now, providing we're in a state where we can
332 if ( parent
== NULL
|| ( type
== mlt_playlist_type
|| type
== mlt_multitrack_type
) )
334 // Push the parent back
335 if ( parent
!= NULL
)
336 context_push_service( context
, parent
, type
);
338 // Create a tractor to contain the multitrack
339 parent
= mlt_tractor_service( mlt_tractor_new( ) );
340 track_service( context
->destructors
, parent
, (mlt_destructor
) mlt_tractor_close
);
341 type
= mlt_tractor_type
;
343 // Flag it as a synthesised tractor for clean up later
344 mlt_properties_set_int( mlt_service_properties( parent
), "fezzik_synth", 1 );
347 if ( type
== mlt_tractor_type
)
349 mlt_service service
= MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent
) ) );
350 mlt_properties properties
= mlt_service_properties( service
);
351 mlt_properties_set_position( properties
, "length", 0 );
352 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
353 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
355 if ( mlt_properties_get( properties
, "id" ) != NULL
)
356 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
,"id" ), service
, 0, NULL
, NULL
);
358 context_push_service( context
, parent
, type
);
359 context_push_service( context
, service
, mlt_multitrack_type
);
363 fprintf( stderr
, "Invalid multitrack position\n" );
367 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
369 // Get the multitrack from the stack
370 enum service_type type
;
371 mlt_service service
= context_pop_service( context
, &type
);
373 if ( service
== NULL
|| type
!= mlt_multitrack_type
)
374 fprintf( stderr
, "End multitrack in the wrong state...\n" );
377 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
379 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
380 mlt_properties properties
= mlt_service_properties( service
);
382 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
384 mlt_properties_set_position( properties
, "length", 0 );
386 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
388 mlt_properties_set( properties
, ( char* )atts
[0], ( char* )atts
[1] );
390 // Out will be overwritten later as we append, so we need to save it
391 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
392 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
395 if ( mlt_properties_get( properties
, "id" ) != NULL
)
396 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
398 context_push_service( context
, service
, mlt_playlist_type
);
401 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
403 // Get the playlist from the stack
404 enum service_type type
;
405 mlt_service service
= context_pop_service( context
, &type
);
407 if ( service
!= NULL
&& type
== mlt_playlist_type
)
409 mlt_properties properties
= mlt_service_properties( service
);
410 mlt_position in
= mlt_properties_get_position( properties
, "in" );
413 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
414 out
= mlt_properties_get_position( properties
, "_westley.out" );
416 out
= mlt_properties_get_position( properties
, "length" ) - 1;
418 if ( mlt_properties_get_position( properties
, "length" ) < out
)
419 mlt_properties_set_position( properties
, "length", out
+ 1 );
421 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
423 // See if the playlist should be added to a playlist or multitrack
424 if ( add_producer( context
, service
, in
, out
) == 0 )
425 context_push_service( context
, service
, type
);
429 fprintf( stderr
, "Invalid state of playlist end\n" );
433 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
435 // use a dummy service to hold properties to allow arbitrary nesting
436 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
437 mlt_service_init( service
, NULL
);
439 mlt_properties properties
= mlt_service_properties( service
);
441 context_push_service( context
, service
, mlt_dummy_producer_type
);
443 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
444 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
447 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
449 enum service_type type
;
450 mlt_service service
= context_pop_service( context
, &type
);
451 mlt_properties properties
= mlt_service_properties( service
);
453 if ( service
!= NULL
&& type
== mlt_dummy_producer_type
)
455 mlt_service producer
= NULL
;
457 qualify_property( context
, properties
, "resource" );
458 char *resource
= mlt_properties_get( properties
, "resource" );
460 // Let Kino-SMIL src be a synonym for resource
461 if ( resource
== NULL
)
463 qualify_property( context
, properties
, "src" );
464 resource
= mlt_properties_get( properties
, "src" );
467 // Instantiate the producer
468 if ( mlt_properties_get( properties
, "mlt_service" ) != NULL
)
471 strncpy( temp
, mlt_properties_get( properties
, "mlt_service" ), 1024 );
472 if ( resource
!= NULL
)
475 strncat( temp
, resource
, 1023 - strlen( temp
) );
477 producer
= MLT_SERVICE( mlt_factory_producer( "fezzik", temp
) );
480 // Just in case the plugin requested doesn't exist...
481 if ( producer
== NULL
&& resource
!= NULL
)
482 producer
= MLT_SERVICE( mlt_factory_producer( "fezzik", resource
) );
484 // Track this producer
485 track_service( context
->destructors
, producer
, (mlt_destructor
) mlt_producer_close
);
487 // Propogate the properties
488 qualify_property( context
, properties
, "resource" );
489 qualify_property( context
, properties
, "luma" );
490 qualify_property( context
, properties
, "luma.resource" );
491 qualify_property( context
, properties
, "composite.luma" );
492 qualify_property( context
, properties
, "producer.resource" );
494 // Handle in/out properties separately
495 mlt_position in
= -1;
496 mlt_position out
= -1;
499 if ( mlt_properties_get( properties
, "in" ) != NULL
)
500 in
= mlt_properties_get_position( properties
, "in" );
501 // Let Kino-SMIL clipBegin be a synonym for in
502 if ( mlt_properties_get( properties
, "clipBegin" ) != NULL
)
503 in
= mlt_properties_get_position( properties
, "clipBegin" );
505 if ( mlt_properties_get( properties
, "out" ) != NULL
)
506 out
= mlt_properties_get_position( properties
, "out" );
507 // Let Kino-SMIL clipEnd be a synonym for out
508 if ( mlt_properties_get( properties
, "clipEnd" ) != NULL
)
509 out
= mlt_properties_get_position( properties
, "clipEnd" );
512 mlt_properties_set( properties
, "in", NULL
);
513 mlt_properties_set( properties
, "out", NULL
);
515 // Inherit the properties
516 mlt_properties_inherit( mlt_service_properties( producer
), properties
);
518 // Attach all filters from service onto producer
519 attach_filters( producer
, service
);
521 // Add the producer to the producer map
522 if ( mlt_properties_get( properties
, "id" ) != NULL
)
523 mlt_properties_set_data( context
->producer_map
, mlt_properties_get(properties
, "id"), producer
, 0, NULL
, NULL
);
525 // See if the producer should be added to a playlist or multitrack
526 if ( add_producer( context
, producer
, in
, out
) == 0 )
528 // Otherwise, set in and out on...
529 if ( in
!= -1 || out
!= -1 )
531 // Get the parent service
532 enum service_type type
;
533 mlt_service parent
= context_pop_service( context
, &type
);
534 if ( parent
!= NULL
)
536 // Get the parent properties
537 properties
= mlt_service_properties( parent
);
539 char *resource
= mlt_properties_get( properties
, "resource" );
541 // Put the parent producer back
542 context_push_service( context
, parent
, type
);
544 // If the parent is a track or entry
545 if ( resource
&& ( strcmp( resource
, "<entry>" ) == 0 ) )
547 mlt_properties_set_position( properties
, "in", in
);
548 mlt_properties_set_position( properties
, "out", out
);
552 // Otherwise, set in and out on producer directly
553 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
558 // Otherwise, set in and out on producer directly
559 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
), in
, out
);
563 // Push the producer onto the stack
564 context_push_service( context
, producer
, mlt_producer_type
);
567 mlt_service_close( service
);
571 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
573 // Get the playlist from the stack
574 enum service_type type
;
575 mlt_service service
= context_pop_service( context
, &type
);
576 mlt_position length
= 0;
578 if ( type
== mlt_playlist_type
&& service
!= NULL
)
580 // Look for the length attribute
581 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
583 if ( strcmp( atts
[0], "length" ) == 0 )
585 length
= atoll( atts
[1] );
590 // Append a blank to the playlist
591 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
593 // Push the playlist back onto the stack
594 context_push_service( context
, service
, type
);
598 fprintf( stderr
, "blank without a playlist - a definite no no\n" );
602 static void on_start_entry( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
604 mlt_producer entry
= NULL
;
605 mlt_properties temp
= mlt_properties_new( );
607 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
609 mlt_properties_set( temp
, (char*) atts
[0], (char*) atts
[1] );
611 // Look for the producer attribute
612 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
614 mlt_producer producer
= mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
);
615 if ( producer
!= NULL
)
616 mlt_properties_set_data( temp
, "producer", producer
, 0, NULL
, NULL
);
620 // If we have a valid entry
621 if ( mlt_properties_get_data( temp
, "producer", NULL
) != NULL
)
623 mlt_playlist_clip_info info
;
624 enum service_type parent_type
;
625 mlt_service parent
= context_pop_service( context
, &parent_type
);
626 mlt_producer producer
= mlt_properties_get_data( temp
, "producer", NULL
);
628 if ( parent_type
== mlt_playlist_type
)
630 // Append the producer to the playlist
631 if ( mlt_properties_get( temp
, "in" ) != NULL
|| mlt_properties_get( temp
, "out" ) != NULL
)
633 mlt_playlist_append_io( MLT_PLAYLIST( parent
), producer
,
634 mlt_properties_get_position( temp
, "in" ),
635 mlt_properties_get_position( temp
, "out" ) );
639 mlt_playlist_append( MLT_PLAYLIST( parent
), producer
);
642 mlt_playlist_get_clip_info( MLT_PLAYLIST( parent
), &info
, mlt_playlist_count( MLT_PLAYLIST( parent
) ) - 1 );
643 entry
= info
.producer
;
647 fprintf( stderr
, "Entry not part of a playlist...\n" );
650 context_push_service( context
, parent
, parent_type
);
653 // Push the cut onto the stack
654 context_push_service( context
, mlt_producer_service( entry
), mlt_entry_type
);
656 mlt_properties_close( temp
);
659 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
661 // Get the entry from the stack
662 enum service_type entry_type
;
663 mlt_service entry
= context_pop_service( context
, &entry_type
);
665 if ( entry
== NULL
&& entry_type
!= mlt_entry_type
)
667 fprintf( stderr
, "Invalid state at end of entry\n" );
671 static void on_start_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
673 // use a dummy service to hold properties to allow arbitrary nesting
674 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
675 mlt_service_init( service
, NULL
);
677 // Push the dummy service onto the stack
678 context_push_service( context
, service
, mlt_entry_type
);
680 mlt_properties_set( mlt_service_properties( service
), "resource", "<track>" );
682 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
684 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
686 // Look for the producer attribute
687 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
689 mlt_producer producer
= mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
);
690 if ( producer
!= NULL
)
691 mlt_properties_set_data( mlt_service_properties( service
), "producer", producer
, 0, NULL
, NULL
);
696 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
698 // Get the track from the stack
699 enum service_type track_type
;
700 mlt_service track
= context_pop_service( context
, &track_type
);
702 if ( track
!= NULL
&& track_type
== mlt_entry_type
)
704 mlt_properties track_props
= mlt_service_properties( track
);
705 enum service_type parent_type
;
706 mlt_service parent
= context_pop_service( context
, &parent_type
);
707 mlt_multitrack multitrack
= NULL
;
709 mlt_producer producer
= mlt_properties_get_data( track_props
, "producer", NULL
);
710 mlt_properties producer_props
= mlt_producer_properties( producer
);
712 if ( parent_type
== mlt_tractor_type
)
713 multitrack
= mlt_tractor_multitrack( MLT_TRACTOR( parent
) );
714 else if ( parent_type
== mlt_multitrack_type
)
715 multitrack
= MLT_MULTITRACK( parent
);
717 fprintf( stderr
, "track contained in an invalid container\n" );
719 if ( multitrack
!= NULL
)
721 // Set the track on the multitrack
723 // Set producer i/o if specified
724 if ( mlt_properties_get( track_props
, "in" ) != NULL
&&
725 mlt_properties_get( track_props
, "out" ) != NULL
)
727 mlt_producer cut
= mlt_producer_cut( MLT_PRODUCER( producer
),
728 mlt_properties_get_position( track_props
, "in" ),
729 mlt_properties_get_position( track_props
, "out" ) );
730 mlt_properties_set_int( mlt_producer_properties( cut
), "cut", 1 );
731 mlt_multitrack_connect( multitrack
, cut
, mlt_multitrack_count( multitrack
) );
732 mlt_producer_close( cut
);
736 mlt_multitrack_connect( multitrack
, producer
, mlt_multitrack_count( multitrack
) );
739 // Set the hide state of the track producer
740 char *hide_s
= mlt_properties_get( track_props
, "hide" );
741 if ( hide_s
!= NULL
)
743 if ( strcmp( hide_s
, "video" ) == 0 )
744 mlt_properties_set_int( producer_props
, "hide", 1 );
745 else if ( strcmp( hide_s
, "audio" ) == 0 )
746 mlt_properties_set_int( producer_props
, "hide", 2 );
747 else if ( strcmp( hide_s
, "both" ) == 0 )
748 mlt_properties_set_int( producer_props
, "hide", 3 );
752 if ( parent
!= NULL
)
753 context_push_service( context
, parent
, parent_type
);
755 mlt_service_close( track
);
759 fprintf( stderr
, "Invalid state at end of track\n" );
763 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
765 // use a dummy service to hold properties to allow arbitrary nesting
766 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
767 mlt_service_init( service
, NULL
);
769 mlt_properties properties
= mlt_service_properties( service
);
771 context_push_service( context
, service
, mlt_dummy_filter_type
);
773 // Set the properties
774 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
775 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
778 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
780 enum service_type type
;
781 mlt_service service
= context_pop_service( context
, &type
);
782 mlt_properties properties
= mlt_service_properties( service
);
784 enum service_type parent_type
;
785 mlt_service parent
= context_pop_service( context
, &parent_type
);
787 if ( service
!= NULL
&& type
== mlt_dummy_filter_type
)
789 mlt_service filter
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
790 mlt_properties filter_props
= mlt_service_properties( filter
);
792 track_service( context
->destructors
, filter
, (mlt_destructor
) mlt_filter_close
);
794 // Propogate the properties
795 qualify_property( context
, properties
, "resource" );
796 qualify_property( context
, properties
, "luma" );
797 qualify_property( context
, properties
, "luma.resource" );
798 qualify_property( context
, properties
, "composite.luma" );
799 qualify_property( context
, properties
, "producer.resource" );
800 mlt_properties_inherit( filter_props
, properties
);
802 // Attach all filters from service onto filter
803 attach_filters( filter
, service
);
805 // Associate the filter with the parent
806 if ( parent
!= NULL
)
808 if ( parent_type
== mlt_tractor_type
)
810 mlt_field field
= mlt_tractor_field( MLT_TRACTOR( parent
) );
811 mlt_field_plant_filter( field
, MLT_FILTER( filter
), mlt_properties_get_int( properties
, "track" ) );
812 mlt_filter_set_in_and_out( MLT_FILTER( filter
),
813 mlt_properties_get_int( properties
, "in" ),
814 mlt_properties_get_int( properties
, "out" ) );
818 mlt_service_attach( parent
, MLT_FILTER( filter
) );
821 // Put the parent back on the stack
822 context_push_service( context
, parent
, parent_type
);
826 fprintf( stderr
, "filter closed with invalid parent...\n" );
829 // Close the dummy filter service
830 mlt_service_close( service
);
834 fprintf( stderr
, "Invalid top of stack on filter close\n" );
838 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
840 // use a dummy service to hold properties to allow arbitrary nesting
841 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
842 mlt_service_init( service
, NULL
);
844 mlt_properties properties
= mlt_service_properties( service
);
846 context_push_service( context
, service
, mlt_dummy_transition_type
);
848 // Set the properties
849 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
850 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
853 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
855 enum service_type type
;
856 mlt_service service
= context_pop_service( context
, &type
);
857 mlt_properties properties
= mlt_service_properties( service
);
859 enum service_type parent_type
;
860 mlt_service parent
= context_pop_service( context
, &parent_type
);
862 if ( service
!= NULL
&& type
== mlt_dummy_transition_type
)
864 mlt_service effect
= MLT_SERVICE( mlt_factory_transition(mlt_properties_get(properties
,"mlt_service"), NULL
) );
865 mlt_properties effect_props
= mlt_service_properties( effect
);
867 track_service( context
->destructors
, effect
, (mlt_destructor
) mlt_transition_close
);
869 // Propogate the properties
870 qualify_property( context
, properties
, "resource" );
871 qualify_property( context
, properties
, "luma" );
872 qualify_property( context
, properties
, "luma.resource" );
873 qualify_property( context
, properties
, "composite.luma" );
874 qualify_property( context
, properties
, "producer.resource" );
875 mlt_properties_inherit( effect_props
, properties
);
877 // Attach all filters from service onto effect
878 attach_filters( effect
, service
);
880 // Associate the filter with the parent
881 if ( parent
!= NULL
)
883 if ( parent_type
== mlt_tractor_type
)
885 mlt_field field
= mlt_tractor_field( MLT_TRACTOR( parent
) );
886 mlt_field_plant_transition( field
, MLT_TRANSITION( effect
),
887 mlt_properties_get_int( properties
, "a_track" ),
888 mlt_properties_get_int( properties
, "b_track" ) );
889 mlt_transition_set_in_and_out( MLT_TRANSITION( effect
),
890 mlt_properties_get_int( properties
, "in" ),
891 mlt_properties_get_int( properties
, "out" ) );
895 fprintf( stderr
, "Misplaced transition - ignoring\n" );
898 // Put the parent back on the stack
899 context_push_service( context
, parent
, parent_type
);
903 fprintf( stderr
, "transition closed with invalid parent...\n" );
906 // Close the dummy filter service
907 mlt_service_close( service
);
911 fprintf( stderr
, "Invalid top of stack on transition close\n" );
915 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
917 enum service_type type
;
918 mlt_service service
= context_pop_service( context
, &type
);
919 mlt_properties properties
= mlt_service_properties( service
);
922 if ( service
!= NULL
)
924 // Set the properties
925 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
927 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
928 context
->property
= strdup( atts
[ 1 ] );
929 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
930 value
= (char*) atts
[ 1 ];
933 if ( context
->property
!= NULL
&& value
!= NULL
)
934 mlt_properties_set( properties
, context
->property
, value
);
936 // Tell parser to collect any further nodes for serialisation
937 context
->is_value
= 1;
939 context_push_service( context
, service
, type
);
943 fprintf( stderr
, "Property without a service '%s'?\n", ( char * )name
);
947 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
949 enum service_type type
;
950 mlt_service service
= context_pop_service( context
, &type
);
951 mlt_properties properties
= mlt_service_properties( service
);
953 if ( service
!= NULL
)
955 // Tell parser to stop building a tree
956 context
->is_value
= 0;
958 // See if there is a xml tree for the value
959 if ( context
->property
!= NULL
&& context
->value_doc
!= NULL
)
964 // Serialise the tree to get value
965 xmlDocDumpMemory( context
->value_doc
, &value
, &size
);
966 mlt_properties_set( properties
, context
->property
, value
);
968 xmlFreeDoc( context
->value_doc
);
969 context
->value_doc
= NULL
;
972 // Close this property handling
973 free( context
->property
);
974 context
->property
= NULL
;
976 context_push_service( context
, service
, type
);
980 fprintf( stderr
, "Property without a service '%s'??\n", (char *)name
);
984 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
986 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
987 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
989 //printf("on_start_element: %s\n", name );
990 context
->branch
[ context
->depth
] ++;
993 // Build a tree from nodes within a property value
994 if ( context
->is_value
== 1 )
996 xmlNodePtr node
= xmlNewNode( NULL
, name
);
998 if ( context
->value_doc
== NULL
)
1001 context
->value_doc
= xmlNewDoc( "1.0" );
1002 xmlDocSetRootElement( context
->value_doc
, node
);
1006 // Append child to tree
1007 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
1009 context_push_node( context
, node
);
1011 // Set the attributes
1012 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
1013 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
1015 else if ( strcmp( name
, "tractor" ) == 0 )
1016 on_start_tractor( context
, name
, atts
);
1017 else if ( strcmp( name
, "multitrack" ) == 0 )
1018 on_start_multitrack( context
, name
, atts
);
1019 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
1020 on_start_playlist( context
, name
, atts
);
1021 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
1022 on_start_producer( context
, name
, atts
);
1023 else if ( strcmp( name
, "blank" ) == 0 )
1024 on_start_blank( context
, name
, atts
);
1025 else if ( strcmp( name
, "entry" ) == 0 )
1026 on_start_entry( context
, name
, atts
);
1027 else if ( strcmp( name
, "track" ) == 0 )
1028 on_start_track( context
, name
, atts
);
1029 else if ( strcmp( name
, "filter" ) == 0 )
1030 on_start_filter( context
, name
, atts
);
1031 else if ( strcmp( name
, "transition" ) == 0 )
1032 on_start_transition( context
, name
, atts
);
1033 else if ( strcmp( name
, "property" ) == 0 )
1034 on_start_property( context
, name
, atts
);
1035 else if ( strcmp( name
, "westley" ) == 0 )
1036 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
1037 mlt_properties_set( context
->producer_map
, ( char * )atts
[ 0 ], ( char * )atts
[ 1 ] );
1040 static void on_end_element( void *ctx
, const xmlChar
*name
)
1042 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1043 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1045 //printf("on_end_element: %s\n", name );
1046 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
1047 context_pop_node( context
);
1048 else if ( strcmp( name
, "multitrack" ) == 0 )
1049 on_end_multitrack( context
, name
);
1050 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
1051 on_end_playlist( context
, name
);
1052 else if ( strcmp( name
, "track" ) == 0 )
1053 on_end_track( context
, name
);
1054 else if ( strcmp( name
, "entry" ) == 0 )
1055 on_end_entry( context
, name
);
1056 else if ( strcmp( name
, "tractor" ) == 0 )
1057 on_end_tractor( context
, name
);
1058 else if ( strcmp( name
, "property" ) == 0 )
1059 on_end_property( context
, name
);
1060 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
1061 on_end_producer( context
, name
);
1062 else if ( strcmp( name
, "filter" ) == 0 )
1063 on_end_filter( context
, name
);
1064 else if ( strcmp( name
, "transition" ) == 0 )
1065 on_end_transition( context
, name
);
1067 context
->branch
[ context
->depth
] = 0;
1071 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
1073 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1074 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1075 char *value
= calloc( len
+ 1, 1 );
1076 enum service_type type
;
1077 mlt_service service
= context_pop_service( context
, &type
);
1078 mlt_properties properties
= mlt_service_properties( service
);
1080 if ( service
!= NULL
)
1081 context_push_service( context
, service
, type
);
1084 strncpy( value
, (const char*) ch
, len
);
1086 if ( context
->stack_node_size
> 0 )
1087 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
1089 // libxml2 generates an on_characters immediately after a get_entity within
1090 // an element value, and we ignore it because it is called again during
1091 // actual substitution.
1092 else if ( context
->property
!= NULL
&& context
->entity_is_replace
== 0 )
1094 char *s
= mlt_properties_get( properties
, context
->property
);
1097 // Append new text to existing content
1098 char *new = calloc( strlen( s
) + len
+ 1, 1 );
1100 strcat( new, value
);
1101 mlt_properties_set( properties
, context
->property
, new );
1105 mlt_properties_set( properties
, context
->property
, value
);
1107 context
->entity_is_replace
= 0;
1112 /** Convert parameters parsed from resource into entity declarations.
1114 static void params_to_entities( deserialise_context context
)
1116 if ( context
->params
!= NULL
)
1120 // Add our params as entitiy declarations
1121 for ( i
= 0; i
< mlt_properties_count( context
->params
); i
++ )
1123 xmlChar
*name
= ( xmlChar
* )mlt_properties_get_name( context
->params
, i
);
1124 xmlAddDocEntity( context
->entity_doc
, name
, XML_INTERNAL_GENERAL_ENTITY
,
1125 context
->publicId
, context
->systemId
, ( xmlChar
* )mlt_properties_get( context
->params
, name
) );
1129 mlt_properties_close( context
->params
);
1130 context
->params
= NULL
;
1134 // The following 3 facilitate entity substitution in the SAX parser
1135 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
1136 const xmlChar
* publicId
, const xmlChar
* systemId
)
1138 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1139 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1141 context
->publicId
= publicId
;
1142 context
->systemId
= systemId
;
1143 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
1145 // Override default entities with our parameters
1146 params_to_entities( context
);
1149 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
1150 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
1152 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1153 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1155 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1158 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1160 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1161 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1162 xmlEntityPtr e
= NULL
;
1164 // Setup for entity declarations if not ready
1165 if ( xmlGetIntSubset( context
->entity_doc
) == NULL
)
1167 xmlCreateIntSubset( context
->entity_doc
, "westley", "", "" );
1168 context
->publicId
= "";
1169 context
->systemId
= "";
1172 // Add our parameters if not already
1173 params_to_entities( context
);
1175 e
= xmlGetDocEntity( context
->entity_doc
, name
);
1177 // Send signal to on_characters that an entity substitutin is pending
1179 context
->entity_is_replace
= 1;
1184 /** Convert a hexadecimal character to its value.
1186 static int tohex( char p
)
1188 return isdigit( p
) ? p
- '0' : tolower( p
) - 'a' + 10;
1191 /** Decode a url-encoded string containing hexadecimal character sequences.
1193 static char *url_decode( char *dest
, char *src
)
1201 *p
++ = ( tohex( *( src
+ 1 ) ) << 4 ) | tohex( *( src
+ 2 ) );
1214 /** Extract the filename from a URL attaching parameters to a properties list.
1216 static void parse_url( mlt_properties properties
, char *url
)
1219 int n
= strlen( url
);
1223 for ( i
= 0; i
< n
; i
++ )
1240 if ( name
!= NULL
&& value
!= NULL
)
1241 mlt_properties_set( properties
, name
, value
);
1247 if ( name
!= NULL
&& value
!= NULL
)
1248 mlt_properties_set( properties
, name
, value
);
1251 mlt_producer
producer_westley_init( int info
, char *data
)
1255 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1256 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1257 mlt_properties properties
= NULL
;
1259 struct _xmlParserCtxt
*xmlcontext
;
1260 int well_formed
= 0;
1261 char *filename
= NULL
;
1263 context
->producer_map
= mlt_properties_new();
1264 context
->destructors
= mlt_properties_new();
1265 context
->params
= mlt_properties_new();
1267 // Decode URL and parse parameters
1268 mlt_properties_set( context
->producer_map
, "root", "" );
1271 filename
= strdup( data
);
1272 parse_url( context
->params
, url_decode( filename
, data
) );
1274 // We need the directory prefix which was used for the westley
1275 if ( strchr( filename
, '/' ) )
1278 mlt_properties_set( context
->producer_map
, "root", filename
);
1279 root
= mlt_properties_get( context
->producer_map
, "root" );
1280 *( strrchr( root
, '/' ) ) = '\0';
1282 // If we don't have an absolute path here, we're heading for disaster...
1283 if ( root
[ 0 ] != '/' )
1285 char *cwd
= getcwd( NULL
, 0 );
1286 char *real
= malloc( strlen( cwd
) + strlen( root
) + 2 );
1287 sprintf( real
, "%s/%s", cwd
, root
);
1288 mlt_properties_set( context
->producer_map
, "root", real
);
1295 // We need to track the number of registered filters
1296 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1298 // Setup SAX callbacks
1299 sax
->startElement
= on_start_element
;
1300 sax
->endElement
= on_end_element
;
1301 sax
->characters
= on_characters
;
1302 sax
->cdataBlock
= on_characters
;
1303 sax
->internalSubset
= on_internal_subset
;
1304 sax
->entityDecl
= on_entity_declaration
;
1305 sax
->getEntity
= on_get_entity
;
1307 // Setup libxml2 SAX parsing
1309 xmlSubstituteEntitiesDefault( 1 );
1310 // This is used to facilitate entity substitution in the SAX parser
1311 context
->entity_doc
= xmlNewDoc( "1.0" );
1313 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1315 xmlcontext
= xmlCreateMemoryParserCtxt( data
, strlen( data
) );
1317 xmlcontext
->sax
= sax
;
1318 xmlcontext
->_private
= ( void* )context
;
1321 xmlParseDocument( xmlcontext
);
1322 well_formed
= xmlcontext
->wellFormed
;
1324 // Cleanup after parsing
1325 xmlFreeDoc( context
->entity_doc
);
1327 xmlcontext
->sax
= NULL
;
1328 xmlcontext
->_private
= NULL
;
1329 xmlFreeParserCtxt( xmlcontext
);
1330 xmlMemoryDump( ); // for debugging
1332 // Get the last producer on the stack
1333 enum service_type type
;
1334 mlt_service service
= context_pop_service( context
, &type
);
1335 if ( well_formed
&& service
!= NULL
)
1337 // Verify it is a producer service (mlt_type="mlt_producer")
1338 // (producer, playlist, multitrack)
1339 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1340 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1345 xmlDocPtr doc
= westley_make_doc( service
);
1346 xmlDocFormatDump( stdout
, doc
, 1 );
1351 if ( well_formed
&& service
!= NULL
)
1353 char *title
= mlt_properties_get( context
->producer_map
, "title" );
1355 // Need the complete producer list for various reasons
1356 properties
= context
->destructors
;
1358 // Now make sure we don't have a reference to the service in the properties
1359 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1361 char *name
= mlt_properties_get_name( properties
, i
);
1362 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1364 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1369 // We are done referencing destructor property list
1370 // Set this var to service properties for convenience
1371 properties
= mlt_service_properties( service
);
1373 // make the returned service destroy the connected services
1374 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1377 mlt_properties_set( properties
, "title", title
);
1379 // Handle deep copies
1380 if ( getenv( "MLT_WESTLEY_DEEP" ) == NULL
)
1382 // Now assign additional properties
1384 mlt_properties_set( properties
, "resource", data
);
1386 // This tells consumer_westley not to deep copy
1387 mlt_properties_set( properties
, "westley", "was here" );
1391 // Allow the project to be edited
1392 mlt_properties_set_int( properties
, "_mlt_service_hidden", 1 );
1397 // Return null if not well formed
1401 mlt_properties_close( context
->destructors
);
1405 mlt_properties_close( context
->producer_map
);
1406 if ( context
->params
!= NULL
)
1407 mlt_properties_close( context
->params
);
1411 return MLT_PRODUCER( service
);