2 * producer_westley.c -- a libxml2 parser of mlt service networks
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 // when the returned producer is closed).
24 #include "producer_westley.h"
25 #include <framework/mlt.h>
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
40 extern xmlDocPtr
westley_make_doc( mlt_service service
);
43 struct deserialise_context_s
45 mlt_service stack_service
[ STACK_SIZE
];
46 int stack_service_size
;
47 mlt_properties producer_map
;
48 mlt_properties destructors
;
50 mlt_properties producer_properties
;
53 xmlNodePtr stack_node
[ STACK_SIZE
];
56 int entity_is_replace
;
58 int branch
[ STACK_SIZE
];
59 const xmlChar
*publicId
;
60 const xmlChar
*systemId
;
61 mlt_properties params
;
62 mlt_deque filter_queue
;
65 typedef struct deserialise_context_s
*deserialise_context
;
67 /** Convert the numerical current branch address to a dot-delimited string.
69 static char *serialise_branch( deserialise_context
this, char *s
)
74 for ( i
= 0; i
< this->depth
; i
++ )
76 int len
= strlen( s
);
77 snprintf( s
+ len
, BRANCH_SIG_LEN
- len
, "%d.", this->branch
[ i
] );
85 static int context_push_service( deserialise_context
this, mlt_service that
)
87 int ret
= this->stack_service_size
>= STACK_SIZE
- 1;
90 this->stack_service
[ this->stack_service_size
++ ] = that
;
92 // Record the tree branch on which this service lives
93 if ( mlt_properties_get( mlt_service_properties( that
), "_westley_branch" ) == NULL
)
95 char s
[ BRANCH_SIG_LEN
];
96 mlt_properties_set( mlt_service_properties( that
), "_westley_branch", serialise_branch( this, s
) );
105 static mlt_service
context_pop_service( deserialise_context
this )
107 mlt_service result
= NULL
;
108 if ( this->stack_service_size
> 0 )
109 result
= this->stack_service
[ -- this->stack_service_size
];
116 static int context_push_node( deserialise_context
this, xmlNodePtr node
)
118 int ret
= this->stack_node_size
>= STACK_SIZE
- 1;
120 this->stack_node
[ this->stack_node_size
++ ] = node
;
127 static xmlNodePtr
context_pop_node( deserialise_context
this )
129 xmlNodePtr result
= NULL
;
130 if ( this->stack_node_size
> 0 )
131 result
= this->stack_node
[ -- this->stack_node_size
];
136 // Set the destructor on a new service
137 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
139 int registered
= mlt_properties_get_int( properties
, "registered" );
140 char *key
= mlt_properties_get( properties
, "registered" );
141 mlt_properties_set_data( properties
, key
, service
, 0, destructor
, NULL
);
142 mlt_properties_set_int( properties
, "registered", ++ registered
);
146 // Prepend the property value with the document root
147 static inline void qualify_property( deserialise_context context
, mlt_properties properties
, char *name
)
149 char *resource
= mlt_properties_get( properties
, name
);
150 if ( resource
!= NULL
)
152 // Qualify file name properties
153 char *root
= mlt_properties_get( context
->producer_map
, "_root" );
156 char *full_resource
= malloc( strlen( root
) + strlen( resource
) + 1 );
157 if ( resource
[ 0 ] != '/' )
159 strcpy( full_resource
, root
);
160 strcat( full_resource
, resource
);
164 strcpy( full_resource
, resource
);
166 mlt_properties_set( properties
, name
, full_resource
);
167 free( full_resource
);
173 // Forward declarations
174 static void on_end_track( deserialise_context context
, const xmlChar
*name
);
175 static void on_end_entry( deserialise_context context
, const xmlChar
*name
);
177 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
179 mlt_service service
= mlt_tractor_service( mlt_tractor_init() );
180 mlt_properties properties
= mlt_service_properties( service
);
182 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
184 mlt_properties_set_position( properties
, "length", 0 );
186 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
187 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
189 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
191 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
192 mlt_properties_set_position( properties
, "length", length
);
195 if ( mlt_properties_get( properties
, "id" ) != NULL
)
196 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
198 context_push_service( context
, service
);
201 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
203 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
204 mlt_properties properties
= mlt_service_properties( service
);
206 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
208 mlt_properties_set_position( properties
, "length", 0 );
210 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
211 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
213 if ( mlt_properties_get( properties
, "id" ) != NULL
)
214 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
216 context_push_service( context
, service
);
219 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
221 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
222 mlt_properties properties
= mlt_service_properties( service
);
224 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
226 mlt_properties_set_position( properties
, "length", 0 );
228 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
230 mlt_properties_set( properties
, ( char* )atts
[0], ( char* )atts
[1] );
232 // Out will be overwritten later as we append, so we need to save it
233 if ( strcmp( atts
[ 0 ], "out" ) == 0 )
234 mlt_properties_set( properties
, "_westley.out", ( char* )atts
[ 1 ] );
237 if ( mlt_properties_get( properties
, "id" ) != NULL
)
238 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
240 context_push_service( context
, service
);
243 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
245 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
247 context
->in_producer
++;
249 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
251 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
255 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
257 // Get the playlist from the stack
258 mlt_service service
= context_pop_service( context
);
259 mlt_position length
= 0;
261 if ( service
== NULL
)
264 // Look for the length attribute
265 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
267 if ( strcmp( atts
[0], "length" ) == 0 )
269 length
= atoll( atts
[1] );
274 // Append a blank to the playlist
275 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
277 // Push the playlist back onto the stack
278 context_push_service( context
, service
);
281 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
283 // Use a dummy service to hold properties to allow arbitrary nesting
284 mlt_service service
= calloc( 1, sizeof( struct mlt_service_s
) );
285 mlt_service_init( service
, NULL
);
287 // Push the dummy service onto the stack
288 context_push_service( context
, service
);
290 if ( strcmp( name
, "entry" ) == 0 )
291 mlt_properties_set( mlt_service_properties( service
), "resource", "<entry>" );
293 mlt_properties_set( mlt_service_properties( service
), "resource", "<track>" );
295 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
297 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
299 // Look for the producer attribute
300 if ( strcmp( atts
[ 0 ], "producer" ) == 0 )
302 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
303 // Push the referenced producer onto the stack
304 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
306 // Remove the dummy service to cause end element failure
307 context_pop_service( context
);
312 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
314 mlt_properties properties
= mlt_properties_new();
316 if ( context
->in_producer
!= 0 )
317 mlt_deque_push_front( context
->filter_queue
, context
->producer_properties
);
319 context
->producer_properties
= properties
;
321 // Set the properties
322 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
323 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
326 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
328 mlt_properties properties
= context
->producer_properties
= mlt_properties_new();
330 // Set the properties
331 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
332 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
335 static void on_start_property( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
337 mlt_properties properties
= context
->producer_properties
;
340 if ( properties
== NULL
)
343 // Set the properties
344 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
346 if ( strcmp( atts
[ 0 ], "name" ) == 0 )
348 context
->property
= strdup( atts
[ 1 ] );
350 else if ( strcmp( atts
[ 0 ], "value" ) == 0 )
352 value
= (char*) atts
[ 1 ];
356 if ( context
->property
!= NULL
&& value
!= NULL
)
357 mlt_properties_set( properties
, context
->property
, value
);
359 // Tell parser to collect any further nodes for serialisation
360 context
->is_value
= 1;
364 /** This function adds a producer to a playlist or multitrack when
365 there is no entry or track element.
368 static int add_producer( deserialise_context context
, mlt_service service
, mlt_position in
, mlt_position out
)
370 // Get the parent producer
371 mlt_service producer
= context_pop_service( context
);
373 if ( producer
!= NULL
)
375 char current_branch
[ BRANCH_SIG_LEN
];
376 char *service_branch
= mlt_properties_get( mlt_service_properties( producer
), "_westley_branch" );
378 // Validate the producer from the stack is an ancestor and not predecessor
379 serialise_branch( context
, current_branch
);
380 if ( service_branch
!= NULL
&& strncmp( service_branch
, current_branch
, strlen( service_branch
) ) == 0 )
382 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
384 // Put the parent producer back
385 context_push_service( context
, producer
);
387 // If the parent producer is a multitrack or playlist (not a track or entry)
388 if ( resource
&& ( strcmp( resource
, "<playlist>" ) == 0 ||
389 strcmp( resource
, "<multitrack>" ) == 0 ) )
391 //printf( "add_producer: current branch %s service branch %s (%d)\n", current_branch, service_branch, strncmp( service_branch, current_branch, strlen( service_branch ) ) );
392 if ( strcmp( resource
, "<playlist>" ) == 0 )
394 // Append this producer to the playlist
395 mlt_playlist_append_io( MLT_PLAYLIST( producer
),
396 MLT_PRODUCER( service
), in
, out
);
400 mlt_properties properties
= mlt_service_properties( service
);
402 // Set this producer on the multitrack
403 mlt_multitrack_connect( MLT_MULTITRACK( producer
),
404 MLT_PRODUCER( service
),
405 mlt_multitrack_count( MLT_MULTITRACK( producer
) ) );
407 // Set the hide state of the track producer
408 char *hide_s
= mlt_properties_get( properties
, "hide" );
409 if ( hide_s
!= NULL
)
411 if ( strcmp( hide_s
, "video" ) == 0 )
412 mlt_properties_set_int( properties
, "hide", 1 );
413 else if ( strcmp( hide_s
, "audio" ) == 0 )
414 mlt_properties_set_int( properties
, "hide", 2 );
415 else if ( strcmp( hide_s
, "both" ) == 0 )
416 mlt_properties_set_int( properties
, "hide", 3 );
420 // Normally, the enclosing entry or track will pop this service off
421 // In its absence we do not push it on.
429 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
431 // Get the multitrack from the stack
432 mlt_service producer
= context_pop_service( context
);
433 if ( producer
== NULL
)
436 // Get the tractor from the stack
437 mlt_service service
= context_pop_service( context
);
439 // Create a tractor if one does not exist
440 char *resource
= NULL
;
441 if ( service
!= NULL
)
442 resource
= mlt_properties_get( mlt_service_properties( service
), "resource" );
443 if ( service
== NULL
|| resource
== NULL
|| strcmp( resource
, "<tractor>" ) )
445 //printf("creating a tractor\n");
446 char current_branch
[ BRANCH_SIG_LEN
];
448 // Put the anonymous service back onto the stack!
449 if ( service
!= NULL
)
450 context_push_service( context
, service
);
452 // Fabricate the tractor
453 service
= mlt_tractor_service( mlt_tractor_init() );
454 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
456 // Inherit the producer's properties
457 mlt_properties properties
= mlt_service_properties( service
);
458 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( MLT_PRODUCER( producer
) ) + 1 );
459 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
460 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer
) ) );
462 mlt_properties_set( properties
, "_westley_branch", serialise_branch( context
, current_branch
) );
465 // Connect the tractor to the multitrack
466 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
467 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
468 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
470 // See if the tractor should be added to a playlist or multitrack
471 add_producer( context
, service
, 0, mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
473 // Always push the multitrack back onto the stack for filters and transitions
474 context_push_service( context
, producer
);
476 // Always push the tractor back onto the stack for filters and transitions
477 context_push_service( context
, service
);
480 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
482 // Get the playlist from the stack
483 mlt_service producer
= context_pop_service( context
);
484 if ( producer
== NULL
)
486 mlt_properties properties
= mlt_service_properties( producer
);
488 mlt_position in
= mlt_properties_get_position( properties
, "in" );
491 if ( mlt_properties_get( properties
, "_westley.out" ) != NULL
)
492 out
= mlt_properties_get_position( properties
, "_westley.out" );
494 out
= mlt_properties_get_position( properties
, "length" ) - 1;
496 if ( mlt_properties_get_position( properties
, "length" ) < out
)
497 mlt_properties_set_position( properties
, "length", out
+ 1 );
499 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
), in
, out
);
501 // See if the playlist should be added to a playlist or multitrack
502 if ( add_producer( context
, producer
, in
, out
) == 0 )
504 // Otherwise, push the playlist back onto the stack
505 context_push_service( context
, producer
);
508 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
510 // Get the producer from the stack
511 mlt_service producer
= context_pop_service( context
);
512 if ( producer
== NULL
)
514 mlt_properties producer_props
= mlt_service_properties( producer
);
516 // See if the producer is a tractor
517 char *resource
= mlt_properties_get( producer_props
, "resource" );
518 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
519 // If so chomp its producer
520 context_pop_service( context
);
522 // Get the dummy track service from the stack
523 mlt_service track
= context_pop_service( context
);
524 if ( track
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( track
), "resource" ), "<track>" ) )
526 context_push_service( context
, producer
);
529 mlt_properties track_props
= mlt_service_properties( track
);
531 // Get the multitrack from the stack
532 mlt_service service
= context_pop_service( context
);
533 if ( service
== NULL
)
535 context_push_service( context
, producer
);
539 // Set the track on the multitrack
540 mlt_multitrack_connect( MLT_MULTITRACK( service
),
541 MLT_PRODUCER( producer
),
542 mlt_multitrack_count( MLT_MULTITRACK( service
) ) );
544 // Set producer i/o if specified
545 if ( mlt_properties_get( track_props
, "in" ) != NULL
||
546 mlt_properties_get( track_props
, "out" ) != NULL
)
548 mlt_producer_set_in_and_out( MLT_PRODUCER( producer
),
549 mlt_properties_get_position( track_props
, "in" ),
550 mlt_properties_get_position( track_props
, "out" ) );
553 // Set the hide state of the track producer
554 char *hide_s
= mlt_properties_get( track_props
, "hide" );
555 if ( hide_s
!= NULL
)
557 if ( strcmp( hide_s
, "video" ) == 0 )
558 mlt_properties_set_int( producer_props
, "hide", 1 );
559 else if ( strcmp( hide_s
, "audio" ) == 0 )
560 mlt_properties_set_int( producer_props
, "hide", 2 );
561 else if ( strcmp( hide_s
, "both" ) == 0 )
562 mlt_properties_set_int( producer_props
, "hide", 3 );
565 // Push the multitrack back onto the stack
566 context_push_service( context
, service
);
568 mlt_service_close( track
);
571 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
573 // Get the producer from the stack
574 mlt_service producer
= context_pop_service( context
);
575 if ( producer
== NULL
)
578 // See if the producer is a tractor
579 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
580 if ( resource
&& strcmp( resource
, "<tractor>" ) == 0 )
581 // If so chomp its producer
582 context_pop_service( context
);
584 // Get the dummy entry service from the stack
585 mlt_service entry
= context_pop_service( context
);
586 if ( entry
== NULL
|| strcmp( mlt_properties_get( mlt_service_properties( entry
), "resource" ), "<entry>" ) )
588 context_push_service( context
, producer
);
592 // Get the playlist from the stack
593 mlt_service service
= context_pop_service( context
);
594 if ( service
== NULL
)
596 context_push_service( context
, producer
);
600 // Append the producer to the playlist
601 if ( mlt_properties_get( mlt_service_properties( entry
), "in" ) != NULL
||
602 mlt_properties_get( mlt_service_properties( entry
), "out" ) != NULL
)
604 mlt_playlist_append_io( MLT_PLAYLIST( service
),
605 MLT_PRODUCER( producer
),
606 mlt_properties_get_position( mlt_service_properties( entry
), "in" ),
607 mlt_properties_get_position( mlt_service_properties( entry
), "out" ) );
611 mlt_playlist_append( MLT_PLAYLIST( service
), MLT_PRODUCER( producer
) );
614 // Push the playlist back onto the stack
615 context_push_service( context
, service
);
617 mlt_service_close( entry
);
620 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
623 mlt_service tractor
= context_pop_service( context
);
624 if ( tractor
== NULL
)
627 // Get the tractor's multitrack
628 mlt_producer multitrack
= mlt_properties_get_data( mlt_service_properties( tractor
), "multitrack", NULL
);
629 if ( multitrack
!= NULL
)
631 // Inherit the producer's properties
632 mlt_properties properties
= mlt_producer_properties( MLT_PRODUCER( tractor
) );
633 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
634 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
635 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
638 // See if the tractor should be added to a playlist or multitrack
639 if ( add_producer( context
, tractor
, 0, mlt_producer_get_out( MLT_PRODUCER( tractor
) ) ) == 0 )
641 // Otherwise, push the tractor back onto the stack
642 context_push_service( context
, tractor
);
645 static void on_end_property( deserialise_context context
, const xmlChar
*name
)
647 // Tell parser to stop building a tree
648 context
->is_value
= 0;
650 // See if there is a xml tree for the value
651 if ( context
->property
!= NULL
&& context
->value_doc
!= NULL
)
656 // Serialise the tree to get value
657 xmlDocDumpMemory( context
->value_doc
, &value
, &size
);
658 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
660 xmlFreeDoc( context
->value_doc
);
661 context
->value_doc
= NULL
;
664 // Close this property handling
665 free( context
->property
);
666 context
->property
= NULL
;
669 static void on_end_producer( deserialise_context context
, const xmlChar
*name
)
671 mlt_properties properties
= context
->producer_properties
;
672 mlt_service service
= NULL
;
674 context
->in_producer
--;
676 if ( properties
== NULL
)
679 qualify_property( context
, properties
, "resource" );
680 char *resource
= mlt_properties_get( properties
, "resource" );
681 // Let Kino-SMIL src be a synonym for resource
682 if ( resource
== NULL
)
684 qualify_property( context
, properties
, "src" );
685 resource
= mlt_properties_get( properties
, "src" );
688 // Instantiate the producer
689 if ( mlt_properties_get( properties
, "mlt_service" ) != NULL
)
692 strncpy( temp
, mlt_properties_get( properties
, "mlt_service" ), 1024 );
693 if ( resource
!= NULL
)
696 strncat( temp
, resource
, 1023 - strlen( temp
) );
698 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", temp
) );
700 if ( service
== NULL
&& resource
!= NULL
)
702 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", resource
) );
705 if ( service
== NULL
)
707 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
709 // Add the producer to the producer map
710 if ( mlt_properties_get( properties
, "id" ) != NULL
)
711 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
713 // Handle in/out properties separately
714 mlt_position in
= -1;
715 mlt_position out
= -1;
718 if ( mlt_properties_get( properties
, "in" ) != NULL
)
719 in
= mlt_properties_get_position( properties
, "in" );
720 // Let Kino-SMIL clipBegin be a synonym for in
721 if ( mlt_properties_get( properties
, "clipBegin" ) != NULL
)
722 in
= mlt_properties_get_position( properties
, "clipBegin" );
724 if ( mlt_properties_get( properties
, "out" ) != NULL
)
725 out
= mlt_properties_get_position( properties
, "out" );
726 // Let Kino-SMIL clipEnd be a synonym for out
727 if ( mlt_properties_get( properties
, "clipEnd" ) != NULL
)
728 out
= mlt_properties_get_position( properties
, "clipEnd" );
731 mlt_properties_set( properties
, "in", NULL
);
732 mlt_properties_set( properties
, "out", NULL
);
734 mlt_properties_inherit( mlt_service_properties( service
), properties
);
735 mlt_properties_close( properties
);
736 context
->producer_properties
= NULL
;
738 // See if the producer should be added to a playlist or multitrack
739 if ( add_producer( context
, service
, in
, out
) == 0 )
741 // Otherwise, set in and out on...
742 if ( in
!= -1 || out
!= -1 )
744 // Get the parent service
745 mlt_service parent
= context_pop_service( context
);
746 if ( parent
!= NULL
)
748 // Get the parent properties
749 properties
= mlt_service_properties( parent
);
751 char *resource
= mlt_properties_get( properties
, "resource" );
753 // Put the parent producer back
754 context_push_service( context
, parent
);
756 // If the parent is a track or entry
757 if ( resource
&& ( strcmp( resource
, "<entry>" ) == 0 ) )
759 mlt_properties_set_position( properties
, "in", in
);
760 mlt_properties_set_position( properties
, "out", out
);
764 // Otherwise, set in and out on producer directly
765 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
770 // Otherwise, set in and out on producer directly
771 mlt_producer_set_in_and_out( MLT_PRODUCER( service
), in
, out
);
775 // Allow for embedded filters
776 while( mlt_deque_count( context
->filter_queue
) )
778 mlt_properties filter_properties
= mlt_deque_pop_front( context
->filter_queue
);
779 mlt_filter filter
= mlt_factory_filter( mlt_properties_get( filter_properties
, "mlt_service" ), NULL
);
780 if ( filter
!= NULL
)
782 track_service( context
->destructors
, filter
, (mlt_destructor
) mlt_filter_close
);
783 qualify_property( context
, filter_properties
, "resource" );
784 qualify_property( context
, filter_properties
, "luma" );
785 qualify_property( context
, filter_properties
, "luma.resource" );
786 qualify_property( context
, filter_properties
, "composite.luma" );
787 qualify_property( context
, filter_properties
, "producer.resource" );
788 mlt_properties_inherit( mlt_filter_properties( filter
), filter_properties
);
789 mlt_properties_close( filter_properties
);
790 mlt_producer_attach( MLT_PRODUCER( service
), filter
);
794 // Push the producer onto the stack
795 context_push_service( context
, service
);
799 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
801 mlt_properties properties
= context
->producer_properties
;
802 if ( context
->in_producer
)
804 mlt_deque_push_back( context
->filter_queue
, properties
);
805 context
->producer_properties
= mlt_deque_pop_front( context
->filter_queue
);
809 if ( properties
== NULL
)
815 mlt_service tractor
= NULL
;
817 // Get the producer from the stack
818 mlt_service producer
= context_pop_service( context
);
819 if ( producer
== NULL
)
822 // See if the producer is a tractor
823 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
824 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
826 // If so, then get the next producer
828 producer
= context_pop_service( context
);
831 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
834 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
835 if ( service
== NULL
)
837 context_push_service( context
, producer
);
840 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
842 // Connect the filter to the producer
843 mlt_filter_connect( MLT_FILTER( service
), producer
,
844 mlt_properties_get_int( properties
, "track" ) );
846 // Set in and out from producer if non existant
847 if ( mlt_properties_get( properties
, "in" ) == NULL
)
848 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
849 if ( mlt_properties_get( properties
, "out" ) == NULL
)
850 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
852 // Propogate the properties
853 qualify_property( context
, properties
, "resource" );
854 qualify_property( context
, properties
, "luma" );
855 qualify_property( context
, properties
, "luma.resource" );
856 qualify_property( context
, properties
, "composite.luma" );
857 qualify_property( context
, properties
, "producer.resource" );
858 mlt_properties_inherit( mlt_service_properties( service
), properties
);
859 mlt_properties_close( properties
);
860 context
->producer_properties
= NULL
;
861 properties
= mlt_service_properties( service
);
863 // Set in and out again due to inheritance
864 mlt_filter_set_in_and_out( MLT_FILTER( service
),
865 mlt_properties_get_position( properties
, "in" ),
866 mlt_properties_get_position( properties
, "out" ) );
868 // If a producer alias is in the producer_map, get it
869 snprintf( key
, 10, "%p", producer
);
870 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
871 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
873 // Put the producer in the producer map
874 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
876 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
878 // For filter chain support, add an alias to the producer map
879 snprintf( key
, 10, "%p", service
);
880 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
882 // Push the filter onto the stack
883 context_push_service( context
, service
);
885 if ( tractor
!= NULL
)
887 // Connect the tractor to the filter
888 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
890 // Push the tractor back onto the stack
891 context_push_service( context
, tractor
);
895 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
897 mlt_service tractor
= NULL
;
898 mlt_properties properties
= context
->producer_properties
;
899 if ( properties
== NULL
)
902 // Get the producer from the stack
903 mlt_service producer
= context_pop_service( context
);
904 if ( producer
== NULL
)
907 // See if the producer is a tractor
908 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
909 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
911 // If so, then get the next producer
913 producer
= context_pop_service( context
);
916 // Create the transition
917 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
918 if ( service
== NULL
)
920 context_push_service( context
, producer
);
923 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
925 // Propogate the properties
926 qualify_property( context
, properties
, "resource" );
927 qualify_property( context
, properties
, "luma" );
928 qualify_property( context
, properties
, "luma.resource" );
929 qualify_property( context
, properties
, "composite.luma" );
930 qualify_property( context
, properties
, "producer.resource" );
931 mlt_properties_inherit( mlt_service_properties( service
), properties
);
932 mlt_properties_close( properties
);
933 context
->producer_properties
= NULL
;
934 properties
= mlt_service_properties( service
);
936 // Set in and out again due to inheritance
937 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
938 mlt_properties_get_position( properties
, "in" ),
939 mlt_properties_get_position( properties
, "out" ) );
941 // Connect the transition to the producer
942 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
943 mlt_properties_get_int( properties
, "a_track" ),
944 mlt_properties_get_int( properties
, "b_track" ) );
946 // Push the transition onto the stack
947 context_push_service( context
, service
);
949 if ( tractor
!= NULL
)
951 // Connect the tractor to the transition
952 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
954 // Push the tractor back onto the stack
955 context_push_service( context
, tractor
);
959 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
961 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
962 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
964 //printf("on_start_element: %s\n", name );
965 context
->branch
[ context
->depth
] ++;
968 // Build a tree from nodes within a property value
969 if ( context
->is_value
== 1 )
971 xmlNodePtr node
= xmlNewNode( NULL
, name
);
973 if ( context
->value_doc
== NULL
)
976 context
->value_doc
= xmlNewDoc( "1.0" );
977 xmlDocSetRootElement( context
->value_doc
, node
);
981 // Append child to tree
982 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
984 context_push_node( context
, node
);
986 // Set the attributes
987 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
988 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
990 else if ( strcmp( name
, "tractor" ) == 0 )
991 on_start_tractor( context
, name
, atts
);
992 else if ( strcmp( name
, "multitrack" ) == 0 )
993 on_start_multitrack( context
, name
, atts
);
994 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
995 on_start_playlist( context
, name
, atts
);
996 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
997 on_start_producer( context
, name
, atts
);
998 else if ( strcmp( name
, "blank" ) == 0 )
999 on_start_blank( context
, name
, atts
);
1000 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
1001 on_start_entry_track( context
, name
, atts
);
1002 else if ( strcmp( name
, "filter" ) == 0 )
1003 on_start_filter( context
, name
, atts
);
1004 else if ( strcmp( name
, "transition" ) == 0 )
1005 on_start_transition( context
, name
, atts
);
1006 else if ( strcmp( name
, "property" ) == 0 )
1007 on_start_property( context
, name
, atts
);
1010 static void on_end_element( void *ctx
, const xmlChar
*name
)
1012 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1013 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1015 //printf("on_end_element: %s\n", name );
1016 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
1017 context_pop_node( context
);
1018 else if ( strcmp( name
, "multitrack" ) == 0 )
1019 on_end_multitrack( context
, name
);
1020 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
1021 on_end_playlist( context
, name
);
1022 else if ( strcmp( name
, "track" ) == 0 )
1023 on_end_track( context
, name
);
1024 else if ( strcmp( name
, "entry" ) == 0 )
1025 on_end_entry( context
, name
);
1026 else if ( strcmp( name
, "tractor" ) == 0 )
1027 on_end_tractor( context
, name
);
1028 else if ( strcmp( name
, "property" ) == 0 )
1029 on_end_property( context
, name
);
1030 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
1031 on_end_producer( context
, name
);
1032 else if ( strcmp( name
, "filter" ) == 0 )
1033 on_end_filter( context
, name
);
1034 else if ( strcmp( name
, "transition" ) == 0 )
1035 on_end_transition( context
, name
);
1037 context
->branch
[ context
->depth
] = 0;
1041 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
1043 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1044 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1045 char *value
= calloc( len
+ 1, 1 );
1048 strncpy( value
, (const char*) ch
, len
);
1050 if ( context
->stack_node_size
> 0 )
1051 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
1053 // libxml2 generates an on_characters immediately after a get_entity within
1054 // an element value, and we ignore it because it is called again during
1055 // actual substitution.
1056 else if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
1057 && context
->entity_is_replace
== 0 )
1059 char *s
= mlt_properties_get( context
->producer_properties
, context
->property
);
1062 // Append new text to existing content
1063 char *new = calloc( strlen( s
) + len
+ 1, 1 );
1065 strcat( new, value
);
1066 mlt_properties_set( context
->producer_properties
, context
->property
, new );
1070 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
1072 context
->entity_is_replace
= 0;
1077 /** Convert parameters parsed from resource into entity declarations.
1079 static void params_to_entities( deserialise_context context
)
1081 if ( context
->params
!= NULL
)
1085 // Add our params as entitiy declarations
1086 for ( i
= 0; i
< mlt_properties_count( context
->params
); i
++ )
1088 xmlChar
*name
= ( xmlChar
* )mlt_properties_get_name( context
->params
, i
);
1089 xmlAddDocEntity( context
->entity_doc
, name
, XML_INTERNAL_GENERAL_ENTITY
,
1090 context
->publicId
, context
->systemId
, ( xmlChar
* )mlt_properties_get( context
->params
, name
) );
1094 mlt_properties_close( context
->params
);
1095 context
->params
= NULL
;
1099 // The following 3 facilitate entity substitution in the SAX parser
1100 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
1101 const xmlChar
* publicId
, const xmlChar
* systemId
)
1103 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1104 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1106 context
->publicId
= publicId
;
1107 context
->systemId
= systemId
;
1108 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
1110 // Override default entities with our parameters
1111 params_to_entities( context
);
1114 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
1115 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
1117 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1118 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1120 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1123 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1125 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1126 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1127 xmlEntityPtr e
= NULL
;
1129 // Setup for entity declarations if not ready
1130 if ( xmlGetIntSubset( context
->entity_doc
) == NULL
)
1132 xmlCreateIntSubset( context
->entity_doc
, "westley", "", "" );
1133 context
->publicId
= "";
1134 context
->systemId
= "";
1137 // Add our parameters if not already
1138 params_to_entities( context
);
1140 e
= xmlGetDocEntity( context
->entity_doc
, name
);
1142 // Send signal to on_characters that an entity substitutin is pending
1144 context
->entity_is_replace
= 1;
1149 /** Convert a hexadecimal character to its value.
1151 static int tohex( char p
)
1153 return isdigit( p
) ? p
- '0' : tolower( p
) - 'a' + 10;
1156 /** Decode a url-encoded string containing hexadecimal character sequences.
1158 static char *url_decode( char *dest
, char *src
)
1166 *p
++ = ( tohex( *( src
+ 1 ) ) << 4 ) | tohex( *( src
+ 2 ) );
1179 /** Extract the filename from a URL attaching parameters to a properties list.
1181 static void parse_url( mlt_properties properties
, char *url
)
1184 int n
= strlen( url
);
1188 for ( i
= 0; i
< n
; i
++ )
1205 if ( name
!= NULL
&& value
!= NULL
)
1206 mlt_properties_set( properties
, name
, value
);
1212 if ( name
!= NULL
&& value
!= NULL
)
1213 mlt_properties_set( properties
, name
, value
);
1216 mlt_producer
producer_westley_init( char *url
)
1220 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1221 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1222 mlt_properties properties
= NULL
;
1224 struct _xmlParserCtxt
*xmlcontext
;
1225 int well_formed
= 0;
1226 char *filename
= strdup( url
);
1228 context
->producer_map
= mlt_properties_new();
1229 context
->destructors
= mlt_properties_new();
1230 context
->params
= mlt_properties_new();
1231 context
->filter_queue
= mlt_deque_init();
1233 // Decode URL and parse parameters
1234 parse_url( context
->params
, url_decode( filename
, url
) );
1236 // We need to track the number of registered filters
1237 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1239 // We need the directory prefix which was used for the westley
1240 mlt_properties_set( context
->producer_map
, "_root", "" );
1241 if ( strchr( filename
, '/' ) )
1244 mlt_properties_set( context
->producer_map
, "_root", filename
);
1245 root
= mlt_properties_get( context
->producer_map
, "_root" );
1246 *( strrchr( root
, '/' ) + 1 ) = '\0';
1249 // Setup SAX callbacks
1250 sax
->startElement
= on_start_element
;
1251 sax
->endElement
= on_end_element
;
1252 sax
->characters
= on_characters
;
1253 sax
->cdataBlock
= on_characters
;
1254 sax
->internalSubset
= on_internal_subset
;
1255 sax
->entityDecl
= on_entity_declaration
;
1256 sax
->getEntity
= on_get_entity
;
1258 // Setup libxml2 SAX parsing
1260 xmlSubstituteEntitiesDefault( 1 );
1261 // This is used to facilitate entity substitution in the SAX parser
1262 context
->entity_doc
= xmlNewDoc( "1.0" );
1263 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1264 xmlcontext
->sax
= sax
;
1265 xmlcontext
->_private
= ( void* )context
;
1268 xmlParseDocument( xmlcontext
);
1269 well_formed
= xmlcontext
->wellFormed
;
1271 // Cleanup after parsing
1272 xmlFreeDoc( context
->entity_doc
);
1274 xmlcontext
->sax
= NULL
;
1275 xmlcontext
->_private
= NULL
;
1276 xmlFreeParserCtxt( xmlcontext
);
1277 xmlMemoryDump( ); // for debugging
1279 // Get the last producer on the stack
1280 mlt_service service
= context_pop_service( context
);
1281 if ( well_formed
&& service
!= NULL
)
1283 // Verify it is a producer service (mlt_type="mlt_producer")
1284 // (producer, playlist, multitrack)
1285 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1286 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1291 xmlDocPtr doc
= westley_make_doc( service
);
1292 xmlDocFormatDump( stdout
, doc
, 1 );
1297 if ( well_formed
&& service
!= NULL
)
1300 // Need the complete producer list for various reasons
1301 properties
= context
->destructors
;
1303 // Now make sure we don't have a reference to the service in the properties
1304 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1306 char *name
= mlt_properties_get_name( properties
, i
);
1307 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1309 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1314 // We are done referencing destructor property list
1315 // Set this var to service properties for convenience
1316 properties
= mlt_service_properties( service
);
1318 // make the returned service destroy the connected services
1319 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1321 // Now assign additional properties
1322 mlt_properties_set( properties
, "resource", url
);
1324 // This tells consumer_westley not to deep copy
1325 mlt_properties_set( properties
, "westley", "was here" );
1329 // Return null if not well formed
1333 mlt_properties_close( context
->destructors
);
1337 mlt_properties_close( context
->producer_map
);
1338 if ( context
->params
!= NULL
)
1339 mlt_properties_close( context
->params
);
1340 mlt_deque_close( context
->filter_queue
);
1344 return MLT_PRODUCER( service
);