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>
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
40 extern xmlDocPtr
westley_make_doc( mlt_service service
);
43 struct deserialise_context_s
45 mlt_service stack_service
[ STACK_SIZE
];
46 int stack_service_size
;
47 mlt_properties producer_map
;
48 mlt_properties destructors
;
50 mlt_properties producer_properties
;
53 xmlNodePtr stack_node
[ STACK_SIZE
];
57 int branch
[ STACK_SIZE
];
58 const xmlChar
*publicId
;
59 const xmlChar
*systemId
;
60 mlt_properties params
;
62 typedef struct deserialise_context_s
*deserialise_context
;
64 /** Convert the numerical current branch address to a dot-delimited string.
66 static char *serialise_branch( deserialise_context
this, char *s
)
71 for ( i
= 0; i
< this->depth
; i
++ )
73 int len
= strlen( s
);
74 snprintf( s
+ len
, BRANCH_SIG_LEN
- len
, "%d.", this->branch
[ i
] );
82 static int context_push_service( deserialise_context
this, mlt_service that
)
84 int ret
= this->stack_service_size
>= STACK_SIZE
- 1;
87 this->stack_service
[ this->stack_service_size
++ ] = that
;
89 // Record the tree branch on which this service lives
90 if ( mlt_properties_get( mlt_service_properties( that
), "_westley_branch" ) == NULL
)
92 char s
[ BRANCH_SIG_LEN
];
93 mlt_properties_set( mlt_service_properties( that
), "_westley_branch", serialise_branch( this, s
) );
102 static mlt_service
context_pop_service( deserialise_context
this )
104 mlt_service result
= NULL
;
105 if ( this->stack_service_size
> 0 )
106 result
= this->stack_service
[ -- this->stack_service_size
];
113 static int context_push_node( deserialise_context
this, xmlNodePtr node
)
115 int ret
= this->stack_node_size
>= STACK_SIZE
- 1;
117 this->stack_node
[ this->stack_node_size
++ ] = node
;
124 static xmlNodePtr
context_pop_node( deserialise_context
this )
126 xmlNodePtr result
= NULL
;
127 if ( this->stack_node_size
> 0 )
128 result
= this->stack_node
[ -- this->stack_node_size
];
133 // Set the destructor on a new service
134 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
136 int registered
= mlt_properties_get_int( properties
, "registered" );
137 char *key
= mlt_properties_get( properties
, "registered" );
138 mlt_properties_set_data( properties
, key
, service
, 0, destructor
, NULL
);
139 mlt_properties_set_int( properties
, "registered", ++ registered
);
143 // Forward declarations
144 static void on_end_track( deserialise_context context
, const xmlChar
*name
);
145 static void on_end_entry( deserialise_context context
, const xmlChar
*name
);
147 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
149 mlt_service service
= mlt_tractor_service( mlt_tractor_init() );
150 mlt_properties properties
= mlt_service_properties( service
);
152 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
154 mlt_properties_set_position( properties
, "length", 0 );
156 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
157 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
159 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
161 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
162 mlt_properties_set_position( properties
, "length", length
);
165 if ( mlt_properties_get( properties
, "id" ) != NULL
)
166 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
168 context_push_service( context
, service
);
171 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
173 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
174 mlt_properties properties
= mlt_service_properties( service
);
176 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
178 mlt_properties_set_position( properties
, "length", 0 );
180 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
181 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
183 if ( mlt_properties_get( properties
, "id" ) != NULL
)
184 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
186 context_push_service( context
, service
);
189 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
191 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
192 mlt_properties properties
= mlt_service_properties( service
);
194 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
196 mlt_properties_set_position( properties
, "length", 0 );
198 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
200 mlt_properties_set( properties
, ( char* )atts
[0], ( char* )atts
[1] );
202 // Out will be overwritten later as we append, so we need to save it
203 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
204 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
207 if ( mlt_properties_get( properties
, "id" ) != NULL
)
208 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
210 context_push_service( context
, service
);
213 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
215 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
217 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
219 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
223 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
225 // Get the playlist from the stack
226 mlt_service service
= context_pop_service( context
);
227 mlt_position length
= 0;
229 if ( service
== NULL
)
232 // Look for the length attribute
233 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
235 if ( strcmp( atts
[0], "length" ) == 0 )
237 length
= atoll( atts
[1] );
242 // Append a blank to the playlist
243 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
245 // Push the playlist back onto the stack
246 context_push_service( context
, service
);
249 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
251 // Use a dummy service to hold properties to allow arbitrary nesting
252 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
253 mlt_service_init( service
, NULL
);
255 // Push the dummy service onto the stack
256 context_push_service( context
, service
);
258 if ( strcmp( name
, "entry" ) == 0 )
259 mlt_properties_set( mlt_service_properties( service
), "resource", "<entry>" );
261 mlt_properties_set( mlt_service_properties( service
), "resource", "<track>" );
263 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
265 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
267 // Look for the producer attribute
268 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
270 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
271 // Push the referenced producer onto the stack
272 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
274 // Remove the dummy service to cause end element failure
275 context_pop_service( context
);
280 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
282 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
284 // Set the properties
285 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
286 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
289 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
291 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
293 // Set the properties
294 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
295 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
298 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
300 mlt_properties properties
= context
->producer_properties
;
303 if ( properties
== NULL
)
306 // Set the properties
307 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
309 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
311 context
->property
= strdup( atts
[ 1 ] );
313 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
315 value
= (char*) atts
[ 1 ];
319 if ( context
->property
!= NULL
&& value
!= NULL
)
320 mlt_properties_set( properties
, context
->property
, value
);
322 // Tell parser to collect any further nodes for serialisation
323 context
->is_value
= 1;
327 /** This function adds a producer to a playlist or multitrack when
328 there is no entry or track element.
331 static int add_producer( deserialise_context context
, mlt_service service
, mlt_position in
, mlt_position out
)
333 // Get the parent producer
334 mlt_service producer
= context_pop_service( context
);
336 if ( producer
!= NULL
)
338 char current_branch
[ BRANCH_SIG_LEN
];
339 char *service_branch
= mlt_properties_get( mlt_service_properties( producer
), "_westley_branch" );
341 // Validate the producer from the stack is an ancestor and not predecessor
342 serialise_branch( context
, current_branch
);
343 if ( service_branch
!= NULL
&& strncmp( service_branch
, current_branch
, strlen( service_branch
) ) == 0 )
345 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
347 // Put the parent producer back
348 context_push_service( context
, producer
);
350 // If the parent producer is a multitrack or playlist (not a track or entry)
351 if ( resource
&& ( strcmp( resource
, "<playlist>" ) == 0 ||
352 strcmp( resource
, "<multitrack>" ) == 0 ) )
354 //printf( "add_producer: current branch %s service branch %s (%d)\n", current_branch, service_branch, strncmp( service_branch, current_branch, strlen( service_branch ) ) );
355 if ( strcmp( resource
, "<playlist>" ) == 0 )
357 // Append this producer to the playlist
358 mlt_playlist_append_io( MLT_PLAYLIST( producer
),
359 MLT_PRODUCER( service
), in
, out
);
363 mlt_properties properties
= mlt_service_properties( service
);
365 // Set this producer on the multitrack
366 mlt_multitrack_connect( MLT_MULTITRACK( producer
),
367 MLT_PRODUCER( service
),
368 mlt_multitrack_count( MLT_MULTITRACK( producer
) ) );
370 // Set the hide state of the track producer
371 char *hide_s
= mlt_properties_get( properties
, "hide" );
372 if ( hide_s
!= NULL
)
374 if ( strcmp( hide_s
, "video" ) == 0 )
375 mlt_properties_set_int( properties
, "hide", 1 );
376 else if ( strcmp( hide_s
, "audio" ) == 0 )
377 mlt_properties_set_int( properties
, "hide", 2 );
378 else if ( strcmp( hide_s
, "both" ) == 0 )
379 mlt_properties_set_int( properties
, "hide", 3 );
383 // Normally, the enclosing entry or track will pop this service off
384 // In its absence we do not push it on.
392 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
394 // Get the multitrack from the stack
395 mlt_service producer
= context_pop_service( context
);
396 if ( producer
== NULL
)
399 // Get the tractor from the stack
400 mlt_service service
= context_pop_service( context
);
402 // Create a tractor if one does not exist
403 char *resource
= NULL
;
404 if ( service
!= NULL
)
405 resource
= mlt_properties_get( mlt_service_properties( service
), "resource" );
406 if ( service
== NULL
|| resource
== NULL
|| strcmp( resource
, "<tractor>" ) )
408 //printf("creating a tractor\n");
409 char current_branch
[ BRANCH_SIG_LEN
];
411 // Put the anonymous service back onto the stack!
412 if ( service
!= NULL
)
413 context_push_service( context
, service
);
415 // Fabricate the tractor
416 service
= mlt_tractor_service( mlt_tractor_init() );
417 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
419 // Inherit the producer's properties
420 mlt_properties properties
= mlt_service_properties( service
);
421 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( MLT_PRODUCER( producer
) ) + 1 );
422 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
423 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer
) ) );
425 mlt_properties_set( properties
, "_westley_branch", serialise_branch( context
, current_branch
) );
428 // Connect the tractor to the multitrack
429 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
430 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
431 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
433 // See if the tractor should be added to a playlist or multitrack
434 add_producer( context
, service
, 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
436 // Always push the multitrack back onto the stack for filters and transitions
437 context_push_service( context
, producer
);
439 // Always push the tractor back onto the stack for filters and transitions
440 context_push_service( context
, service
);
443 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
445 // Get the playlist from the stack
446 mlt_service producer
= context_pop_service( context
);
447 if ( producer
== NULL
)
449 mlt_properties properties
= mlt_service_properties( producer
);
451 mlt_position in
= mlt_properties_get_position( properties
, "in" );
454 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
455 out
= mlt_properties_get_position( properties
, "_westley.out" );
457 out
= mlt_properties_get_position( properties
, "length" ) - 1;
459 if ( mlt_properties_get_position( properties
, "length" ) < out
)
460 mlt_properties_set_position( properties
, "length", out
+ 1 );
462 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
), in
, out
);
464 // See if the playlist should be added to a playlist or multitrack
465 if ( add_producer( context
, producer
, in
, out
) == 0 )
467 // Otherwise, push the playlist back onto the stack
468 context_push_service( context
, producer
);
471 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
473 // Get the producer from the stack
474 mlt_service producer
= context_pop_service( context
);
475 if ( producer
== NULL
)
477 mlt_properties producer_props
= mlt_service_properties( producer
);
479 // See if the producer is a tractor
480 char *resource
= mlt_properties_get( producer_props
, "resource" );
481 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
482 // If so chomp its producer
483 context_pop_service( context
);
485 // Get the dummy track service from the stack
486 mlt_service track
= context_pop_service( context
);
487 if ( track
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( track
), "resource" ), "<track>" ) )
489 context_push_service( context
, producer
);
492 mlt_properties track_props
= mlt_service_properties( track
);
494 // Get the multitrack from the stack
495 mlt_service service
= context_pop_service( context
);
496 if ( service
== NULL
)
498 context_push_service( context
, producer
);
502 // Set the track on the multitrack
503 mlt_multitrack_connect( MLT_MULTITRACK( service
),
504 MLT_PRODUCER( producer
),
505 mlt_multitrack_count( MLT_MULTITRACK( service
) ) );
507 // Set producer i/o if specified
508 if ( mlt_properties_get( track_props
, "in" ) != NULL
||
509 mlt_properties_get( track_props
, "out" ) != NULL
)
511 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
),
512 mlt_properties_get_position( track_props
, "in" ),
513 mlt_properties_get_position( track_props
, "out" ) );
516 // Set the hide state of the track producer
517 char *hide_s
= mlt_properties_get( track_props
, "hide" );
518 if ( hide_s
!= NULL
)
520 if ( strcmp( hide_s
, "video" ) == 0 )
521 mlt_properties_set_int( producer_props
, "hide", 1 );
522 else if ( strcmp( hide_s
, "audio" ) == 0 )
523 mlt_properties_set_int( producer_props
, "hide", 2 );
524 else if ( strcmp( hide_s
, "both" ) == 0 )
525 mlt_properties_set_int( producer_props
, "hide", 3 );
528 // Push the multitrack back onto the stack
529 context_push_service( context
, service
);
531 mlt_service_close( track
);
534 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
536 // Get the producer from the stack
537 mlt_service producer
= context_pop_service( context
);
538 if ( producer
== NULL
)
541 // See if the producer is a tractor
542 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
543 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
544 // If so chomp its producer
545 context_pop_service( context
);
547 // Get the dummy entry service from the stack
548 mlt_service entry
= context_pop_service( context
);
549 if ( entry
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( entry
), "resource" ), "<entry>" ) )
551 context_push_service( context
, producer
);
555 // Get the playlist from the stack
556 mlt_service service
= context_pop_service( context
);
557 if ( service
== NULL
)
559 context_push_service( context
, producer
);
563 // Append the producer to the playlist
564 if ( mlt_properties_get( mlt_service_properties( entry
), "in" ) != NULL
||
565 mlt_properties_get( mlt_service_properties( entry
), "out" ) != NULL
)
567 mlt_playlist_append_io( MLT_PLAYLIST( service
),
568 MLT_PRODUCER( producer
),
569 mlt_properties_get_position( mlt_service_properties( entry
), "in" ),
570 mlt_properties_get_position( mlt_service_properties( entry
), "out" ) );
574 mlt_playlist_append( MLT_PLAYLIST( service
), MLT_PRODUCER( producer
) );
577 // Push the playlist back onto the stack
578 context_push_service( context
, service
);
580 mlt_service_close( entry
);
583 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
586 mlt_service tractor
= context_pop_service( context
);
587 if ( tractor
== NULL
)
590 // Get the tractor's multitrack
591 mlt_producer multitrack
= mlt_properties_get_data( mlt_service_properties( tractor
), "multitrack", NULL
);
592 if ( multitrack
!= NULL
)
594 // Inherit the producer's properties
595 mlt_properties properties
= mlt_producer_properties( MLT_PRODUCER( tractor
) );
596 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
597 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
598 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
601 // See if the tractor should be added to a playlist or multitrack
602 if ( add_producer( context
, tractor
, 0, mlt_producer_get_out( MLT_PRODUCER( tractor
) ) ) == 0 )
604 // Otherwise, push the tractor back onto the stack
605 context_push_service( context
, tractor
);
608 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
610 // Tell parser to stop building a tree
611 context
->is_value
= 0;
613 // See if there is a xml tree for the value
614 if ( context
->property
!= NULL
&& context
->value_doc
!= NULL
)
619 // Serialise the tree to get value
620 xmlDocDumpMemory( context
->value_doc
, &value
, &size
);
621 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
623 xmlFreeDoc( context
->value_doc
);
624 context
->value_doc
= NULL
;
627 // Close this property handling
628 free( context
->property
);
629 context
->property
= NULL
;
632 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
634 mlt_properties properties
= context
->producer_properties
;
635 mlt_service service
= NULL
;
637 if ( properties
== NULL
)
640 char *resource
= mlt_properties_get( properties
, "resource" );
641 // Let Kino-SMIL src be a synonym for resource
642 if ( resource
== NULL
)
643 resource
= mlt_properties_get( properties
, "src" );
645 // Instantiate the producer
646 if ( mlt_properties_get( properties
, "mlt_service" ) != NULL
)
649 strncpy( temp
, mlt_properties_get( properties
, "mlt_service" ), 1024 );
650 if ( resource
!= NULL
)
653 strncat( temp
, resource
, 1023 - strlen( temp
) );
655 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", temp
) );
657 if ( service
== NULL
&& resource
!= NULL
)
659 char *root
= mlt_properties_get( context
->producer_map
, "_root" );
660 char *full_resource
= malloc( strlen( root
) + strlen( resource
) + 1 );
661 if ( resource
[ 0 ] != '/' )
663 strcpy( full_resource
, root
);
664 strcat( full_resource
, resource
);
668 strcpy( full_resource
, resource
);
670 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource
) );
671 free( full_resource
);
673 if ( service
== NULL
)
675 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
677 // Add the producer to the producer map
678 if ( mlt_properties_get( properties
, "id" ) != NULL
)
679 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
681 // Handle in/out properties separately
682 mlt_position in
= -1;
683 mlt_position out
= -1;
686 if ( mlt_properties_get( properties
, "in" ) != NULL
)
687 in
= mlt_properties_get_position( properties
, "in" );
688 // Let Kino-SMIL clipBegin be a synonym for in
689 if ( mlt_properties_get( properties
, "clipBegin" ) != NULL
)
690 in
= mlt_properties_get_position( properties
, "clipBegin" );
692 if ( mlt_properties_get( properties
, "out" ) != NULL
)
693 out
= mlt_properties_get_position( properties
, "out" );
694 // Let Kino-SMIL clipEnd be a synonym for out
695 if ( mlt_properties_get( properties
, "clipEnd" ) != NULL
)
696 out
= mlt_properties_get_position( properties
, "clipEnd" );
699 mlt_properties_set( properties
, "in", NULL
);
700 mlt_properties_set( properties
, "out", NULL
);
702 mlt_properties_inherit( mlt_service_properties( service
), properties
);
703 mlt_properties_close( properties
);
704 context
->producer_properties
= NULL
;
706 // See if the producer should be added to a playlist or multitrack
707 if ( add_producer( context
, service
, in
, out
) == 0 )
709 // Otherwise, set in and out on...
710 if ( in
!= -1 || out
!= -1 )
712 // Get the parent service
713 mlt_service parent
= context_pop_service( context
);
714 if ( parent
!= NULL
)
716 // Get the parent properties
717 properties
= mlt_service_properties( parent
);
719 char *resource
= mlt_properties_get( properties
, "resource" );
721 // Put the parent producer back
722 context_push_service( context
, parent
);
724 // If the parent is a track or entry
725 if ( resource
&& ( strcmp( resource
, "<entry>" ) == 0 ) )
727 mlt_properties_set_position( properties
, "in", in
);
728 mlt_properties_set_position( properties
, "out", out
);
732 // Otherwise, set in and out on producer directly
733 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
738 // Otherwise, set in and out on producer directly
739 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
743 // Push the producer onto the stack
744 context_push_service( context
, service
);
748 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
750 mlt_properties properties
= context
->producer_properties
;
751 if ( properties
== NULL
)
757 mlt_service tractor
= NULL
;
759 // Get the producer from the stack
760 mlt_service producer
= context_pop_service( context
);
761 if ( producer
== NULL
)
764 // See if the producer is a tractor
765 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
766 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
768 // If so, then get the next producer
770 producer
= context_pop_service( context
);
773 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
776 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
777 if ( service
== NULL
)
779 context_push_service( context
, producer
);
782 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
784 // Connect the filter to the producer
785 mlt_filter_connect( MLT_FILTER( service
), producer
,
786 mlt_properties_get_int( properties
, "track" ) );
788 // Set in and out from producer if non existant
789 if ( mlt_properties_get( properties
, "in" ) == NULL
)
790 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
791 if ( mlt_properties_get( properties
, "out" ) == NULL
)
792 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
794 // Propogate the properties
795 mlt_properties_inherit( mlt_service_properties( service
), properties
);
796 mlt_properties_close( properties
);
797 context
->producer_properties
= NULL
;
798 properties
= mlt_service_properties( service
);
800 // Set in and out again due to inheritance
801 mlt_filter_set_in_and_out( MLT_FILTER( service
),
802 mlt_properties_get_position( properties
, "in" ),
803 mlt_properties_get_position( properties
, "out" ) );
805 // If a producer alias is in the producer_map, get it
806 snprintf( key
, 10, "%p", producer
);
807 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
808 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
810 // Put the producer in the producer map
811 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
813 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
815 // For filter chain support, add an alias to the producer map
816 snprintf( key
, 10, "%p", service
);
817 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
819 // Push the filter onto the stack
820 context_push_service( context
, service
);
822 if ( tractor
!= NULL
)
824 // Connect the tractor to the filter
825 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
827 // Push the tractor back onto the stack
828 context_push_service( context
, tractor
);
832 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
834 mlt_service tractor
= NULL
;
835 mlt_properties properties
= context
->producer_properties
;
836 if ( properties
== NULL
)
839 // Get the producer from the stack
840 mlt_service producer
= context_pop_service( context
);
841 if ( producer
== NULL
)
844 // See if the producer is a tractor
845 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
846 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
848 // If so, then get the next producer
850 producer
= context_pop_service( context
);
853 // Create the transition
854 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
855 if ( service
== NULL
)
857 context_push_service( context
, producer
);
860 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
862 // Propogate the properties
863 mlt_properties_inherit( mlt_service_properties( service
), properties
);
864 mlt_properties_close( properties
);
865 context
->producer_properties
= NULL
;
866 properties
= mlt_service_properties( service
);
868 // Set in and out again due to inheritance
869 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
870 mlt_properties_get_position( properties
, "in" ),
871 mlt_properties_get_position( properties
, "out" ) );
873 // Connect the transition to the producer
874 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
875 mlt_properties_get_int( properties
, "a_track" ),
876 mlt_properties_get_int( properties
, "b_track" ) );
878 // Push the transition onto the stack
879 context_push_service( context
, service
);
881 if ( tractor
!= NULL
)
883 // Connect the tractor to the transition
884 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
886 // Push the tractor back onto the stack
887 context_push_service( context
, tractor
);
891 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
893 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
894 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
896 //printf("on_start_element: %s\n", name );
897 context
->branch
[ context
->depth
] ++;
900 // Build a tree from nodes within a property value
901 if ( context
->is_value
== 1 )
903 xmlNodePtr node
= xmlNewNode( NULL
, name
);
905 if ( context
->value_doc
== NULL
)
908 context
->value_doc
= xmlNewDoc( "1.0" );
909 xmlDocSetRootElement( context
->value_doc
, node
);
913 // Append child to tree
914 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
916 context_push_node( context
, node
);
918 // Set the attributes
919 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
920 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
922 else if ( strcmp( name
, "tractor" ) == 0 )
923 on_start_tractor( context
, name
, atts
);
924 else if ( strcmp( name
, "multitrack" ) == 0 )
925 on_start_multitrack( context
, name
, atts
);
926 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
927 on_start_playlist( context
, name
, atts
);
928 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
929 on_start_producer( context
, name
, atts
);
930 else if ( strcmp( name
, "blank" ) == 0 )
931 on_start_blank( context
, name
, atts
);
932 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
933 on_start_entry_track( context
, name
, atts
);
934 else if ( strcmp( name
, "filter" ) == 0 )
935 on_start_filter( context
, name
, atts
);
936 else if ( strcmp( name
, "transition" ) == 0 )
937 on_start_transition( context
, name
, atts
);
938 else if ( strcmp( name
, "property" ) == 0 )
939 on_start_property( context
, name
, atts
);
942 static void on_end_element( void *ctx
, const xmlChar
*name
)
944 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
945 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
947 //printf("on_end_element: %s\n", name );
948 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
949 context_pop_node( context
);
950 else if ( strcmp( name
, "multitrack" ) == 0 )
951 on_end_multitrack( context
, name
);
952 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
953 on_end_playlist( context
, name
);
954 else if ( strcmp( name
, "track" ) == 0 )
955 on_end_track( context
, name
);
956 else if ( strcmp( name
, "entry" ) == 0 )
957 on_end_entry( context
, name
);
958 else if ( strcmp( name
, "tractor" ) == 0 )
959 on_end_tractor( context
, name
);
960 else if ( strcmp( name
, "property" ) == 0 )
961 on_end_property( context
, name
);
962 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
963 on_end_producer( context
, name
);
964 else if ( strcmp( name
, "filter" ) == 0 )
965 on_end_filter( context
, name
);
966 else if ( strcmp( name
, "transition" ) == 0 )
967 on_end_transition( context
, name
);
969 context
->branch
[ context
->depth
] = 0;
973 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
975 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
976 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
977 char *value
= calloc( len
+ 1, 1 );
980 strncpy( value
, (const char*) ch
, len
);
982 if ( context
->stack_node_size
> 0 )
983 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
985 else if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
)
986 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
991 /** Convert parameters parsed from resource into entity declarations.
993 static void params_to_entities( deserialise_context context
)
995 if ( context
->params
!= NULL
)
999 // Add our params as entitiy declarations
1000 for ( i
= 0; i
< mlt_properties_count( context
->params
); i
++ )
1002 xmlChar
*name
= ( xmlChar
* )mlt_properties_get_name( context
->params
, i
);
1003 xmlAddDocEntity( context
->entity_doc
, name
, XML_INTERNAL_GENERAL_ENTITY
,
1004 context
->publicId
, context
->systemId
, ( xmlChar
* )mlt_properties_get( context
->params
, name
) );
1008 mlt_properties_close( context
->params
);
1009 context
->params
= NULL
;
1013 // The following 3 facilitate entity substitution in the SAX parser
1014 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
1015 const xmlChar
* publicId
, const xmlChar
* systemId
)
1017 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1018 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1020 context
->publicId
= publicId
;
1021 context
->systemId
= systemId
;
1022 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
1024 // Override default entities with our parameters
1025 params_to_entities( context
);
1028 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
1029 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
1031 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1032 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1034 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1037 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1039 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1040 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1042 // Setup for entity declarations if not ready
1043 if ( xmlGetIntSubset( context
->entity_doc
) == NULL
)
1045 xmlCreateIntSubset( context
->entity_doc
, "westley", "", "" );
1046 context
->publicId
= "";
1047 context
->systemId
= "";
1050 // Add our parameters if not already
1051 params_to_entities( context
);
1053 return xmlGetDocEntity( context
->entity_doc
, name
);
1056 /** Convert a hexadecimal character to its value.
1058 static int tohex( char p
)
1060 return isdigit( p
) ? p
- '0' : tolower( p
) - 'a' + 10;
1063 /** Decode a url-encoded string containing hexadecimal character sequences.
1065 static char *url_decode( char *dest
, char *src
)
1073 *p
++ = ( tohex( *( src
+ 1 ) ) << 4 ) | tohex( *( src
+ 2 ) );
1086 /** Extract the filename from a URL attaching parameters to a properties list.
1088 static void parse_url( mlt_properties properties
, char *url
)
1091 int n
= strlen( url
);
1095 for ( i
= 0; i
< n
; i
++ )
1112 if ( name
!= NULL
&& value
!= NULL
)
1113 mlt_properties_set( properties
, name
, value
);
1119 if ( name
!= NULL
&& value
!= NULL
)
1120 mlt_properties_set( properties
, name
, value
);
1123 mlt_producer
producer_westley_init( char *url
)
1125 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1126 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1127 mlt_properties properties
= NULL
;
1129 struct _xmlParserCtxt
*xmlcontext
;
1130 int well_formed
= 0;
1131 char *filename
= strdup( url
);
1133 context
->producer_map
= mlt_properties_new();
1134 context
->destructors
= mlt_properties_new();
1135 context
->params
= mlt_properties_new();
1137 // Decode URL and parse parameters
1138 parse_url( context
->params
, url_decode( filename
, url
) );
1140 // We need to track the number of registered filters
1141 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1143 // We need the directory prefix which was used for the westley
1144 mlt_properties_set( context
->producer_map
, "_root", "" );
1145 if ( strchr( filename
, '/' ) )
1148 mlt_properties_set( context
->producer_map
, "_root", filename
);
1149 root
= mlt_properties_get( context
->producer_map
, "_root" );
1150 *( strrchr( root
, '/' ) + 1 ) = '\0';
1153 // Setup SAX callbacks
1154 sax
->startElement
= on_start_element
;
1155 sax
->endElement
= on_end_element
;
1156 sax
->characters
= on_characters
;
1157 sax
->cdataBlock
= on_characters
;
1158 sax
->internalSubset
= on_internal_subset
;
1159 sax
->entityDecl
= on_entity_declaration
;
1160 sax
->getEntity
= on_get_entity
;
1162 // Setup libxml2 SAX parsing
1164 xmlSubstituteEntitiesDefault( 1 );
1165 // This is used to facilitate entity substitution in the SAX parser
1166 context
->entity_doc
= xmlNewDoc( "1.0" );
1167 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1168 xmlcontext
->sax
= sax
;
1169 xmlcontext
->_private
= ( void* )context
;
1172 xmlParseDocument( xmlcontext
);
1173 well_formed
= xmlcontext
->wellFormed
;
1175 // Cleanup after parsing
1176 xmlFreeDoc( context
->entity_doc
);
1178 xmlcontext
->sax
= NULL
;
1179 xmlcontext
->_private
= NULL
;
1180 xmlFreeParserCtxt( xmlcontext
);
1181 xmlMemoryDump( ); // for debugging
1183 // Get the last producer on the stack
1184 mlt_service service
= context_pop_service( context
);
1185 if ( well_formed
&& service
!= NULL
)
1187 // Verify it is a producer service (mlt_type="mlt_producer")
1188 // (producer, playlist, multitrack)
1189 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1190 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1195 xmlDocPtr doc
= westley_make_doc( service
);
1196 xmlDocFormatDump( stdout
, doc
, 1 );
1201 if ( well_formed
&& service
!= NULL
)
1204 // Need the complete producer list for various reasons
1205 properties
= context
->destructors
;
1207 // Now make sure we don't have a reference to the service in the properties
1208 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1210 char *name
= mlt_properties_get_name( properties
, i
);
1211 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1213 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1218 // We are done referencing destructor property list
1219 // Set this var to service properties for convenience
1220 properties
= mlt_service_properties( service
);
1222 // make the returned service destroy the connected services
1223 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1225 // Now assign additional properties
1226 mlt_properties_set( properties
, "resource", url
);
1228 // This tells consumer_westley not to deep copy
1229 mlt_properties_set( properties
, "westley", "was here" );
1233 // Return null if not well formed
1237 mlt_properties_close( context
->destructors
);
1241 mlt_properties_close( context
->producer_map
);
1242 if ( context
->params
!= NULL
)
1243 mlt_properties_close( context
->params
);
1247 return MLT_PRODUCER( service
);