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] );
124 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
125 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
128 if ( mlt_properties_get( properties
, "id" ) != NULL
)
129 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
131 context_push_service( context
, service
);
134 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
136 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
138 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
140 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
144 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
146 // Get the playlist from the stack
147 mlt_service service
= context_pop_service( context
);
148 mlt_position length
= 0;
150 // Look for the length attribute
151 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
153 if ( strcmp( atts
[0], "length" ) == 0 )
155 length
= atoll( atts
[1] );
160 // Append a blank to the playlist
161 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
163 // Push the playlist back onto the stack
164 context_push_service( context
, service
);
167 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
169 // Look for the producer attribute
170 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
172 if ( strcmp( atts
[0], "producer" ) == 0 )
174 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
175 // Push the referenced producer onto the stack
176 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
182 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
184 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
186 // Set the properties
187 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
188 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
191 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
193 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
195 // Set the properties
196 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
197 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
200 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
202 mlt_properties properties
= context
->producer_properties
;
205 if ( properties
== NULL
)
208 // Set the properties
209 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
211 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
213 context
->property
= strdup( atts
[ 1 ] );
215 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
217 value
= (char*) atts
[ 1 ];
221 if ( context
->property
!= NULL
&& value
!= NULL
)
222 mlt_properties_set( properties
, context
->property
, value
);
225 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
227 // Get the producer (multitrack) from the stack
228 mlt_service producer
= context_pop_service( context
);
230 // Get the tractor from the stack
231 mlt_service service
= context_pop_service( context
);
233 // Connect the tractor to the producer
234 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
235 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
236 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
238 // Push the tractor back onto the stack
239 context_push_service( context
, service
);
241 // Push the producer back onto the stack
242 context_push_service( context
, producer
);
245 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
247 // Get the playlist from the stack
248 mlt_service service
= context_pop_service( context
);
249 mlt_properties properties
= mlt_service_properties( service
);
251 mlt_position in
= mlt_properties_get_position( properties
, "in" );
254 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
255 out
= mlt_properties_get_position( properties
, "_westley.out" );
257 out
= mlt_properties_get_position( properties
, "length" ) - 1;
259 if ( mlt_properties_get_position( properties
, "length" ) < out
)
260 mlt_properties_set_position( properties
, "length", out
+ 1 );
262 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
264 // Push the playlist back onto the stack
265 context_push_service( context
, service
);
268 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
270 // Get the producer from the stack
271 mlt_service producer
= context_pop_service( context
);
273 // Get the multitrack from the stack
274 mlt_service service
= context_pop_service( context
);
276 // Set the track on the multitrack
277 mlt_multitrack_connect( MLT_MULTITRACK( service
),
278 MLT_PRODUCER( producer
),
279 mlt_multitrack_count( MLT_MULTITRACK( service
) ) );
281 // Push the multitrack back onto the stack
282 context_push_service( context
, service
);
285 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
287 // Get the producer from the stack
288 mlt_service producer
= context_pop_service( context
);
290 // Get the playlist from the stack
291 mlt_service service
= context_pop_service( context
);
293 // Append the producer to the playlist
294 mlt_playlist_append_io( MLT_PLAYLIST( service
),
295 MLT_PRODUCER( producer
),
296 mlt_properties_get_position( mlt_service_properties( producer
), "in" ),
297 mlt_properties_get_position( mlt_service_properties( producer
), "out" ) );
299 // Push the playlist back onto the stack
300 context_push_service( context
, service
);
303 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
305 // Get and discard the last producer
306 mlt_producer multitrack
= MLT_PRODUCER( context_pop_service( context
) );
309 mlt_service tractor
= context_pop_service( context
);
310 multitrack
= mlt_properties_get_data( mlt_service_properties( tractor
), "multitrack", NULL
);
312 // Inherit the producer's properties
313 mlt_properties properties
= mlt_producer_properties( multitrack
);
314 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
315 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
316 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
318 // Push the playlist back onto the stack
319 context_push_service( context
, tractor
);
322 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
324 // Close this property handling
325 free( context
->property
);
326 context
->property
= NULL
;
329 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
331 mlt_properties properties
= context
->producer_properties
;
332 mlt_service service
= NULL
;
334 if ( properties
== NULL
)
337 // Instatiate the producer
338 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties
, "resource" ) ) );
340 if ( service
== NULL
&& mlt_properties_get( properties
, "mlt_service" ) != NULL
)
342 service
= MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties
, "mlt_service" ),
343 mlt_properties_get( properties
, "resource" ) ) );
346 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
348 // Add the producer to the producer map
349 if ( mlt_properties_get( properties
, "id" ) != NULL
)
350 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
352 mlt_properties_inherit( mlt_service_properties( service
), properties
);
353 mlt_properties_close( properties
);
354 context
->producer_properties
= NULL
;
355 properties
= mlt_service_properties( service
);
358 mlt_producer_set_in_and_out( MLT_PRODUCER( service
),
359 mlt_properties_get_position( properties
, "in" ),
360 mlt_properties_get_position( properties
, "out" ) );
362 // Push the new producer onto the stack
363 context_push_service( context
, service
);
366 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
368 mlt_properties properties
= context
->producer_properties
;
369 if ( properties
== NULL
)
376 // Get the producer from the stack
377 mlt_service producer
= context_pop_service( context
);
378 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
381 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
383 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
385 // Connect the filter to the producer
386 mlt_filter_connect( MLT_FILTER( service
), producer
,
387 mlt_properties_get_int( properties
, "track" ) );
389 // Set in and out from producer if non existant
390 if ( mlt_properties_get( properties
, "in" ) == NULL
)
391 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
392 if ( mlt_properties_get( properties
, "out" ) == NULL
)
393 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
395 // Propogate the properties
396 mlt_properties_inherit( mlt_service_properties( service
), properties
);
397 mlt_properties_close( properties
);
398 context
->producer_properties
= NULL
;
399 properties
= mlt_service_properties( service
);
402 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
403 mlt_filter_set_in_and_out( MLT_FILTER( service
),
404 mlt_properties_get_position( properties
, "in" ),
405 mlt_properties_get_position( properties
, "out" ) );
407 // Get the parent producer from the stack
408 mlt_service tractor
= context_pop_service( context
);
410 if ( tractor
!= NULL
)
412 //fprintf( stderr, "connecting tractor %s to filter\n", mlt_properties_get( mlt_service_properties( tractor ), "resource" ) );
413 // Connect the tractor to the filter
414 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor
), "resource" ), "<tractor>" ) == 0 )
415 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
417 // Push the parent producer back onto the stack
418 context_push_service( context
, tractor
);
421 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
422 // If a producer alias is in the producer_map, get it
423 snprintf( key
, 10, "%p", producer
);
424 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
425 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
427 // Put the producer in the producer map
428 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
430 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
432 // For filter chain support, add an alias to the producer map
433 snprintf( key
, 10, "%p", service
);
434 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
436 // Push the filter onto the stack
437 context_push_service( context
, service
);
441 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
443 mlt_properties properties
= context
->producer_properties
;
444 if ( properties
== NULL
)
447 // Get the producer from the stack
448 mlt_service producer
= context_pop_service( context
);
450 // Create the transition
451 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
453 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
455 // Propogate the properties
456 mlt_properties_inherit( mlt_service_properties( service
), properties
);
457 mlt_properties_close( properties
);
458 context
->producer_properties
= NULL
;
459 properties
= mlt_service_properties( service
);
462 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
463 mlt_properties_get_position( properties
, "in" ),
464 mlt_properties_get_position( properties
, "out" ) );
466 // Connect the filter to the producer
467 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
468 mlt_properties_get_int( properties
, "a_track" ),
469 mlt_properties_get_int( properties
, "b_track" ) );
471 // Get the tractor from the stack
472 mlt_service tractor
= context_pop_service( context
);
474 // Connect the tractor to the transition
475 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
477 // Push the tractor back onto the stack
478 context_push_service( context
, tractor
);
480 // Push the transition onto the stack
481 context_push_service( context
, service
);
484 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
486 deserialise_context context
= ( deserialise_context
) ctx
;
488 if ( strcmp( name
, "tractor" ) == 0 )
489 on_start_tractor( context
, name
, atts
);
490 else if ( strcmp( name
, "multitrack" ) == 0 )
491 on_start_multitrack( context
, name
, atts
);
492 else if ( strcmp( name
, "playlist" ) == 0 )
493 on_start_playlist( context
, name
, atts
);
494 else if ( strcmp( name
, "producer" ) == 0 )
495 on_start_producer( context
, name
, atts
);
496 else if ( strcmp( name
, "blank" ) == 0 )
497 on_start_blank( context
, name
, atts
);
498 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
499 on_start_entry_track( context
, name
, atts
);
500 else if ( strcmp( name
, "filter" ) == 0 )
501 on_start_filter( context
, name
, atts
);
502 else if ( strcmp( name
, "transition" ) == 0 )
503 on_start_transition( context
, name
, atts
);
504 else if ( strcmp( name
, "property" ) == 0 )
505 on_start_property( context
, name
, atts
);
508 static void on_end_element( void *ctx
, const xmlChar
*name
)
510 deserialise_context context
= ( deserialise_context
) ctx
;
512 if ( strcmp( name
, "multitrack" ) == 0 )
513 on_end_multitrack( context
, name
);
514 else if ( strcmp( name
, "playlist" ) == 0 )
515 on_end_playlist( context
, name
);
516 else if ( strcmp( name
, "track" ) == 0 )
517 on_end_track( context
, name
);
518 else if ( strcmp( name
, "entry" ) == 0 )
519 on_end_entry( context
, name
);
520 else if ( strcmp( name
, "tractor" ) == 0 )
521 on_end_tractor( context
, name
);
522 else if ( strcmp( name
, "property" ) == 0 )
523 on_end_property( context
, name
);
524 else if ( strcmp( name
, "producer" ) == 0 )
525 on_end_producer( context
, name
);
526 else if ( strcmp( name
, "filter" ) == 0 )
527 on_end_filter( context
, name
);
528 else if ( strcmp( name
, "transition" ) == 0 )
529 on_end_transition( context
, name
);
532 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
534 deserialise_context context
= ( deserialise_context
) ctx
;
535 char *value
= calloc( len
+ 1, 1 );
538 strncpy( value
, (const char*) ch
, len
);
540 if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
)
541 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
546 mlt_producer
producer_westley_init( char *filename
)
549 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
550 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
551 mlt_properties properties
= NULL
;
554 context
->producer_map
= mlt_properties_new();
555 context
->destructors
= mlt_properties_new();
556 // We need to track the number of registered filters
557 mlt_properties_set_int( context
->destructors
, "registered", 0 );
558 sax
->startElement
= on_start_element
;
559 sax
->endElement
= on_end_element
;
560 sax
->characters
= on_characters
;
568 xmlSAXUserParseFile( sax
, context
, filename
);
570 // Need the complete producer list for various reasons
571 properties
= context
->destructors
;
573 // Get the last producer on the stack
574 mlt_service service
= context_pop_service( context
);
576 // Do we actually have a producer here?
577 if ( service
!= NULL
)
579 // Now make sure we don't have a reference to the service in the properties
580 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
582 char *name
= mlt_properties_get_name( properties
, i
);
583 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
585 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
590 // We are done referencing destructor property list
591 // Set this var to service properties for convenience
592 properties
= mlt_service_properties( service
);
594 // make the returned service destroy the connected services
595 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
597 // Now assign additional properties
598 mlt_properties_set( properties
, "resource", filename
);
600 // This tells consumer_westley not to deep copy
601 mlt_properties_set( properties
, "westley", "was here" );
606 mlt_properties_close( properties
);
609 free( context
->stack_service
);
610 mlt_properties_close( context
->producer_map
);
617 return MLT_PRODUCER( service
);