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
)
645 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties
, "mlt_service" ) ) );
647 if ( service
== NULL
&& resource
!= NULL
)
649 char *root
= mlt_properties_get( context
->producer_map
, "_root" );
650 char *full_resource
= malloc( strlen( root
) + strlen( resource
) + 1 );
651 if ( resource
[ 0 ] != '/' )
653 strcpy( full_resource
, root
);
654 strcat( full_resource
, resource
);
658 strcpy( full_resource
, resource
);
660 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource
) );
661 free( full_resource
);
663 if ( service
== NULL
)
665 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
667 // Add the producer to the producer map
668 if ( mlt_properties_get( properties
, "id" ) != NULL
)
669 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
671 // Handle in/out properties separately
672 mlt_position in
= -1;
673 mlt_position out
= -1;
676 if ( mlt_properties_get( properties
, "in" ) != NULL
)
677 in
= mlt_properties_get_position( properties
, "in" );
678 // Let Kino-SMIL clipBegin be a synonym for in
679 if ( mlt_properties_get( properties
, "clipBegin" ) != NULL
)
680 in
= mlt_properties_get_position( properties
, "clipBegin" );
682 if ( mlt_properties_get( properties
, "out" ) != NULL
)
683 out
= mlt_properties_get_position( properties
, "out" );
684 // Let Kino-SMIL clipEnd be a synonym for out
685 if ( mlt_properties_get( properties
, "clipEnd" ) != NULL
)
686 out
= mlt_properties_get_position( properties
, "clipEnd" );
689 mlt_properties_set( properties
, "in", NULL
);
690 mlt_properties_set( properties
, "out", NULL
);
692 mlt_properties_inherit( mlt_service_properties( service
), properties
);
693 mlt_properties_close( properties
);
694 context
->producer_properties
= NULL
;
696 // See if the producer should be added to a playlist or multitrack
697 if ( add_producer( context
, service
, in
, out
) == 0 )
699 // Otherwise, set in and out on...
700 if ( in
!= -1 || out
!= -1 )
702 // Get the parent service
703 mlt_service parent
= context_pop_service( context
);
704 if ( parent
!= NULL
)
706 // Get the parent properties
707 properties
= mlt_service_properties( parent
);
709 char *resource
= mlt_properties_get( properties
, "resource" );
711 // Put the parent producer back
712 context_push_service( context
, parent
);
714 // If the parent is a track or entry
715 if ( resource
&& ( strcmp( resource
, "<entry>" ) == 0 ) )
717 mlt_properties_set_position( properties
, "in", in
);
718 mlt_properties_set_position( properties
, "out", out
);
722 // Otherwise, set in and out on producer directly
723 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
728 // Otherwise, set in and out on producer directly
729 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
733 // Push the producer onto the stack
734 context_push_service( context
, service
);
738 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
740 mlt_properties properties
= context
->producer_properties
;
741 if ( properties
== NULL
)
747 mlt_service tractor
= NULL
;
749 // Get the producer from the stack
750 mlt_service producer
= context_pop_service( context
);
751 if ( producer
== NULL
)
754 // See if the producer is a tractor
755 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
756 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
758 // If so, then get the next producer
760 producer
= context_pop_service( context
);
763 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
766 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
767 if ( service
== NULL
)
769 context_push_service( context
, producer
);
772 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
774 // Connect the filter to the producer
775 mlt_filter_connect( MLT_FILTER( service
), producer
,
776 mlt_properties_get_int( properties
, "track" ) );
778 // Set in and out from producer if non existant
779 if ( mlt_properties_get( properties
, "in" ) == NULL
)
780 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
781 if ( mlt_properties_get( properties
, "out" ) == NULL
)
782 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
784 // Propogate the properties
785 mlt_properties_inherit( mlt_service_properties( service
), properties
);
786 mlt_properties_close( properties
);
787 context
->producer_properties
= NULL
;
788 properties
= mlt_service_properties( service
);
790 // Set in and out again due to inheritance
791 mlt_filter_set_in_and_out( MLT_FILTER( service
),
792 mlt_properties_get_position( properties
, "in" ),
793 mlt_properties_get_position( properties
, "out" ) );
795 // If a producer alias is in the producer_map, get it
796 snprintf( key
, 10, "%p", producer
);
797 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
798 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
800 // Put the producer in the producer map
801 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
803 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
805 // For filter chain support, add an alias to the producer map
806 snprintf( key
, 10, "%p", service
);
807 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
809 // Push the filter onto the stack
810 context_push_service( context
, service
);
812 if ( tractor
!= NULL
)
814 // Connect the tractor to the filter
815 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
817 // Push the tractor back onto the stack
818 context_push_service( context
, tractor
);
822 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
824 mlt_service tractor
= NULL
;
825 mlt_properties properties
= context
->producer_properties
;
826 if ( properties
== NULL
)
829 // Get the producer from the stack
830 mlt_service producer
= context_pop_service( context
);
831 if ( producer
== NULL
)
834 // See if the producer is a tractor
835 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
836 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
838 // If so, then get the next producer
840 producer
= context_pop_service( context
);
843 // Create the transition
844 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
845 if ( service
== NULL
)
847 context_push_service( context
, producer
);
850 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
852 // Propogate the properties
853 mlt_properties_inherit( mlt_service_properties( service
), properties
);
854 mlt_properties_close( properties
);
855 context
->producer_properties
= NULL
;
856 properties
= mlt_service_properties( service
);
858 // Set in and out again due to inheritance
859 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
860 mlt_properties_get_position( properties
, "in" ),
861 mlt_properties_get_position( properties
, "out" ) );
863 // Connect the transition to the producer
864 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
865 mlt_properties_get_int( properties
, "a_track" ),
866 mlt_properties_get_int( properties
, "b_track" ) );
868 // Push the transition onto the stack
869 context_push_service( context
, service
);
871 if ( tractor
!= NULL
)
873 // Connect the tractor to the transition
874 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
876 // Push the tractor back onto the stack
877 context_push_service( context
, tractor
);
881 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
883 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
884 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
886 //printf("on_start_element: %s\n", name );
887 context
->branch
[ context
->depth
] ++;
890 // Build a tree from nodes within a property value
891 if ( context
->is_value
== 1 )
893 xmlNodePtr node
= xmlNewNode( NULL
, name
);
895 if ( context
->value_doc
== NULL
)
898 context
->value_doc
= xmlNewDoc( "1.0" );
899 xmlDocSetRootElement( context
->value_doc
, node
);
903 // Append child to tree
904 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
906 context_push_node( context
, node
);
908 // Set the attributes
909 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
910 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
912 else if ( strcmp( name
, "tractor" ) == 0 )
913 on_start_tractor( context
, name
, atts
);
914 else if ( strcmp( name
, "multitrack" ) == 0 )
915 on_start_multitrack( context
, name
, atts
);
916 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
917 on_start_playlist( context
, name
, atts
);
918 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
919 on_start_producer( context
, name
, atts
);
920 else if ( strcmp( name
, "blank" ) == 0 )
921 on_start_blank( context
, name
, atts
);
922 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
923 on_start_entry_track( context
, name
, atts
);
924 else if ( strcmp( name
, "filter" ) == 0 )
925 on_start_filter( context
, name
, atts
);
926 else if ( strcmp( name
, "transition" ) == 0 )
927 on_start_transition( context
, name
, atts
);
928 else if ( strcmp( name
, "property" ) == 0 )
929 on_start_property( context
, name
, atts
);
932 static void on_end_element( void *ctx
, const xmlChar
*name
)
934 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
935 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
937 //printf("on_end_element: %s\n", name );
938 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
939 context_pop_node( context
);
940 else if ( strcmp( name
, "multitrack" ) == 0 )
941 on_end_multitrack( context
, name
);
942 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
943 on_end_playlist( context
, name
);
944 else if ( strcmp( name
, "track" ) == 0 )
945 on_end_track( context
, name
);
946 else if ( strcmp( name
, "entry" ) == 0 )
947 on_end_entry( context
, name
);
948 else if ( strcmp( name
, "tractor" ) == 0 )
949 on_end_tractor( context
, name
);
950 else if ( strcmp( name
, "property" ) == 0 )
951 on_end_property( context
, name
);
952 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
953 on_end_producer( context
, name
);
954 else if ( strcmp( name
, "filter" ) == 0 )
955 on_end_filter( context
, name
);
956 else if ( strcmp( name
, "transition" ) == 0 )
957 on_end_transition( context
, name
);
959 context
->branch
[ context
->depth
] = 0;
963 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
965 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
966 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
967 char *value
= calloc( len
+ 1, 1 );
970 strncpy( value
, (const char*) ch
, len
);
972 if ( context
->stack_node_size
> 0 )
973 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
975 else if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
)
976 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
981 // The following 3 facilitate entity substitution in the SAX parser
982 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
983 const xmlChar
* publicId
, const xmlChar
* systemId
)
985 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
986 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
988 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
991 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
992 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
994 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
995 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
997 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1000 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1002 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1003 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1005 return xmlGetDocEntity( context
->entity_doc
, name
);
1009 mlt_producer
producer_westley_init( char *filename
)
1011 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1012 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1013 mlt_properties properties
= NULL
;
1015 struct _xmlParserCtxt
*xmlcontext
;
1016 int well_formed
= 0;
1018 context
->producer_map
= mlt_properties_new();
1019 context
->destructors
= mlt_properties_new();
1021 // We need to track the number of registered filters
1022 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1024 // We need the directory prefix which was used for the westley
1025 mlt_properties_set( context
->producer_map
, "_root", "" );
1026 if ( strchr( filename
, '/' ) )
1029 mlt_properties_set( context
->producer_map
, "_root", filename
);
1030 root
= mlt_properties_get( context
->producer_map
, "_root" );
1031 *( strrchr( root
, '/' ) + 1 ) = '\0';
1034 // Setup SAX callbacks
1035 sax
->startElement
= on_start_element
;
1036 sax
->endElement
= on_end_element
;
1037 sax
->characters
= on_characters
;
1038 sax
->cdataBlock
= on_characters
;
1039 sax
->internalSubset
= on_internal_subset
;
1040 sax
->entityDecl
= on_entity_declaration
;
1041 sax
->getEntity
= on_get_entity
;
1043 // Setup libxml2 SAX parsing
1045 xmlSubstituteEntitiesDefault( 1 );
1046 // This is used to facilitate entity substitution in the SAX parser
1047 context
->entity_doc
= xmlNewDoc( "1.0" );
1048 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1049 xmlcontext
->sax
= sax
;
1050 xmlcontext
->_private
= ( void* )context
;
1053 xmlParseDocument( xmlcontext
);
1054 well_formed
= xmlcontext
->wellFormed
;
1056 // Cleanup after parsing
1057 xmlFreeDoc( context
->entity_doc
);
1059 xmlcontext
->sax
= NULL
;
1060 xmlcontext
->_private
= NULL
;
1061 xmlFreeParserCtxt( xmlcontext
);
1062 xmlMemoryDump( ); // for debugging
1064 // Get the last producer on the stack
1065 mlt_service service
= context_pop_service( context
);
1066 if ( well_formed
&& service
!= NULL
)
1068 // Verify it is a producer service (mlt_type="mlt_producer")
1069 // (producer, playlist, multitrack)
1070 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1071 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1076 xmlDocPtr doc
= westley_make_doc( service
);
1077 xmlDocFormatDump( stdout
, doc
, 1 );
1082 if ( well_formed
&& service
!= NULL
)
1085 // Need the complete producer list for various reasons
1086 properties
= context
->destructors
;
1088 // Now make sure we don't have a reference to the service in the properties
1089 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1091 char *name
= mlt_properties_get_name( properties
, i
);
1092 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1094 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1099 // We are done referencing destructor property list
1100 // Set this var to service properties for convenience
1101 properties
= mlt_service_properties( service
);
1103 // make the returned service destroy the connected services
1104 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1106 // Now assign additional properties
1107 mlt_properties_set( properties
, "resource", filename
);
1109 // This tells consumer_westley not to deep copy
1110 mlt_properties_set( properties
, "westley", "was here" );
1114 // Return null if not well formed
1118 mlt_properties_close( context
->destructors
);
1121 free( context
->stack_service
);
1122 mlt_properties_close( context
->producer_map
);
1125 return MLT_PRODUCER( service
);