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
23 #include "producer_westley.h"
24 #include <framework/mlt.h>
29 #include <libxml/parser.h>
31 #define STACK_SIZE 1000
33 struct deserialise_context_s
35 mlt_service stack_service
[ STACK_SIZE
];
36 int stack_service_size
;
37 mlt_properties producer_map
;
38 mlt_properties destructors
;
40 mlt_properties producer_properties
;
42 typedef struct deserialise_context_s
*deserialise_context
;
48 static int context_push_service( deserialise_context
this, mlt_service that
)
50 int ret
= this->stack_service_size
>= STACK_SIZE
;
52 this->stack_service
[ this->stack_service_size
++ ] = that
;
59 static mlt_service
context_pop_service( deserialise_context
this )
61 mlt_service result
= NULL
;
62 if ( this->stack_service_size
> 0 )
63 result
= this->stack_service
[ -- this->stack_service_size
];
67 // Set the destructor on a new service
68 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
70 int registered
= mlt_properties_get_int( properties
, "registered" );
71 char *key
= mlt_properties_get( properties
, "registered" );
72 mlt_properties_set_data( properties
, key
, service
, 0, destructor
, NULL
);
73 mlt_properties_set_int( properties
, "registered", ++ registered
);
76 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
78 mlt_service service
= mlt_tractor_service( mlt_tractor_init() );
79 mlt_properties properties
= mlt_service_properties( service
);
81 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
83 mlt_properties_set_position( properties
, "length", 0 );
85 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
86 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
88 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
90 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
91 mlt_properties_set_position( properties
, "length", length
);
94 context_push_service( context
, service
);
97 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
99 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
100 mlt_properties properties
= mlt_service_properties( service
);
102 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
104 mlt_properties_set_position( properties
, "length", 0 );
106 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
107 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
109 context_push_service( context
, service
);
112 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
114 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
115 mlt_properties properties
= mlt_service_properties( service
);
117 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
119 mlt_properties_set_position( properties
, "length", 0 );
121 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
123 mlt_properties_set( properties
, ( char* )atts
[0], ( char* )atts
[1] );
125 // Out will be overwritten later as we append, so we need to save it
126 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
127 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
130 if ( mlt_properties_get( properties
, "id" ) != NULL
)
131 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
133 context_push_service( context
, service
);
136 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
138 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
140 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
142 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
146 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
148 // Get the playlist from the stack
149 mlt_service service
= context_pop_service( context
);
150 mlt_position length
= 0;
152 // Look for the length attribute
153 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
155 if ( strcmp( atts
[0], "length" ) == 0 )
157 length
= atoll( atts
[1] );
162 // Append a blank to the playlist
163 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
165 // Push the playlist back onto the stack
166 context_push_service( context
, service
);
169 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
171 // Use a dummy service to hold properties to allow arbitratry nesting
172 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
173 mlt_service_init( service
, NULL
);
175 // Push the dummy service onto the stack
176 context_push_service( context
, service
);
178 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
180 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
182 // Look for the producer attribute
183 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
185 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
186 // Push the referenced producer onto the stack
187 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
192 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
194 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
196 // Set the properties
197 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
198 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
201 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
203 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
205 // Set the properties
206 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
207 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
210 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
212 mlt_properties properties
= context
->producer_properties
;
215 if ( properties
== NULL
)
218 // Set the properties
219 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
221 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
223 context
->property
= strdup( atts
[ 1 ] );
225 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
227 value
= (char*) atts
[ 1 ];
231 if ( context
->property
!= NULL
&& value
!= NULL
)
232 mlt_properties_set( properties
, context
->property
, value
);
235 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
237 // Get the producer (multitrack) from the stack
238 mlt_service producer
= context_pop_service( context
);
240 // Get the tractor from the stack
241 mlt_service service
= context_pop_service( context
);
243 // Connect the tractor to the producer
244 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
245 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
246 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
248 // Push the tractor back onto the stack
249 context_push_service( context
, service
);
251 // Push the producer back onto the stack
252 context_push_service( context
, producer
);
255 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
257 // Get the playlist from the stack
258 mlt_service service
= context_pop_service( context
);
259 mlt_properties properties
= mlt_service_properties( service
);
261 mlt_position in
= mlt_properties_get_position( properties
, "in" );
264 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
265 out
= mlt_properties_get_position( properties
, "_westley.out" );
267 out
= mlt_properties_get_position( properties
, "length" ) - 1;
269 if ( mlt_properties_get_position( properties
, "length" ) < out
)
270 mlt_properties_set_position( properties
, "length", out
+ 1 );
272 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
274 // Push the playlist back onto the stack
275 context_push_service( context
, service
);
278 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
280 // Get the producer from the stack
281 mlt_service producer
= context_pop_service( context
);
283 // Get the dummy track service from the stack
284 mlt_service track
= context_pop_service( context
);
286 // Get the multitrack from the stack
287 mlt_service service
= context_pop_service( context
);
289 // Set the track on the multitrack
290 mlt_multitrack_connect( MLT_MULTITRACK( service
),
291 MLT_PRODUCER( producer
),
292 mlt_multitrack_count( MLT_MULTITRACK( service
) ) );
294 // Set producer i/o if specified
295 if ( mlt_properties_get( mlt_service_properties( track
), "in" ) != NULL
||
296 mlt_properties_get( mlt_service_properties( track
), "out" ) != NULL
)
298 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
),
299 mlt_properties_get_position( mlt_service_properties( track
), "in" ),
300 mlt_properties_get_position( mlt_service_properties( track
), "out" ) );
303 // Push the multitrack back onto the stack
304 context_push_service( context
, service
);
306 mlt_service_close( track
);
309 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
311 // Get the producer from the stack
312 mlt_service producer
= context_pop_service( context
);
314 // Get the dummy entry service from the stack
315 mlt_service entry
= context_pop_service( context
);
317 // Get the playlist from the stack
318 mlt_service service
= context_pop_service( context
);
320 // Append the producer to the playlist
321 if ( mlt_properties_get( mlt_service_properties( entry
), "in" ) != NULL
||
322 mlt_properties_get( mlt_service_properties( entry
), "out" ) != NULL
)
324 mlt_playlist_append_io( MLT_PLAYLIST( service
),
325 MLT_PRODUCER( producer
),
326 mlt_properties_get_position( mlt_service_properties( entry
), "in" ),
327 mlt_properties_get_position( mlt_service_properties( entry
), "out" ) );
331 mlt_playlist_append( MLT_PLAYLIST( service
), MLT_PRODUCER( producer
) );
334 // Push the playlist back onto the stack
335 context_push_service( context
, service
);
337 mlt_service_close( entry
);
340 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
342 // Get and discard the last producer
343 mlt_producer multitrack
= MLT_PRODUCER( context_pop_service( context
) );
346 mlt_service tractor
= context_pop_service( context
);
347 multitrack
= mlt_properties_get_data( mlt_service_properties( tractor
), "multitrack", NULL
);
349 // Inherit the producer's properties
350 mlt_properties properties
= mlt_producer_properties( multitrack
);
351 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
352 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
353 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
355 // Push the playlist back onto the stack
356 context_push_service( context
, tractor
);
359 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
361 // Close this property handling
362 free( context
->property
);
363 context
->property
= NULL
;
366 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
368 mlt_properties properties
= context
->producer_properties
;
369 mlt_service service
= NULL
;
371 if ( properties
== NULL
)
374 // Instatiate the producer
375 if ( mlt_properties_get( properties
, "resource" ) != NULL
)
376 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties
, "resource" ) ) );
377 if ( service
== NULL
&& mlt_properties_get( properties
, "mlt_service" ) != NULL
)
379 service
= MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties
, "mlt_service" ),
380 mlt_properties_get( properties
, "resource" ) ) );
383 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
385 // Add the producer to the producer map
386 if ( mlt_properties_get( properties
, "id" ) != NULL
)
387 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
389 mlt_properties_inherit( mlt_service_properties( service
), properties
);
390 mlt_properties_close( properties
);
391 context
->producer_properties
= NULL
;
392 properties
= mlt_service_properties( service
);
395 mlt_producer_set_in_and_out( MLT_PRODUCER( service
),
396 mlt_properties_get_position( properties
, "in" ),
397 mlt_properties_get_position( properties
, "out" ) );
399 // Push the new producer onto the stack
400 context_push_service( context
, service
);
403 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
405 mlt_properties properties
= context
->producer_properties
;
406 if ( properties
== NULL
)
413 // Get the producer from the stack
414 mlt_service producer
= context_pop_service( context
);
415 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
418 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
420 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
422 // Connect the filter to the producer
423 mlt_filter_connect( MLT_FILTER( service
), producer
,
424 mlt_properties_get_int( properties
, "track" ) );
426 // Set in and out from producer if non existant
427 if ( mlt_properties_get( properties
, "in" ) == NULL
)
428 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
429 if ( mlt_properties_get( properties
, "out" ) == NULL
)
430 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
432 // Propogate the properties
433 mlt_properties_inherit( mlt_service_properties( service
), properties
);
434 mlt_properties_close( properties
);
435 context
->producer_properties
= NULL
;
436 properties
= mlt_service_properties( service
);
439 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
440 mlt_filter_set_in_and_out( MLT_FILTER( service
),
441 mlt_properties_get_position( properties
, "in" ),
442 mlt_properties_get_position( properties
, "out" ) );
444 // Get the parent producer from the stack
445 mlt_service tractor
= context_pop_service( context
);
447 if ( tractor
!= NULL
)
449 //fprintf( stderr, "connecting tractor %s to filter\n", mlt_properties_get( mlt_service_properties( tractor ), "resource" ) );
450 // Connect the tractor to the filter
451 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor
), "resource" ), "<tractor>" ) == 0 )
452 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
454 // Push the parent producer back onto the stack
455 context_push_service( context
, tractor
);
458 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
459 // If a producer alias is in the producer_map, get it
460 snprintf( key
, 10, "%p", producer
);
461 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
462 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
464 // Put the producer in the producer map
465 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
467 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
469 // For filter chain support, add an alias to the producer map
470 snprintf( key
, 10, "%p", service
);
471 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
473 // Push the filter onto the stack
474 context_push_service( context
, service
);
478 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
480 mlt_properties properties
= context
->producer_properties
;
481 if ( properties
== NULL
)
484 // Get the producer from the stack
485 mlt_service producer
= context_pop_service( context
);
487 // Create the transition
488 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
490 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
492 // Propogate the properties
493 mlt_properties_inherit( mlt_service_properties( service
), properties
);
494 mlt_properties_close( properties
);
495 context
->producer_properties
= NULL
;
496 properties
= mlt_service_properties( service
);
499 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
500 mlt_properties_get_position( properties
, "in" ),
501 mlt_properties_get_position( properties
, "out" ) );
503 // Connect the filter to the producer
504 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
505 mlt_properties_get_int( properties
, "a_track" ),
506 mlt_properties_get_int( properties
, "b_track" ) );
508 // Get the tractor from the stack
509 mlt_service tractor
= context_pop_service( context
);
511 // Connect the tractor to the transition
512 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
514 // Push the tractor back onto the stack
515 context_push_service( context
, tractor
);
517 // Push the transition onto the stack
518 context_push_service( context
, service
);
521 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
523 deserialise_context context
= ( deserialise_context
) ctx
;
525 if ( strcmp( name
, "tractor" ) == 0 )
526 on_start_tractor( context
, name
, atts
);
527 else if ( strcmp( name
, "multitrack" ) == 0 )
528 on_start_multitrack( context
, name
, atts
);
529 else if ( strcmp( name
, "playlist" ) == 0 )
530 on_start_playlist( context
, name
, atts
);
531 else if ( strcmp( name
, "producer" ) == 0 )
532 on_start_producer( context
, name
, atts
);
533 else if ( strcmp( name
, "blank" ) == 0 )
534 on_start_blank( context
, name
, atts
);
535 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
536 on_start_entry_track( context
, name
, atts
);
537 else if ( strcmp( name
, "filter" ) == 0 )
538 on_start_filter( context
, name
, atts
);
539 else if ( strcmp( name
, "transition" ) == 0 )
540 on_start_transition( context
, name
, atts
);
541 else if ( strcmp( name
, "property" ) == 0 )
542 on_start_property( context
, name
, atts
);
545 static void on_end_element( void *ctx
, const xmlChar
*name
)
547 deserialise_context context
= ( deserialise_context
) ctx
;
549 if ( strcmp( name
, "multitrack" ) == 0 )
550 on_end_multitrack( context
, name
);
551 else if ( strcmp( name
, "playlist" ) == 0 )
552 on_end_playlist( context
, name
);
553 else if ( strcmp( name
, "track" ) == 0 )
554 on_end_track( context
, name
);
555 else if ( strcmp( name
, "entry" ) == 0 )
556 on_end_entry( context
, name
);
557 else if ( strcmp( name
, "tractor" ) == 0 )
558 on_end_tractor( context
, name
);
559 else if ( strcmp( name
, "property" ) == 0 )
560 on_end_property( context
, name
);
561 else if ( strcmp( name
, "producer" ) == 0 )
562 on_end_producer( context
, name
);
563 else if ( strcmp( name
, "filter" ) == 0 )
564 on_end_filter( context
, name
);
565 else if ( strcmp( name
, "transition" ) == 0 )
566 on_end_transition( context
, name
);
569 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
571 deserialise_context context
= ( deserialise_context
) ctx
;
572 char *value
= calloc( len
+ 1, 1 );
575 strncpy( value
, (const char*) ch
, len
);
577 if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
)
578 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
583 mlt_producer
producer_westley_init( char *filename
)
586 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
587 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
588 mlt_properties properties
= NULL
;
591 context
->producer_map
= mlt_properties_new();
592 context
->destructors
= mlt_properties_new();
593 // We need to track the number of registered filters
594 mlt_properties_set_int( context
->destructors
, "registered", 0 );
595 sax
->startElement
= on_start_element
;
596 sax
->endElement
= on_end_element
;
597 sax
->characters
= on_characters
;
605 xmlSAXUserParseFile( sax
, context
, filename
);
607 // Need the complete producer list for various reasons
608 properties
= context
->destructors
;
610 // Get the last producer on the stack
611 mlt_service service
= context_pop_service( context
);
613 // Do we actually have a producer here?
614 if ( service
!= NULL
)
616 // Now make sure we don't have a reference to the service in the properties
617 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
619 char *name
= mlt_properties_get_name( properties
, i
);
620 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
622 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
627 // We are done referencing destructor property list
628 // Set this var to service properties for convenience
629 properties
= mlt_service_properties( service
);
631 // make the returned service destroy the connected services
632 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
634 // Now assign additional properties
635 mlt_properties_set( properties
, "resource", filename
);
637 // This tells consumer_westley not to deep copy
638 mlt_properties_set( properties
, "westley", "was here" );
643 mlt_properties_close( properties
);
646 free( context
->stack_service
);
647 mlt_properties_close( context
->producer_map
);
654 return MLT_PRODUCER( service
);