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_properties_debug( filter_properties
, "Filter?", stderr
);
780 mlt_filter filter
= mlt_factory_filter( mlt_properties_get( filter_properties
, "mlt_service" ), NULL
);
781 if ( filter
!= NULL
)
783 track_service( context
->destructors
, filter
, (mlt_destructor
) mlt_filter_close
);
784 qualify_property( context
, filter_properties
, "resource" );
785 qualify_property( context
, filter_properties
, "luma" );
786 qualify_property( context
, filter_properties
, "luma.resource" );
787 qualify_property( context
, filter_properties
, "composite.luma" );
788 qualify_property( context
, filter_properties
, "producer.resource" );
789 mlt_properties_inherit( mlt_filter_properties( filter
), filter_properties
);
790 mlt_properties_close( filter_properties
);
791 mlt_producer_attach( MLT_PRODUCER( service
), filter
);
795 // Push the producer onto the stack
796 context_push_service( context
, service
);
800 static void on_end_filter( deserialise_context context
, const xmlChar
*name
)
802 mlt_properties properties
= context
->producer_properties
;
803 if ( context
->in_producer
)
805 mlt_deque_push_back( context
->filter_queue
, properties
);
806 context
->producer_properties
= mlt_deque_pop_front( context
->filter_queue
);
810 if ( properties
== NULL
)
816 mlt_service tractor
= NULL
;
818 // Get the producer from the stack
819 mlt_service producer
= context_pop_service( context
);
820 if ( producer
== NULL
)
823 // See if the producer is a tractor
824 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
825 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
827 // If so, then get the next producer
829 producer
= context_pop_service( context
);
832 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
835 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
836 if ( service
== NULL
)
838 context_push_service( context
, producer
);
841 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
843 // Connect the filter to the producer
844 mlt_filter_connect( MLT_FILTER( service
), producer
,
845 mlt_properties_get_int( properties
, "track" ) );
847 // Set in and out from producer if non existant
848 if ( mlt_properties_get( properties
, "in" ) == NULL
)
849 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
850 if ( mlt_properties_get( properties
, "out" ) == NULL
)
851 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
853 // Propogate the properties
854 qualify_property( context
, properties
, "resource" );
855 qualify_property( context
, properties
, "luma" );
856 qualify_property( context
, properties
, "luma.resource" );
857 qualify_property( context
, properties
, "composite.luma" );
858 qualify_property( context
, properties
, "producer.resource" );
859 mlt_properties_inherit( mlt_service_properties( service
), properties
);
860 mlt_properties_close( properties
);
861 context
->producer_properties
= NULL
;
862 properties
= mlt_service_properties( service
);
864 // Set in and out again due to inheritance
865 mlt_filter_set_in_and_out( MLT_FILTER( service
),
866 mlt_properties_get_position( properties
, "in" ),
867 mlt_properties_get_position( properties
, "out" ) );
869 // If a producer alias is in the producer_map, get it
870 snprintf( key
, 10, "%p", producer
);
871 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
872 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
874 // Put the producer in the producer map
875 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
877 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
879 // For filter chain support, add an alias to the producer map
880 snprintf( key
, 10, "%p", service
);
881 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
883 // Push the filter onto the stack
884 context_push_service( context
, service
);
886 if ( tractor
!= NULL
)
888 // Connect the tractor to the filter
889 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
891 // Push the tractor back onto the stack
892 context_push_service( context
, tractor
);
896 static void on_end_transition( deserialise_context context
, const xmlChar
*name
)
898 mlt_service tractor
= NULL
;
899 mlt_properties properties
= context
->producer_properties
;
900 if ( properties
== NULL
)
903 // Get the producer from the stack
904 mlt_service producer
= context_pop_service( context
);
905 if ( producer
== NULL
)
908 // See if the producer is a tractor
909 char *resource
= mlt_properties_get( mlt_service_properties( producer
), "resource" );
910 if ( resource
!= NULL
&& strcmp( resource
, "<tractor>" ) == 0 )
912 // If so, then get the next producer
914 producer
= context_pop_service( context
);
917 // Create the transition
918 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
919 if ( service
== NULL
)
921 context_push_service( context
, producer
);
924 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
926 // Propogate the properties
927 qualify_property( context
, properties
, "resource" );
928 qualify_property( context
, properties
, "luma" );
929 qualify_property( context
, properties
, "luma.resource" );
930 qualify_property( context
, properties
, "composite.luma" );
931 qualify_property( context
, properties
, "producer.resource" );
932 mlt_properties_inherit( mlt_service_properties( service
), properties
);
933 mlt_properties_close( properties
);
934 context
->producer_properties
= NULL
;
935 properties
= mlt_service_properties( service
);
937 // Set in and out again due to inheritance
938 mlt_transition_set_in_and_out( MLT_TRANSITION( service
),
939 mlt_properties_get_position( properties
, "in" ),
940 mlt_properties_get_position( properties
, "out" ) );
942 // Connect the transition to the producer
943 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
944 mlt_properties_get_int( properties
, "a_track" ),
945 mlt_properties_get_int( properties
, "b_track" ) );
947 // Push the transition onto the stack
948 context_push_service( context
, service
);
950 if ( tractor
!= NULL
)
952 // Connect the tractor to the transition
953 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
955 // Push the tractor back onto the stack
956 context_push_service( context
, tractor
);
960 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
962 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
963 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
965 //printf("on_start_element: %s\n", name );
966 context
->branch
[ context
->depth
] ++;
969 // Build a tree from nodes within a property value
970 if ( context
->is_value
== 1 )
972 xmlNodePtr node
= xmlNewNode( NULL
, name
);
974 if ( context
->value_doc
== NULL
)
977 context
->value_doc
= xmlNewDoc( "1.0" );
978 xmlDocSetRootElement( context
->value_doc
, node
);
982 // Append child to tree
983 xmlAddChild( context
->stack_node
[ context
->stack_node_size
- 1 ], node
);
985 context_push_node( context
, node
);
987 // Set the attributes
988 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
989 xmlSetProp( node
, atts
[ 0 ], atts
[ 1 ] );
991 else if ( strcmp( name
, "tractor" ) == 0 )
992 on_start_tractor( context
, name
, atts
);
993 else if ( strcmp( name
, "multitrack" ) == 0 )
994 on_start_multitrack( context
, name
, atts
);
995 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
996 on_start_playlist( context
, name
, atts
);
997 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
998 on_start_producer( context
, name
, atts
);
999 else if ( strcmp( name
, "blank" ) == 0 )
1000 on_start_blank( context
, name
, atts
);
1001 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
1002 on_start_entry_track( context
, name
, atts
);
1003 else if ( strcmp( name
, "filter" ) == 0 )
1004 on_start_filter( context
, name
, atts
);
1005 else if ( strcmp( name
, "transition" ) == 0 )
1006 on_start_transition( context
, name
, atts
);
1007 else if ( strcmp( name
, "property" ) == 0 )
1008 on_start_property( context
, name
, atts
);
1011 static void on_end_element( void *ctx
, const xmlChar
*name
)
1013 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1014 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1016 //printf("on_end_element: %s\n", name );
1017 if ( context
->is_value
== 1 && strcmp( name
, "property" ) != 0 )
1018 context_pop_node( context
);
1019 else if ( strcmp( name
, "multitrack" ) == 0 )
1020 on_end_multitrack( context
, name
);
1021 else if ( strcmp( name
, "playlist" ) == 0 || strcmp( name
, "seq" ) == 0 || strcmp( name
, "smil" ) == 0 )
1022 on_end_playlist( context
, name
);
1023 else if ( strcmp( name
, "track" ) == 0 )
1024 on_end_track( context
, name
);
1025 else if ( strcmp( name
, "entry" ) == 0 )
1026 on_end_entry( context
, name
);
1027 else if ( strcmp( name
, "tractor" ) == 0 )
1028 on_end_tractor( context
, name
);
1029 else if ( strcmp( name
, "property" ) == 0 )
1030 on_end_property( context
, name
);
1031 else if ( strcmp( name
, "producer" ) == 0 || strcmp( name
, "video" ) == 0 )
1032 on_end_producer( context
, name
);
1033 else if ( strcmp( name
, "filter" ) == 0 )
1034 on_end_filter( context
, name
);
1035 else if ( strcmp( name
, "transition" ) == 0 )
1036 on_end_transition( context
, name
);
1038 context
->branch
[ context
->depth
] = 0;
1042 static void on_characters( void *ctx
, const xmlChar
*ch
, int len
)
1044 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1045 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1046 char *value
= calloc( len
+ 1, 1 );
1049 strncpy( value
, (const char*) ch
, len
);
1051 if ( context
->stack_node_size
> 0 )
1052 xmlNodeAddContent( context
->stack_node
[ context
->stack_node_size
- 1 ], ( xmlChar
* )value
);
1054 // libxml2 generates an on_characters immediately after a get_entity within
1055 // an element value, and we ignore it because it is called again during
1056 // actual substitution.
1057 else if ( context
->property
!= NULL
&& context
->producer_properties
!= NULL
1058 && context
->entity_is_replace
== 0 )
1060 char *s
= mlt_properties_get( context
->producer_properties
, context
->property
);
1063 // Append new text to existing content
1064 char *new = calloc( strlen( s
) + len
+ 1, 1 );
1066 strcat( new, value
);
1067 mlt_properties_set( context
->producer_properties
, context
->property
, new );
1071 mlt_properties_set( context
->producer_properties
, context
->property
, value
);
1073 context
->entity_is_replace
= 0;
1078 /** Convert parameters parsed from resource into entity declarations.
1080 static void params_to_entities( deserialise_context context
)
1082 if ( context
->params
!= NULL
)
1086 // Add our params as entitiy declarations
1087 for ( i
= 0; i
< mlt_properties_count( context
->params
); i
++ )
1089 xmlChar
*name
= ( xmlChar
* )mlt_properties_get_name( context
->params
, i
);
1090 xmlAddDocEntity( context
->entity_doc
, name
, XML_INTERNAL_GENERAL_ENTITY
,
1091 context
->publicId
, context
->systemId
, ( xmlChar
* )mlt_properties_get( context
->params
, name
) );
1095 mlt_properties_close( context
->params
);
1096 context
->params
= NULL
;
1100 // The following 3 facilitate entity substitution in the SAX parser
1101 static void on_internal_subset( void *ctx
, const xmlChar
* name
,
1102 const xmlChar
* publicId
, const xmlChar
* systemId
)
1104 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1105 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1107 context
->publicId
= publicId
;
1108 context
->systemId
= systemId
;
1109 xmlCreateIntSubset( context
->entity_doc
, name
, publicId
, systemId
);
1111 // Override default entities with our parameters
1112 params_to_entities( context
);
1115 static void on_entity_declaration( void *ctx
, const xmlChar
* name
, int type
,
1116 const xmlChar
* publicId
, const xmlChar
* systemId
, xmlChar
* content
)
1118 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1119 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1121 xmlAddDocEntity( context
->entity_doc
, name
, type
, publicId
, systemId
, content
);
1124 xmlEntityPtr
on_get_entity( void *ctx
, const xmlChar
* name
)
1126 struct _xmlParserCtxt
*xmlcontext
= ( struct _xmlParserCtxt
* )ctx
;
1127 deserialise_context context
= ( deserialise_context
)( xmlcontext
->_private
);
1128 xmlEntityPtr e
= NULL
;
1130 // Setup for entity declarations if not ready
1131 if ( xmlGetIntSubset( context
->entity_doc
) == NULL
)
1133 xmlCreateIntSubset( context
->entity_doc
, "westley", "", "" );
1134 context
->publicId
= "";
1135 context
->systemId
= "";
1138 // Add our parameters if not already
1139 params_to_entities( context
);
1141 e
= xmlGetDocEntity( context
->entity_doc
, name
);
1143 // Send signal to on_characters that an entity substitutin is pending
1145 context
->entity_is_replace
= 1;
1150 /** Convert a hexadecimal character to its value.
1152 static int tohex( char p
)
1154 return isdigit( p
) ? p
- '0' : tolower( p
) - 'a' + 10;
1157 /** Decode a url-encoded string containing hexadecimal character sequences.
1159 static char *url_decode( char *dest
, char *src
)
1167 *p
++ = ( tohex( *( src
+ 1 ) ) << 4 ) | tohex( *( src
+ 2 ) );
1180 /** Extract the filename from a URL attaching parameters to a properties list.
1182 static void parse_url( mlt_properties properties
, char *url
)
1185 int n
= strlen( url
);
1189 for ( i
= 0; i
< n
; i
++ )
1206 if ( name
!= NULL
&& value
!= NULL
)
1207 mlt_properties_set( properties
, name
, value
);
1213 if ( name
!= NULL
&& value
!= NULL
)
1214 mlt_properties_set( properties
, name
, value
);
1217 mlt_producer
producer_westley_init( char *url
)
1221 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
1222 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
1223 mlt_properties properties
= NULL
;
1225 struct _xmlParserCtxt
*xmlcontext
;
1226 int well_formed
= 0;
1227 char *filename
= strdup( url
);
1229 context
->producer_map
= mlt_properties_new();
1230 context
->destructors
= mlt_properties_new();
1231 context
->params
= mlt_properties_new();
1232 context
->filter_queue
= mlt_deque_init();
1234 // Decode URL and parse parameters
1235 parse_url( context
->params
, url_decode( filename
, url
) );
1237 // We need to track the number of registered filters
1238 mlt_properties_set_int( context
->destructors
, "registered", 0 );
1240 // We need the directory prefix which was used for the westley
1241 mlt_properties_set( context
->producer_map
, "_root", "" );
1242 if ( strchr( filename
, '/' ) )
1245 mlt_properties_set( context
->producer_map
, "_root", filename
);
1246 root
= mlt_properties_get( context
->producer_map
, "_root" );
1247 *( strrchr( root
, '/' ) + 1 ) = '\0';
1250 // Setup SAX callbacks
1251 sax
->startElement
= on_start_element
;
1252 sax
->endElement
= on_end_element
;
1253 sax
->characters
= on_characters
;
1254 sax
->cdataBlock
= on_characters
;
1255 sax
->internalSubset
= on_internal_subset
;
1256 sax
->entityDecl
= on_entity_declaration
;
1257 sax
->getEntity
= on_get_entity
;
1259 // Setup libxml2 SAX parsing
1261 xmlSubstituteEntitiesDefault( 1 );
1262 // This is used to facilitate entity substitution in the SAX parser
1263 context
->entity_doc
= xmlNewDoc( "1.0" );
1264 xmlcontext
= xmlCreateFileParserCtxt( filename
);
1265 xmlcontext
->sax
= sax
;
1266 xmlcontext
->_private
= ( void* )context
;
1269 xmlParseDocument( xmlcontext
);
1270 well_formed
= xmlcontext
->wellFormed
;
1272 // Cleanup after parsing
1273 xmlFreeDoc( context
->entity_doc
);
1275 xmlcontext
->sax
= NULL
;
1276 xmlcontext
->_private
= NULL
;
1277 xmlFreeParserCtxt( xmlcontext
);
1278 xmlMemoryDump( ); // for debugging
1280 // Get the last producer on the stack
1281 mlt_service service
= context_pop_service( context
);
1282 if ( well_formed
&& service
!= NULL
)
1284 // Verify it is a producer service (mlt_type="mlt_producer")
1285 // (producer, playlist, multitrack)
1286 char *type
= mlt_properties_get( mlt_service_properties( service
), "mlt_type" );
1287 if ( type
== NULL
|| ( strcmp( type
, "mlt_producer" ) != 0 && strcmp( type
, "producer" ) != 0 ) )
1292 xmlDocPtr doc
= westley_make_doc( service
);
1293 xmlDocFormatDump( stdout
, doc
, 1 );
1298 if ( well_formed
&& service
!= NULL
)
1301 // Need the complete producer list for various reasons
1302 properties
= context
->destructors
;
1304 // Now make sure we don't have a reference to the service in the properties
1305 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
1307 char *name
= mlt_properties_get_name( properties
, i
);
1308 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
1310 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
1315 // We are done referencing destructor property list
1316 // Set this var to service properties for convenience
1317 properties
= mlt_service_properties( service
);
1319 // make the returned service destroy the connected services
1320 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
1322 // Now assign additional properties
1323 mlt_properties_set( properties
, "resource", url
);
1325 // This tells consumer_westley not to deep copy
1326 mlt_properties_set( properties
, "westley", "was here" );
1330 // Return null if not well formed
1334 mlt_properties_close( context
->destructors
);
1338 mlt_properties_close( context
->producer_map
);
1339 if ( context
->params
!= NULL
)
1340 mlt_properties_close( context
->params
);
1341 mlt_deque_close( context
->filter_queue
);
1345 return MLT_PRODUCER( service
);