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).
23 // TODO: determine why deserialise_context can not be released.
25 #include "producer_westley.h"
26 #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
];
59 typedef struct deserialise_context_s
*deserialise_context
;
61 /** Convert the numerical current branch address to a dot-delimited string.
63 static char *serialise_branch( deserialise_context
this, char *s
)
68 for ( i
= 0; i
< this->depth
; i
++ )
70 int len
= strlen( s
);
71 snprintf( s
+ len
, BRANCH_SIG_LEN
- len
, "%d.", this->branch
[ i
] );
79 static int context_push_service( deserialise_context
this, mlt_service that
)
81 int ret
= this->stack_service_size
>= STACK_SIZE
- 1;
84 this->stack_service
[ this->stack_service_size
++ ] = that
;
86 // Record the tree branch on which this service lives
87 if ( mlt_properties_get( mlt_service_properties( that
), "_westley_branch" ) == NULL
)
89 char s
[ BRANCH_SIG_LEN
];
90 mlt_properties_set( mlt_service_properties( that
), "_westley_branch", serialise_branch( this, s
) );
99 static mlt_service
context_pop_service( deserialise_context
this )
101 mlt_service result
= NULL
;
102 if ( this->stack_service_size
> 0 )
103 result
= this->stack_service
[ -- this->stack_service_size
];
110 static int context_push_node( deserialise_context
this, xmlNodePtr node
)
112 int ret
= this->stack_node_size
>= STACK_SIZE
- 1;
114 this->stack_node
[ this->stack_node_size
++ ] = node
;
121 static xmlNodePtr
context_pop_node( deserialise_context
this )
123 xmlNodePtr result
= NULL
;
124 if ( this->stack_node_size
> 0 )
125 result
= this->stack_node
[ -- this->stack_node_size
];
130 // Set the destructor on a new service
131 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
133 int registered
= mlt_properties_get_int( properties
, "registered" );
134 char *key
= mlt_properties_get( properties
, "registered" );
135 mlt_properties_set_data( properties
, key
, service
, 0, destructor
, NULL
);
136 mlt_properties_set_int( properties
, "registered", ++ registered
);
140 // Forward declarations
141 static void on_end_track( deserialise_context context
, const xmlChar
*name
);
142 static void on_end_entry( deserialise_context context
, const xmlChar
*name
);
144 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
146 mlt_service service
= mlt_tractor_service( mlt_tractor_init() );
147 mlt_properties properties
= mlt_service_properties( service
);
149 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
151 mlt_properties_set_position( properties
, "length", 0 );
153 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
154 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
156 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
158 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
159 mlt_properties_set_position( properties
, "length", length
);
162 if ( mlt_properties_get( properties
, "id" ) != NULL
)
163 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
165 context_push_service( context
, service
);
168 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
170 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
171 mlt_properties properties
= mlt_service_properties( service
);
173 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
175 mlt_properties_set_position( properties
, "length", 0 );
177 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
178 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
180 if ( mlt_properties_get( properties
, "id" ) != NULL
)
181 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
183 context_push_service( context
, service
);
186 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
188 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
189 mlt_properties properties
= mlt_service_properties( service
);
191 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
193 mlt_properties_set_position( properties
, "length", 0 );
195 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
197 mlt_properties_set( properties
, ( char* )atts
[0], ( char* )atts
[1] );
199 // Out will be overwritten later as we append, so we need to save it
200 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
201 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
204 if ( mlt_properties_get( properties
, "id" ) != NULL
)
205 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
207 context_push_service( context
, service
);
210 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
212 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
214 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
216 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
220 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
222 // Get the playlist from the stack
223 mlt_service service
= context_pop_service( context
);
224 mlt_position length
= 0;
226 if ( service
== NULL
)
229 // Look for the length attribute
230 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
232 if ( strcmp( atts
[0], "length" ) == 0 )
234 length
= atoll( atts
[1] );
239 // Append a blank to the playlist
240 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
242 // Push the playlist back onto the stack
243 context_push_service( context
, service
);
246 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
248 // Use a dummy service to hold properties to allow arbitrary nesting
249 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
250 mlt_service_init( service
, NULL
);
252 // Push the dummy service onto the stack
253 context_push_service( context
, service
);
255 if ( strcmp( name
, "entry" ) == 0 )
256 mlt_properties_set( mlt_service_properties( service
), "resource", "<entry>" );
258 mlt_properties_set( mlt_service_properties( service
), "resource", "<track>" );
260 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
262 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
264 // Look for the producer attribute
265 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
267 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
268 // Push the referenced producer onto the stack
269 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
271 // Remove the dummy service to cause end element failure
272 context_pop_service( context
);
277 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
279 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
281 // Set the properties
282 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
283 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
286 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
288 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
290 // Set the properties
291 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
292 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
295 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
297 mlt_properties properties
= context
->producer_properties
;
300 if ( properties
== NULL
)
303 // Set the properties
304 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
306 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
308 context
->property
= strdup( atts
[ 1 ] );
310 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
312 value
= (char*) atts
[ 1 ];
316 if ( context
->property
!= NULL
&& value
!= NULL
)
317 mlt_properties_set( properties
, context
->property
, value
);
319 // Tell parser to collect any further nodes for serialisation
320 context
->is_value
= 1;
324 /** This function adds a producer to a playlist or multitrack when
325 there is no entry or track element.
328 static int add_producer( deserialise_context context
, mlt_service service
, mlt_position in
, mlt_position out
)
330 // Get the parent producer
331 mlt_service producer
= context_pop_service( context
);
333 if ( producer
!= NULL
)
335 char current_branch
[ BRANCH_SIG_LEN
];
336 char *service_branch
= mlt_properties_get( mlt_service_properties( producer
), "_westley_branch" );
338 // Validate the producer from the stack is an ancestor and not predecessor
339 serialise_branch( context
, current_branch
);
340 if ( service_branch
!= NULL
&& strncmp( service_branch
, current_branch
, strlen( service_branch
) ) == 0 )
342 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
344 // Put the parent producer back
345 context_push_service( context
, producer
);
347 // If the parent producer is a multitrack or playlist (not a track or entry)
348 if ( resource
&& ( strcmp( resource
, "<playlist>" ) == 0 ||
349 strcmp( resource
, "<multitrack>" ) == 0 ) )
351 //printf( "add_producer: current branch %s service branch %s (%d)\n", current_branch, service_branch, strncmp( service_branch, current_branch, strlen( service_branch ) ) );
352 if ( strcmp( resource
, "<playlist>" ) == 0 )
354 // Append this producer to the playlist
355 mlt_playlist_append_io( MLT_PLAYLIST( producer
),
356 MLT_PRODUCER( service
), in
, out
);
360 mlt_properties properties
= mlt_service_properties( service
);
362 // Set this producer on the multitrack
363 mlt_multitrack_connect( MLT_MULTITRACK( producer
),
364 MLT_PRODUCER( service
),
365 mlt_multitrack_count( MLT_MULTITRACK( producer
) ) );
367 // Set the hide state of the track producer
368 char *hide_s
= mlt_properties_get( properties
, "hide" );
369 if ( hide_s
!= NULL
)
371 if ( strcmp( hide_s
, "video" ) == 0 )
372 mlt_properties_set_int( properties
, "hide", 1 );
373 else if ( strcmp( hide_s
, "audio" ) == 0 )
374 mlt_properties_set_int( properties
, "hide", 2 );
375 else if ( strcmp( hide_s
, "both" ) == 0 )
376 mlt_properties_set_int( properties
, "hide", 3 );
380 // Normally, the enclosing entry or track will pop this service off
381 // In its absence we do not push it on.
389 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
391 // Get the multitrack from the stack
392 mlt_service producer
= context_pop_service( context
);
393 if ( producer
== NULL
)
396 // Get the tractor from the stack
397 mlt_service service
= context_pop_service( context
);
399 // Create a tractor if one does not exist
400 char *resource
= NULL
;
401 if ( service
!= NULL
)
402 resource
= mlt_properties_get( mlt_service_properties( service
), "resource" );
403 if ( service
== NULL
|| resource
== NULL
|| strcmp( resource
, "<tractor>" ) )
405 //printf("creating a tractor\n");
406 char current_branch
[ BRANCH_SIG_LEN
];
408 // Put the anonymous service back onto the stack!
409 if ( service
!= NULL
)
410 context_push_service( context
, service
);
412 // Fabricate the tractor
413 service
= mlt_tractor_service( mlt_tractor_init() );
414 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
416 // Inherit the producer's properties
417 mlt_properties properties
= mlt_service_properties( service
);
418 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( MLT_PRODUCER( producer
) ) + 1 );
419 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
420 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer
) ) );
422 mlt_properties_set( properties
, "_westley_branch", serialise_branch( context
, current_branch
) );
425 // Connect the tractor to the multitrack
426 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
427 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
428 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
430 // See if the tractor should be added to a playlist or multitrack
431 add_producer( context
, service
, 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
433 // Always push the multitrack back onto the stack for filters and transitions
434 context_push_service( context
, producer
);
436 // Always push the tractor back onto the stack for filters and transitions
437 context_push_service( context
, service
);
440 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
442 // Get the playlist from the stack
443 mlt_service producer
= context_pop_service( context
);
444 if ( producer
== NULL
)
446 mlt_properties properties
= mlt_service_properties( producer
);
448 mlt_position in
= mlt_properties_get_position( properties
, "in" );
451 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
452 out
= mlt_properties_get_position( properties
, "_westley.out" );
454 out
= mlt_properties_get_position( properties
, "length" ) - 1;
456 if ( mlt_properties_get_position( properties
, "length" ) < out
)
457 mlt_properties_set_position( properties
, "length", out
+ 1 );
459 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
), in
, out
);
461 // See if the playlist should be added to a playlist or multitrack
462 if ( add_producer( context
, producer
, in
, out
) == 0 )
464 // Otherwise, push the playlist back onto the stack
465 context_push_service( context
, producer
);
468 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
470 // Get the producer from the stack
471 mlt_service producer
= context_pop_service( context
);
472 if ( producer
== NULL
)
474 mlt_properties producer_props
= mlt_service_properties( producer
);
476 // See if the producer is a tractor
477 char *resource
= mlt_properties_get( producer_props
, "resource" );
478 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
479 // If so chomp its producer
480 context_pop_service( context
);
482 // Get the dummy track service from the stack
483 mlt_service track
= context_pop_service( context
);
484 if ( track
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( track
), "resource" ), "<track>" ) )
486 context_push_service( context
, producer
);
489 mlt_properties track_props
= mlt_service_properties( track
);
491 // Get the multitrack from the stack
492 mlt_service service
= context_pop_service( context
);
493 if ( service
== NULL
)
495 context_push_service( context
, producer
);
499 // Set the track on the multitrack
500 mlt_multitrack_connect( MLT_MULTITRACK( service
),
501 MLT_PRODUCER( producer
),
502 mlt_multitrack_count( MLT_MULTITRACK( service
) ) );
504 // Set producer i/o if specified
505 if ( mlt_properties_get( track_props
, "in" ) != NULL
||
506 mlt_properties_get( track_props
, "out" ) != NULL
)
508 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
),
509 mlt_properties_get_position( track_props
, "in" ),
510 mlt_properties_get_position( track_props
, "out" ) );
513 // Set the hide state of the track producer
514 char *hide_s
= mlt_properties_get( track_props
, "hide" );
515 if ( hide_s
!= NULL
)
517 if ( strcmp( hide_s
, "video" ) == 0 )
518 mlt_properties_set_int( producer_props
, "hide", 1 );
519 else if ( strcmp( hide_s
, "audio" ) == 0 )
520 mlt_properties_set_int( producer_props
, "hide", 2 );
521 else if ( strcmp( hide_s
, "both" ) == 0 )
522 mlt_properties_set_int( producer_props
, "hide", 3 );
525 // Push the multitrack back onto the stack
526 context_push_service( context
, service
);
528 mlt_service_close( track
);
531 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
533 // Get the producer from the stack
534 mlt_service producer
= context_pop_service( context
);
535 if ( producer
== NULL
)
538 // See if the producer is a tractor
539 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
540 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
541 // If so chomp its producer
542 context_pop_service( context
);
544 // Get the dummy entry service from the stack
545 mlt_service entry
= context_pop_service( context
);
546 if ( entry
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( entry
), "resource" ), "<entry>" ) )
548 context_push_service( context
, producer
);
552 // Get the playlist from the stack
553 mlt_service service
= context_pop_service( context
);
554 if ( service
== NULL
)
556 context_push_service( context
, producer
);
560 // Append the producer to the playlist
561 if ( mlt_properties_get( mlt_service_properties( entry
), "in" ) != NULL
||
562 mlt_properties_get( mlt_service_properties( entry
), "out" ) != NULL
)
564 mlt_playlist_append_io( MLT_PLAYLIST( service
),
565 MLT_PRODUCER( producer
),
566 mlt_properties_get_position( mlt_service_properties( entry
), "in" ),
567 mlt_properties_get_position( mlt_service_properties( entry
), "out" ) );
571 mlt_playlist_append( MLT_PLAYLIST( service
), MLT_PRODUCER( producer
) );
574 // Push the playlist back onto the stack
575 context_push_service( context
, service
);
577 mlt_service_close( entry
);
580 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
583 mlt_service tractor
= context_pop_service( context
);
584 if ( tractor
== NULL
)
587 // Get the tractor's multitrack
588 mlt_producer multitrack
= mlt_properties_get_data( mlt_service_properties( tractor
), "multitrack", NULL
);
589 if ( multitrack
!= NULL
)
591 // Inherit the producer's properties
592 mlt_properties properties
= mlt_producer_properties( MLT_PRODUCER( tractor
) );
593 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
594 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
595 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
598 // See if the tractor should be added to a playlist or multitrack
599 if ( add_producer( context
, tractor
, 0, mlt_producer_get_out( MLT_PRODUCER( tractor
) ) ) == 0 )
601 // Otherwise, push the tractor back onto the stack
602 context_push_service( context
, tractor
);
605 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
607 // Tell parser to stop building a tree
608 context
->is_value
= 0;
610 // See if there is a xml tree for the value
611 if ( context
->property
!= NULL
&& context
->value_doc
!= NULL
)
616 // Serialise the tree to get value
617 xmlDocDumpMemory( context
->value_doc
, &value
, &size
);
618 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
620 xmlFreeDoc( context
->value_doc
);
621 context
->value_doc
= NULL
;
624 // Close this property handling
625 free( context
->property
);
626 context
->property
= NULL
;
629 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
631 mlt_properties properties
= context
->producer_properties
;
632 mlt_service service
= NULL
;
634 if ( properties
== NULL
)
637 char *resource
= mlt_properties_get( properties
, "resource" );
638 // Let Kino-SMIL src be a synonym for resource
639 if ( resource
== NULL
)
640 resource
= mlt_properties_get( properties
, "src" );
642 // Instantiate the producer
643 if ( mlt_properties_get( properties
, "mlt_service" ) != NULL
)
646 strncpy( temp
, mlt_properties_get( properties
, "mlt_service" ), 1024 );
647 if ( resource
!= NULL
)
650 strncat( temp
, resource
, 1023 - strlen( temp
) );
652 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", temp
) );
654 if ( service
== NULL
&& resource
!= NULL
)
656 char *root
= mlt_properties_get( context
->producer_map
, "_root" );
657 char *full_resource
= malloc( strlen( root
) + strlen( resource
) + 1 );
658 if ( resource
[ 0 ] != '/' )
660 strcpy( full_resource
, root
);
661 strcat( full_resource
, resource
);
665 strcpy( full_resource
, resource
);
667 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource
) );
668 free( full_resource
);
670 if ( service
== NULL
)
672 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
674 // Add the producer to the producer map
675 if ( mlt_properties_get( properties
, "id" ) != NULL
)
676 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
678 // Handle in/out properties separately
679 mlt_position in
= -1;
680 mlt_position out
= -1;
683 if ( mlt_properties_get( properties
, "in" ) != NULL
)
684 in
= mlt_properties_get_position( properties
, "in" );
685 // Let Kino-SMIL clipBegin be a synonym for in
686 if ( mlt_properties_get( properties
, "clipBegin" ) != NULL
)
687 in
= mlt_properties_get_position( properties
, "clipBegin" );
689 if ( mlt_properties_get( properties
, "out" ) != NULL
)
690 out
= mlt_properties_get_position( properties
, "out" );
691 // Let Kino-SMIL clipEnd be a synonym for out
692 if ( mlt_properties_get( properties
, "clipEnd" ) != NULL
)
693 out
= mlt_properties_get_position( properties
, "clipEnd" );
696 mlt_properties_set( properties
, "in", NULL
);
697 mlt_properties_set( properties
, "out", NULL
);
699 mlt_properties_inherit( mlt_service_properties( service
), properties
);
700 mlt_properties_close( properties
);
701 context
->producer_properties
= NULL
;
703 // See if the producer should be added to a playlist or multitrack
704 if ( add_producer( context
, service
, in
, out
) == 0 )
706 // Otherwise, set in and out on...
707 if ( in
!= -1 || out
!= -1 )
709 // Get the parent service
710 mlt_service parent
= context_pop_service( context
);
711 if ( parent
!= NULL
)
713 // Get the parent properties
714 properties
= mlt_service_properties( parent
);
716 char *resource
= mlt_properties_get( properties
, "resource" );
718 // Put the parent producer back
719 context_push_service( context
, parent
);
721 // If the parent is a track or entry
722 if ( resource
&& ( strcmp( resource
, "<entry>" ) == 0 ) )
724 mlt_properties_set_position( properties
, "in", in
);
725 mlt_properties_set_position( properties
, "out", out
);
729 // Otherwise, set in and out on producer directly
730 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
735 // Otherwise, set in and out on producer directly
736 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
740 // Push the producer onto the stack
741 context_push_service( context
, service
);
745 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
747 mlt_properties properties
= context
->producer_properties
;
748 if ( properties
== NULL
)
754 mlt_service tractor
= NULL
;
756 // Get the producer from the stack
757 mlt_service producer
= context_pop_service( context
);
758 if ( producer
== NULL
)
761 // See if the producer is a tractor
762 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
763 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
765 // If so, then get the next producer
767 producer
= context_pop_service( context
);
770 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
773 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
774 if ( service
== NULL
)
776 context_push_service( context
, producer
);
779 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
781 // Connect the filter to the producer
782 mlt_filter_connect( MLT_FILTER( service
), producer
,
783 mlt_properties_get_int( properties
, "track" ) );
785 // Set in and out from producer if non existant
786 if ( mlt_properties_get( properties
, "in" ) == NULL
)
787 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
788 if ( mlt_properties_get( properties
, "out" ) == NULL
)
789 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
791 // Propogate the properties
792 mlt_properties_inherit( mlt_service_properties( service
), properties
);
793 mlt_properties_close( properties
);
794 context
->producer_properties
= NULL
;
795 properties
= mlt_service_properties( service
);
797 // Set in and out again due to inheritance
798 mlt_filter_set_in_and_out( MLT_FILTER( service
),
799 mlt_properties_get_position( properties
, "in" ),
800 mlt_properties_get_position( properties
, "out" ) );
802 // If a producer alias is in the producer_map, get it
803 snprintf( key
, 10, "%p", producer
);
804 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
805 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
807 // Put the producer in the producer map
808 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
810 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
812 // For filter chain support, add an alias to the producer map
813 snprintf( key
, 10, "%p", service
);
814 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
816 // Push the filter onto the stack
817 context_push_service( context
, service
);
819 if ( tractor
!= NULL
)
821 // Connect the tractor to the filter
822 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
824 // Push the tractor back onto the stack
825 context_push_service( context
, tractor
);
829 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
831 mlt_service tractor
= NULL
;
832 mlt_properties properties
= context
->producer_properties
;
833 if ( properties
== NULL
)
836 // Get the producer from the stack
837 mlt_service producer
= context_pop_service( context
);
838 if ( producer
== NULL
)
841 // See if the producer is a tractor
842 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
843 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
845 // If so, then get the next producer
847 producer
= context_pop_service( context
);
850 // Create the transition
851 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
852 if ( service
== NULL
)
854 context_push_service( context
, producer
);
857 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
859 // Propogate the properties
860 mlt_properties_inherit( mlt_service_properties( service
), properties
);
861 mlt_properties_close( properties
);
862 context
->producer_properties
= NULL
;
863 properties
= mlt_service_properties( service
);
865 // Set in and out again due to inheritance
866 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
867 mlt_properties_get_position( properties
, "in" ),
868 mlt_properties_get_position( properties
, "out" ) );
870 // Connect the transition to the producer
871 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
872 mlt_properties_get_int( properties
, "a_track" ),
873 mlt_properties_get_int( properties
, "b_track" ) );
875 // Push the transition onto the stack
876 context_push_service( context
, service
);
878 if ( tractor
!= NULL
)
880 // Connect the tractor to the transition
881 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
883 // Push the tractor back onto the stack
884 context_push_service( context
, tractor
);
888 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
890 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
891 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
893 //printf("on_start_element: %s\n", name );
894 context
->branch
[ context
->depth
] ++;
897 // Build a tree from nodes within a property value
898 if ( context
->is_value
== 1 )
900 xmlNodePtr node
= xmlNewNode( NULL
, name
);
902 if ( context
->value_doc
== NULL
)
905 context
->value_doc
= xmlNewDoc( "1.0" );
906 xmlDocSetRootElement( context
->value_doc
, node
);
910 // Append child to tree
911 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
913 context_push_node( context
, node
);
915 // Set the attributes
916 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
917 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
919 else if ( strcmp( name
, "tractor" ) == 0 )
920 on_start_tractor( context
, name
, atts
);
921 else if ( strcmp( name
, "multitrack" ) == 0 )
922 on_start_multitrack( context
, name
, atts
);
923 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
924 on_start_playlist( context
, name
, atts
);
925 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
926 on_start_producer( context
, name
, atts
);
927 else if ( strcmp( name
, "blank" ) == 0 )
928 on_start_blank( context
, name
, atts
);
929 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
930 on_start_entry_track( context
, name
, atts
);
931 else if ( strcmp( name
, "filter" ) == 0 )
932 on_start_filter( context
, name
, atts
);
933 else if ( strcmp( name
, "transition" ) == 0 )
934 on_start_transition( context
, name
, atts
);
935 else if ( strcmp( name
, "property" ) == 0 )
936 on_start_property( context
, name
, atts
);
939 static void on_end_element( void *ctx
, const xmlChar
*name
)
941 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
942 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
944 //printf("on_end_element: %s\n", name );
945 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
946 context_pop_node( context
);
947 else if ( strcmp( name
, "multitrack" ) == 0 )
948 on_end_multitrack( context
, name
);
949 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
950 on_end_playlist( context
, name
);
951 else if ( strcmp( name
, "track" ) == 0 )
952 on_end_track( context
, name
);
953 else if ( strcmp( name
, "entry" ) == 0 )
954 on_end_entry( context
, name
);
955 else if ( strcmp( name
, "tractor" ) == 0 )
956 on_end_tractor( context
, name
);
957 else if ( strcmp( name
, "property" ) == 0 )
958 on_end_property( context
, name
);
959 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
960 on_end_producer( context
, name
);
961 else if ( strcmp( name
, "filter" ) == 0 )
962 on_end_filter( context
, name
);
963 else if ( strcmp( name
, "transition" ) == 0 )
964 on_end_transition( context
, name
);
966 context
->branch
[ context
->depth
] = 0;
970 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
972 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
973 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
974 char *value
= calloc( len
+ 1, 1 );
977 strncpy( value
, (const char*) ch
, len
);
979 if ( context
->stack_node_size
> 0 )
980 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
982 else if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
)
983 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
988 // The following 3 facilitate entity substitution in the SAX parser
989 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
990 const xmlChar
* publicId
, const xmlChar
* systemId
)
992 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
993 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
995 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
998 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
999 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
1001 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1002 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1004 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1007 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1009 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1010 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1012 return xmlGetDocEntity( context
->entity_doc
, name
);
1016 mlt_producer
producer_westley_init( char *filename
)
1018 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1019 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1020 mlt_properties properties
= NULL
;
1022 struct _xmlParserCtxt
*xmlcontext
;
1023 int well_formed
= 0;
1025 context
->producer_map
= mlt_properties_new();
1026 context
->destructors
= mlt_properties_new();
1028 // We need to track the number of registered filters
1029 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1031 // We need the directory prefix which was used for the westley
1032 mlt_properties_set( context
->producer_map
, "_root", "" );
1033 if ( strchr( filename
, '/' ) )
1036 mlt_properties_set( context
->producer_map
, "_root", filename
);
1037 root
= mlt_properties_get( context
->producer_map
, "_root" );
1038 *( strrchr( root
, '/' ) + 1 ) = '\0';
1041 // Setup SAX callbacks
1042 sax
->startElement
= on_start_element
;
1043 sax
->endElement
= on_end_element
;
1044 sax
->characters
= on_characters
;
1045 sax
->cdataBlock
= on_characters
;
1046 sax
->internalSubset
= on_internal_subset
;
1047 sax
->entityDecl
= on_entity_declaration
;
1048 sax
->getEntity
= on_get_entity
;
1050 // Setup libxml2 SAX parsing
1052 xmlSubstituteEntitiesDefault( 1 );
1053 // This is used to facilitate entity substitution in the SAX parser
1054 context
->entity_doc
= xmlNewDoc( "1.0" );
1055 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1056 xmlcontext
->sax
= sax
;
1057 xmlcontext
->_private
= ( void* )context
;
1060 xmlParseDocument( xmlcontext
);
1061 well_formed
= xmlcontext
->wellFormed
;
1063 // Cleanup after parsing
1064 xmlFreeDoc( context
->entity_doc
);
1066 xmlcontext
->sax
= NULL
;
1067 xmlcontext
->_private
= NULL
;
1068 xmlFreeParserCtxt( xmlcontext
);
1069 xmlMemoryDump( ); // for debugging
1071 // Get the last producer on the stack
1072 mlt_service service
= context_pop_service( context
);
1073 if ( well_formed
&& service
!= NULL
)
1075 // Verify it is a producer service (mlt_type="mlt_producer")
1076 // (producer, playlist, multitrack)
1077 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1078 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1083 xmlDocPtr doc
= westley_make_doc( service
);
1084 xmlDocFormatDump( stdout
, doc
, 1 );
1089 if ( well_formed
&& service
!= NULL
)
1092 // Need the complete producer list for various reasons
1093 properties
= context
->destructors
;
1095 // Now make sure we don't have a reference to the service in the properties
1096 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1098 char *name
= mlt_properties_get_name( properties
, i
);
1099 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1101 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1106 // We are done referencing destructor property list
1107 // Set this var to service properties for convenience
1108 properties
= mlt_service_properties( service
);
1110 // make the returned service destroy the connected services
1111 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1113 // Now assign additional properties
1114 mlt_properties_set( properties
, "resource", filename
);
1116 // This tells consumer_westley not to deep copy
1117 mlt_properties_set( properties
, "westley", "was here" );
1121 // Return null if not well formed
1125 mlt_properties_close( context
->destructors
);
1128 free( context
->stack_service
);
1129 mlt_properties_close( context
->producer_map
);
1132 return MLT_PRODUCER( service
);