enhance miracle LOAD command to accept a service: prefix.
[melted] / src / modules / westley / producer_westley.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 // TODO: destroy unreferenced producers (they are currently destroyed
22 // when the returned producer is closed).
23
24 #include "producer_westley.h"
25 #include <framework/mlt.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
34
35 #define STACK_SIZE 1000
36 #define BRANCH_SIG_LEN 4000
37
38 #undef DEBUG
39 #ifdef DEBUG
40 extern xmlDocPtr westley_make_doc( mlt_service service );
41 #endif
42
43 struct deserialise_context_s
44 {
45 mlt_service stack_service[ STACK_SIZE ];
46 int stack_service_size;
47 mlt_properties producer_map;
48 mlt_properties destructors;
49 char *property;
50 mlt_properties producer_properties;
51 int is_value;
52 xmlDocPtr value_doc;
53 xmlNodePtr stack_node[ STACK_SIZE ];
54 int stack_node_size;
55 xmlDocPtr entity_doc;
56 int depth;
57 int branch[ STACK_SIZE ];
58 const xmlChar *publicId;
59 const xmlChar *systemId;
60 mlt_properties params;
61 };
62 typedef struct deserialise_context_s *deserialise_context;
63
64 /** Convert the numerical current branch address to a dot-delimited string.
65 */
66 static char *serialise_branch( deserialise_context this, char *s )
67 {
68 int i;
69
70 s[0] = 0;
71 for ( i = 0; i < this->depth; i++ )
72 {
73 int len = strlen( s );
74 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", this->branch[ i ] );
75 }
76 return s;
77 }
78
79 /** Push a service.
80 */
81
82 static int context_push_service( deserialise_context this, mlt_service that )
83 {
84 int ret = this->stack_service_size >= STACK_SIZE - 1;
85 if ( ret == 0 )
86 {
87 this->stack_service[ this->stack_service_size++ ] = that;
88
89 // Record the tree branch on which this service lives
90 if ( mlt_properties_get( mlt_service_properties( that ), "_westley_branch" ) == NULL )
91 {
92 char s[ BRANCH_SIG_LEN ];
93 mlt_properties_set( mlt_service_properties( that ), "_westley_branch", serialise_branch( this, s ) );
94 }
95 }
96 return ret;
97 }
98
99 /** Pop a service.
100 */
101
102 static mlt_service context_pop_service( deserialise_context this )
103 {
104 mlt_service result = NULL;
105 if ( this->stack_service_size > 0 )
106 result = this->stack_service[ -- this->stack_service_size ];
107 return result;
108 }
109
110 /** Push a node.
111 */
112
113 static int context_push_node( deserialise_context this, xmlNodePtr node )
114 {
115 int ret = this->stack_node_size >= STACK_SIZE - 1;
116 if ( ret == 0 )
117 this->stack_node[ this->stack_node_size ++ ] = node;
118 return ret;
119 }
120
121 /** Pop a node.
122 */
123
124 static xmlNodePtr context_pop_node( deserialise_context this )
125 {
126 xmlNodePtr result = NULL;
127 if ( this->stack_node_size > 0 )
128 result = this->stack_node[ -- this->stack_node_size ];
129 return result;
130 }
131
132
133 // Set the destructor on a new service
134 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
135 {
136 int registered = mlt_properties_get_int( properties, "registered" );
137 char *key = mlt_properties_get( properties, "registered" );
138 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
139 mlt_properties_set_int( properties, "registered", ++ registered );
140 }
141
142
143 // Forward declarations
144 static void on_end_track( deserialise_context context, const xmlChar *name );
145 static void on_end_entry( deserialise_context context, const xmlChar *name );
146
147 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
148 {
149 mlt_service service = mlt_tractor_service( mlt_tractor_init() );
150 mlt_properties properties = mlt_service_properties( service );
151
152 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
153
154 mlt_properties_set_position( properties, "length", 0 );
155
156 for ( ; atts != NULL && *atts != NULL; atts += 2 )
157 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
158
159 if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
160 {
161 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
162 mlt_properties_set_position( properties, "length", length );
163 }
164
165 if ( mlt_properties_get( properties, "id" ) != NULL )
166 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
167
168 context_push_service( context, service );
169 }
170
171 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
172 {
173 mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
174 mlt_properties properties = mlt_service_properties( service );
175
176 track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
177
178 mlt_properties_set_position( properties, "length", 0 );
179
180 for ( ; atts != NULL && *atts != NULL; atts += 2 )
181 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
182
183 if ( mlt_properties_get( properties, "id" ) != NULL )
184 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
185
186 context_push_service( context, service );
187 }
188
189 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
190 {
191 mlt_service service = mlt_playlist_service( mlt_playlist_init() );
192 mlt_properties properties = mlt_service_properties( service );
193
194 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
195
196 mlt_properties_set_position( properties, "length", 0 );
197
198 for ( ; atts != NULL && *atts != NULL; atts += 2 )
199 {
200 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
201
202 // Out will be overwritten later as we append, so we need to save it
203 if ( strcmp( atts[ 0 ], "out" ) == 0 )
204 mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
205 }
206
207 if ( mlt_properties_get( properties, "id" ) != NULL )
208 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
209
210 context_push_service( context, service );
211 }
212
213 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
214 {
215 mlt_properties properties = context->producer_properties = mlt_properties_new();
216
217 for ( ; atts != NULL && *atts != NULL; atts += 2 )
218 {
219 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
220 }
221 }
222
223 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
224 {
225 // Get the playlist from the stack
226 mlt_service service = context_pop_service( context );
227 mlt_position length = 0;
228
229 if ( service == NULL )
230 return;
231
232 // Look for the length attribute
233 for ( ; atts != NULL && *atts != NULL; atts += 2 )
234 {
235 if ( strcmp( atts[0], "length" ) == 0 )
236 {
237 length = atoll( atts[1] );
238 break;
239 }
240 }
241
242 // Append a blank to the playlist
243 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
244
245 // Push the playlist back onto the stack
246 context_push_service( context, service );
247 }
248
249 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
250 {
251 // Use a dummy service to hold properties to allow arbitrary nesting
252 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
253 mlt_service_init( service, NULL );
254
255 // Push the dummy service onto the stack
256 context_push_service( context, service );
257
258 if ( strcmp( name, "entry" ) == 0 )
259 mlt_properties_set( mlt_service_properties( service ), "resource", "<entry>" );
260 else
261 mlt_properties_set( mlt_service_properties( service ), "resource", "<track>" );
262
263 for ( ; atts != NULL && *atts != NULL; atts += 2 )
264 {
265 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
266
267 // Look for the producer attribute
268 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
269 {
270 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
271 // Push the referenced producer onto the stack
272 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
273 else
274 // Remove the dummy service to cause end element failure
275 context_pop_service( context );
276 }
277 }
278 }
279
280 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
281 {
282 mlt_properties properties = context->producer_properties = mlt_properties_new();
283
284 // Set the properties
285 for ( ; atts != NULL && *atts != NULL; atts += 2 )
286 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
287 }
288
289 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
290 {
291 mlt_properties properties = context->producer_properties = mlt_properties_new();
292
293 // Set the properties
294 for ( ; atts != NULL && *atts != NULL; atts += 2 )
295 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
296 }
297
298 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
299 {
300 mlt_properties properties = context->producer_properties;
301 char *value = NULL;
302
303 if ( properties == NULL )
304 return;
305
306 // Set the properties
307 for ( ; atts != NULL && *atts != NULL; atts += 2 )
308 {
309 if ( strcmp( atts[ 0 ], "name" ) == 0 )
310 {
311 context->property = strdup( atts[ 1 ] );
312 }
313 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
314 {
315 value = (char*) atts[ 1 ];
316 }
317 }
318
319 if ( context->property != NULL && value != NULL )
320 mlt_properties_set( properties, context->property, value );
321
322 // Tell parser to collect any further nodes for serialisation
323 context->is_value = 1;
324 }
325
326
327 /** This function adds a producer to a playlist or multitrack when
328 there is no entry or track element.
329 */
330
331 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
332 {
333 // Get the parent producer
334 mlt_service producer = context_pop_service( context );
335
336 if ( producer != NULL )
337 {
338 char current_branch[ BRANCH_SIG_LEN ];
339 char *service_branch = mlt_properties_get( mlt_service_properties( producer ), "_westley_branch" );
340
341 // Validate the producer from the stack is an ancestor and not predecessor
342 serialise_branch( context, current_branch );
343 if ( service_branch != NULL && strncmp( service_branch, current_branch, strlen( service_branch ) ) == 0 )
344 {
345 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
346
347 // Put the parent producer back
348 context_push_service( context, producer );
349
350 // If the parent producer is a multitrack or playlist (not a track or entry)
351 if ( resource && ( strcmp( resource, "<playlist>" ) == 0 ||
352 strcmp( resource, "<multitrack>" ) == 0 ) )
353 {
354 //printf( "add_producer: current branch %s service branch %s (%d)\n", current_branch, service_branch, strncmp( service_branch, current_branch, strlen( service_branch ) ) );
355 if ( strcmp( resource, "<playlist>" ) == 0 )
356 {
357 // Append this producer to the playlist
358 mlt_playlist_append_io( MLT_PLAYLIST( producer ),
359 MLT_PRODUCER( service ), in, out );
360 }
361 else
362 {
363 mlt_properties properties = mlt_service_properties( service );
364
365 // Set this producer on the multitrack
366 mlt_multitrack_connect( MLT_MULTITRACK( producer ),
367 MLT_PRODUCER( service ),
368 mlt_multitrack_count( MLT_MULTITRACK( producer ) ) );
369
370 // Set the hide state of the track producer
371 char *hide_s = mlt_properties_get( properties, "hide" );
372 if ( hide_s != NULL )
373 {
374 if ( strcmp( hide_s, "video" ) == 0 )
375 mlt_properties_set_int( properties, "hide", 1 );
376 else if ( strcmp( hide_s, "audio" ) == 0 )
377 mlt_properties_set_int( properties, "hide", 2 );
378 else if ( strcmp( hide_s, "both" ) == 0 )
379 mlt_properties_set_int( properties, "hide", 3 );
380 }
381
382 }
383 // Normally, the enclosing entry or track will pop this service off
384 // In its absence we do not push it on.
385 return 1;
386 }
387 }
388 }
389 return 0;
390 }
391
392 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
393 {
394 // Get the multitrack from the stack
395 mlt_service producer = context_pop_service( context );
396 if ( producer == NULL )
397 return;
398
399 // Get the tractor from the stack
400 mlt_service service = context_pop_service( context );
401
402 // Create a tractor if one does not exist
403 char *resource = NULL;
404 if ( service != NULL )
405 resource = mlt_properties_get( mlt_service_properties( service ), "resource" );
406 if ( service == NULL || resource == NULL || strcmp( resource, "<tractor>" ) )
407 {
408 //printf("creating a tractor\n");
409 char current_branch[ BRANCH_SIG_LEN ];
410
411 // Put the anonymous service back onto the stack!
412 if ( service != NULL )
413 context_push_service( context, service );
414
415 // Fabricate the tractor
416 service = mlt_tractor_service( mlt_tractor_init() );
417 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
418
419 // Inherit the producer's properties
420 mlt_properties properties = mlt_service_properties( service );
421 mlt_properties_set_position( properties, "length", mlt_producer_get_out( MLT_PRODUCER( producer ) ) + 1 );
422 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
423 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
424
425 mlt_properties_set( properties, "_westley_branch", serialise_branch( context, current_branch ) );
426 }
427
428 // Connect the tractor to the multitrack
429 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
430 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
431 MLT_MULTITRACK( producer ), 0, NULL, NULL );
432
433 // See if the tractor should be added to a playlist or multitrack
434 add_producer( context, service, 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
435
436 // Always push the multitrack back onto the stack for filters and transitions
437 context_push_service( context, producer );
438
439 // Always push the tractor back onto the stack for filters and transitions
440 context_push_service( context, service );
441 }
442
443 static void on_end_playlist( deserialise_context context, const xmlChar *name )
444 {
445 // Get the playlist from the stack
446 mlt_service producer = context_pop_service( context );
447 if ( producer == NULL )
448 return;
449 mlt_properties properties = mlt_service_properties( producer );
450
451 mlt_position in = mlt_properties_get_position( properties, "in" );
452 mlt_position out;
453
454 if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
455 out = mlt_properties_get_position( properties, "_westley.out" );
456 else
457 out = mlt_properties_get_position( properties, "length" ) - 1;
458
459 if ( mlt_properties_get_position( properties, "length" ) < out )
460 mlt_properties_set_position( properties, "length", out + 1 );
461
462 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
463
464 // See if the playlist should be added to a playlist or multitrack
465 if ( add_producer( context, producer, in, out ) == 0 )
466
467 // Otherwise, push the playlist back onto the stack
468 context_push_service( context, producer );
469 }
470
471 static void on_end_track( deserialise_context context, const xmlChar *name )
472 {
473 // Get the producer from the stack
474 mlt_service producer = context_pop_service( context );
475 if ( producer == NULL )
476 return;
477 mlt_properties producer_props = mlt_service_properties( producer );
478
479 // See if the producer is a tractor
480 char *resource = mlt_properties_get( producer_props, "resource" );
481 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
482 // If so chomp its producer
483 context_pop_service( context );
484
485 // Get the dummy track service from the stack
486 mlt_service track = context_pop_service( context );
487 if ( track == NULL || strcmp( mlt_properties_get( mlt_service_properties( track ), "resource" ), "<track>" ) )
488 {
489 context_push_service( context, producer );
490 return;
491 }
492 mlt_properties track_props = mlt_service_properties( track );
493
494 // Get the multitrack from the stack
495 mlt_service service = context_pop_service( context );
496 if ( service == NULL )
497 {
498 context_push_service( context, producer );
499 return;
500 }
501
502 // Set the track on the multitrack
503 mlt_multitrack_connect( MLT_MULTITRACK( service ),
504 MLT_PRODUCER( producer ),
505 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
506
507 // Set producer i/o if specified
508 if ( mlt_properties_get( track_props, "in" ) != NULL ||
509 mlt_properties_get( track_props, "out" ) != NULL )
510 {
511 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
512 mlt_properties_get_position( track_props, "in" ),
513 mlt_properties_get_position( track_props, "out" ) );
514 }
515
516 // Set the hide state of the track producer
517 char *hide_s = mlt_properties_get( track_props, "hide" );
518 if ( hide_s != NULL )
519 {
520 if ( strcmp( hide_s, "video" ) == 0 )
521 mlt_properties_set_int( producer_props, "hide", 1 );
522 else if ( strcmp( hide_s, "audio" ) == 0 )
523 mlt_properties_set_int( producer_props, "hide", 2 );
524 else if ( strcmp( hide_s, "both" ) == 0 )
525 mlt_properties_set_int( producer_props, "hide", 3 );
526 }
527
528 // Push the multitrack back onto the stack
529 context_push_service( context, service );
530
531 mlt_service_close( track );
532 }
533
534 static void on_end_entry( deserialise_context context, const xmlChar *name )
535 {
536 // Get the producer from the stack
537 mlt_service producer = context_pop_service( context );
538 if ( producer == NULL )
539 return;
540
541 // See if the producer is a tractor
542 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
543 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
544 // If so chomp its producer
545 context_pop_service( context );
546
547 // Get the dummy entry service from the stack
548 mlt_service entry = context_pop_service( context );
549 if ( entry == NULL || strcmp( mlt_properties_get( mlt_service_properties( entry ), "resource" ), "<entry>" ) )
550 {
551 context_push_service( context, producer );
552 return;
553 }
554
555 // Get the playlist from the stack
556 mlt_service service = context_pop_service( context );
557 if ( service == NULL )
558 {
559 context_push_service( context, producer );
560 return;
561 }
562
563 // Append the producer to the playlist
564 if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
565 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
566 {
567 mlt_playlist_append_io( MLT_PLAYLIST( service ),
568 MLT_PRODUCER( producer ),
569 mlt_properties_get_position( mlt_service_properties( entry ), "in" ),
570 mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
571 }
572 else
573 {
574 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
575 }
576
577 // Push the playlist back onto the stack
578 context_push_service( context, service );
579
580 mlt_service_close( entry );
581 }
582
583 static void on_end_tractor( deserialise_context context, const xmlChar *name )
584 {
585 // Get the tractor
586 mlt_service tractor = context_pop_service( context );
587 if ( tractor == NULL )
588 return;
589
590 // Get the tractor's multitrack
591 mlt_producer multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
592 if ( multitrack != NULL )
593 {
594 // Inherit the producer's properties
595 mlt_properties properties = mlt_producer_properties( MLT_PRODUCER( tractor ) );
596 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
597 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
598 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
599 }
600
601 // See if the tractor should be added to a playlist or multitrack
602 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
603
604 // Otherwise, push the tractor back onto the stack
605 context_push_service( context, tractor );
606 }
607
608 static void on_end_property( deserialise_context context, const xmlChar *name )
609 {
610 // Tell parser to stop building a tree
611 context->is_value = 0;
612
613 // See if there is a xml tree for the value
614 if ( context->property != NULL && context->value_doc != NULL )
615 {
616 xmlChar *value;
617 int size;
618
619 // Serialise the tree to get value
620 xmlDocDumpMemory( context->value_doc, &value, &size );
621 mlt_properties_set( context->producer_properties, context->property, value );
622 xmlFree( value );
623 xmlFreeDoc( context->value_doc );
624 context->value_doc = NULL;
625 }
626
627 // Close this property handling
628 free( context->property );
629 context->property = NULL;
630 }
631
632 static void on_end_producer( deserialise_context context, const xmlChar *name )
633 {
634 mlt_properties properties = context->producer_properties;
635 mlt_service service = NULL;
636
637 if ( properties == NULL )
638 return;
639
640 char *resource = mlt_properties_get( properties, "resource" );
641 // Let Kino-SMIL src be a synonym for resource
642 if ( resource == NULL )
643 resource = mlt_properties_get( properties, "src" );
644
645 // Instantiate the producer
646 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
647 {
648 char temp[ 1024 ];
649 strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
650 if ( resource != NULL )
651 {
652 strcat( temp, ":" );
653 strncat( temp, resource, 1023 - strlen( temp ) );
654 }
655 service = MLT_SERVICE( mlt_factory_producer( "fezzik", temp ) );
656 }
657 if ( service == NULL && resource != NULL )
658 {
659 char *root = mlt_properties_get( context->producer_map, "_root" );
660 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
661 if ( resource[ 0 ] != '/' )
662 {
663 strcpy( full_resource, root );
664 strcat( full_resource, resource );
665 }
666 else
667 {
668 strcpy( full_resource, resource );
669 }
670 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
671 free( full_resource );
672 }
673 if ( service == NULL )
674 return;
675 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
676
677 // Add the producer to the producer map
678 if ( mlt_properties_get( properties, "id" ) != NULL )
679 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
680
681 // Handle in/out properties separately
682 mlt_position in = -1;
683 mlt_position out = -1;
684
685 // Get in
686 if ( mlt_properties_get( properties, "in" ) != NULL )
687 in = mlt_properties_get_position( properties, "in" );
688 // Let Kino-SMIL clipBegin be a synonym for in
689 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
690 in = mlt_properties_get_position( properties, "clipBegin" );
691 // Get out
692 if ( mlt_properties_get( properties, "out" ) != NULL )
693 out = mlt_properties_get_position( properties, "out" );
694 // Let Kino-SMIL clipEnd be a synonym for out
695 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
696 out = mlt_properties_get_position( properties, "clipEnd" );
697
698 // Remove in and out
699 mlt_properties_set( properties, "in", NULL );
700 mlt_properties_set( properties, "out", NULL );
701
702 mlt_properties_inherit( mlt_service_properties( service ), properties );
703 mlt_properties_close( properties );
704 context->producer_properties = NULL;
705
706 // See if the producer should be added to a playlist or multitrack
707 if ( add_producer( context, service, in, out ) == 0 )
708 {
709 // Otherwise, set in and out on...
710 if ( in != -1 || out != -1 )
711 {
712 // Get the parent service
713 mlt_service parent = context_pop_service( context );
714 if ( parent != NULL )
715 {
716 // Get the parent properties
717 properties = mlt_service_properties( parent );
718
719 char *resource = mlt_properties_get( properties, "resource" );
720
721 // Put the parent producer back
722 context_push_service( context, parent );
723
724 // If the parent is a track or entry
725 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
726 {
727 mlt_properties_set_position( properties, "in", in );
728 mlt_properties_set_position( properties, "out", out );
729 }
730 else
731 {
732 // Otherwise, set in and out on producer directly
733 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
734 }
735 }
736 else
737 {
738 // Otherwise, set in and out on producer directly
739 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
740 }
741 }
742
743 // Push the producer onto the stack
744 context_push_service( context, service );
745 }
746 }
747
748 static void on_end_filter( deserialise_context context, const xmlChar *name )
749 {
750 mlt_properties properties = context->producer_properties;
751 if ( properties == NULL )
752 return;
753
754 char *id;
755 char key[11];
756 key[ 10 ] = '\0';
757 mlt_service tractor = NULL;
758
759 // Get the producer from the stack
760 mlt_service producer = context_pop_service( context );
761 if ( producer == NULL )
762 return;
763
764 // See if the producer is a tractor
765 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
766 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
767 {
768 // If so, then get the next producer
769 tractor = producer;
770 producer = context_pop_service( context );
771 }
772
773 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
774
775 // Create the filter
776 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
777 if ( service == NULL )
778 {
779 context_push_service( context, producer );
780 return;
781 }
782 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
783
784 // Connect the filter to the producer
785 mlt_filter_connect( MLT_FILTER( service ), producer,
786 mlt_properties_get_int( properties, "track" ) );
787
788 // Set in and out from producer if non existant
789 if ( mlt_properties_get( properties, "in" ) == NULL )
790 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
791 if ( mlt_properties_get( properties, "out" ) == NULL )
792 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
793
794 // Propogate the properties
795 mlt_properties_inherit( mlt_service_properties( service ), properties );
796 mlt_properties_close( properties );
797 context->producer_properties = NULL;
798 properties = mlt_service_properties( service );
799
800 // Set in and out again due to inheritance
801 mlt_filter_set_in_and_out( MLT_FILTER( service ),
802 mlt_properties_get_position( properties, "in" ),
803 mlt_properties_get_position( properties, "out" ) );
804
805 // If a producer alias is in the producer_map, get it
806 snprintf( key, 10, "%p", producer );
807 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
808 producer = mlt_properties_get_data( context->producer_map, key, NULL );
809
810 // Put the producer in the producer map
811 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
812 if ( id != NULL )
813 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
814
815 // For filter chain support, add an alias to the producer map
816 snprintf( key, 10, "%p", service );
817 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
818
819 // Push the filter onto the stack
820 context_push_service( context, service );
821
822 if ( tractor != NULL )
823 {
824 // Connect the tractor to the filter
825 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
826
827 // Push the tractor back onto the stack
828 context_push_service( context, tractor );
829 }
830 }
831
832 static void on_end_transition( deserialise_context context, const xmlChar *name )
833 {
834 mlt_service tractor = NULL;
835 mlt_properties properties = context->producer_properties;
836 if ( properties == NULL )
837 return;
838
839 // Get the producer from the stack
840 mlt_service producer = context_pop_service( context );
841 if ( producer == NULL )
842 return;
843
844 // See if the producer is a tractor
845 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
846 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
847 {
848 // If so, then get the next producer
849 tractor = producer;
850 producer = context_pop_service( context );
851 }
852
853 // Create the transition
854 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
855 if ( service == NULL )
856 {
857 context_push_service( context, producer );
858 return;
859 }
860 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
861
862 // Propogate the properties
863 mlt_properties_inherit( mlt_service_properties( service ), properties );
864 mlt_properties_close( properties );
865 context->producer_properties = NULL;
866 properties = mlt_service_properties( service );
867
868 // Set in and out again due to inheritance
869 mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
870 mlt_properties_get_position( properties, "in" ),
871 mlt_properties_get_position( properties, "out" ) );
872
873 // Connect the transition to the producer
874 mlt_transition_connect( MLT_TRANSITION( service ), producer,
875 mlt_properties_get_int( properties, "a_track" ),
876 mlt_properties_get_int( properties, "b_track" ) );
877
878 // Push the transition onto the stack
879 context_push_service( context, service );
880
881 if ( tractor != NULL )
882 {
883 // Connect the tractor to the transition
884 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
885
886 // Push the tractor back onto the stack
887 context_push_service( context, tractor );
888 }
889 }
890
891 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
892 {
893 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
894 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
895
896 //printf("on_start_element: %s\n", name );
897 context->branch[ context->depth ] ++;
898 context->depth ++;
899
900 // Build a tree from nodes within a property value
901 if ( context->is_value == 1 )
902 {
903 xmlNodePtr node = xmlNewNode( NULL, name );
904
905 if ( context->value_doc == NULL )
906 {
907 // Start a new tree
908 context->value_doc = xmlNewDoc( "1.0" );
909 xmlDocSetRootElement( context->value_doc, node );
910 }
911 else
912 {
913 // Append child to tree
914 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
915 }
916 context_push_node( context, node );
917
918 // Set the attributes
919 for ( ; atts != NULL && *atts != NULL; atts += 2 )
920 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
921 }
922 else if ( strcmp( name, "tractor" ) == 0 )
923 on_start_tractor( context, name, atts );
924 else if ( strcmp( name, "multitrack" ) == 0 )
925 on_start_multitrack( context, name, atts );
926 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
927 on_start_playlist( context, name, atts );
928 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
929 on_start_producer( context, name, atts );
930 else if ( strcmp( name, "blank" ) == 0 )
931 on_start_blank( context, name, atts );
932 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
933 on_start_entry_track( context, name, atts );
934 else if ( strcmp( name, "filter" ) == 0 )
935 on_start_filter( context, name, atts );
936 else if ( strcmp( name, "transition" ) == 0 )
937 on_start_transition( context, name, atts );
938 else if ( strcmp( name, "property" ) == 0 )
939 on_start_property( context, name, atts );
940 }
941
942 static void on_end_element( void *ctx, const xmlChar *name )
943 {
944 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
945 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
946
947 //printf("on_end_element: %s\n", name );
948 if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
949 context_pop_node( context );
950 else if ( strcmp( name, "multitrack" ) == 0 )
951 on_end_multitrack( context, name );
952 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
953 on_end_playlist( context, name );
954 else if ( strcmp( name, "track" ) == 0 )
955 on_end_track( context, name );
956 else if ( strcmp( name, "entry" ) == 0 )
957 on_end_entry( context, name );
958 else if ( strcmp( name, "tractor" ) == 0 )
959 on_end_tractor( context, name );
960 else if ( strcmp( name, "property" ) == 0 )
961 on_end_property( context, name );
962 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
963 on_end_producer( context, name );
964 else if ( strcmp( name, "filter" ) == 0 )
965 on_end_filter( context, name );
966 else if ( strcmp( name, "transition" ) == 0 )
967 on_end_transition( context, name );
968
969 context->branch[ context->depth ] = 0;
970 context->depth --;
971 }
972
973 static void on_characters( void *ctx, const xmlChar *ch, int len )
974 {
975 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
976 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
977 char *value = calloc( len + 1, 1 );
978
979 value[ len ] = 0;
980 strncpy( value, (const char*) ch, len );
981
982 if ( context->stack_node_size > 0 )
983 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
984
985 else if ( context->property != NULL && context->producer_properties != NULL )
986 mlt_properties_set( context->producer_properties, context->property, value );
987
988 free( value);
989 }
990
991 /** Convert parameters parsed from resource into entity declarations.
992 */
993 static void params_to_entities( deserialise_context context )
994 {
995 if ( context->params != NULL )
996 {
997 int i;
998
999 // Add our params as entitiy declarations
1000 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1001 {
1002 xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1003 xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1004 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, name ) );
1005 }
1006
1007 // Flag completion
1008 mlt_properties_close( context->params );
1009 context->params = NULL;
1010 }
1011 }
1012
1013 // The following 3 facilitate entity substitution in the SAX parser
1014 static void on_internal_subset( void *ctx, const xmlChar* name,
1015 const xmlChar* publicId, const xmlChar* systemId )
1016 {
1017 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1018 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1019
1020 context->publicId = publicId;
1021 context->systemId = systemId;
1022 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1023
1024 // Override default entities with our parameters
1025 params_to_entities( context );
1026 }
1027
1028 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
1029 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1030 {
1031 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1032 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1033
1034 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1035 }
1036
1037 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1038 {
1039 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1040 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1041
1042 // Setup for entity declarations if not ready
1043 if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1044 {
1045 xmlCreateIntSubset( context->entity_doc, "westley", "", "" );
1046 context->publicId = "";
1047 context->systemId = "";
1048 }
1049
1050 // Add our parameters if not already
1051 params_to_entities( context );
1052
1053 return xmlGetDocEntity( context->entity_doc, name );
1054 }
1055
1056 /** Convert a hexadecimal character to its value.
1057 */
1058 static int tohex( char p )
1059 {
1060 return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1061 }
1062
1063 /** Decode a url-encoded string containing hexadecimal character sequences.
1064 */
1065 static char *url_decode( char *dest, char *src )
1066 {
1067 char *p = dest;
1068
1069 while ( *src )
1070 {
1071 if ( *src == '%' )
1072 {
1073 *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1074 src += 3;
1075 }
1076 else
1077 {
1078 *p ++ = *src ++;
1079 }
1080 }
1081
1082 *p = *src;
1083 return dest;
1084 }
1085
1086 /** Extract the filename from a URL attaching parameters to a properties list.
1087 */
1088 static void parse_url( mlt_properties properties, char *url )
1089 {
1090 int i;
1091 int n = strlen( url );
1092 char *name = NULL;
1093 char *value = NULL;
1094
1095 for ( i = 0; i < n; i++ )
1096 {
1097 switch ( url[ i ] )
1098 {
1099 case '?':
1100 url[ i++ ] = '\0';
1101 name = &url[ i ];
1102 break;
1103
1104 case ':':
1105 case '=':
1106 url[ i++ ] = '\0';
1107 value = &url[ i ];
1108 break;
1109
1110 case '&':
1111 url[ i++ ] = '\0';
1112 if ( name != NULL && value != NULL )
1113 mlt_properties_set( properties, name, value );
1114 name = &url[ i ];
1115 value = NULL;
1116 break;
1117 }
1118 }
1119 if ( name != NULL && value != NULL )
1120 mlt_properties_set( properties, name, value );
1121 }
1122
1123 mlt_producer producer_westley_init( char *url )
1124 {
1125 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1126 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1127 mlt_properties properties = NULL;
1128 int i = 0;
1129 struct _xmlParserCtxt *xmlcontext;
1130 int well_formed = 0;
1131 char *filename = strdup( url );
1132
1133 context->producer_map = mlt_properties_new();
1134 context->destructors = mlt_properties_new();
1135 context->params = mlt_properties_new();
1136
1137 // Decode URL and parse parameters
1138 parse_url( context->params, url_decode( filename, url ) );
1139
1140 // We need to track the number of registered filters
1141 mlt_properties_set_int( context->destructors, "registered", 0 );
1142
1143 // We need the directory prefix which was used for the westley
1144 mlt_properties_set( context->producer_map, "_root", "" );
1145 if ( strchr( filename, '/' ) )
1146 {
1147 char *root = NULL;
1148 mlt_properties_set( context->producer_map, "_root", filename );
1149 root = mlt_properties_get( context->producer_map, "_root" );
1150 *( strrchr( root, '/' ) + 1 ) = '\0';
1151 }
1152
1153 // Setup SAX callbacks
1154 sax->startElement = on_start_element;
1155 sax->endElement = on_end_element;
1156 sax->characters = on_characters;
1157 sax->cdataBlock = on_characters;
1158 sax->internalSubset = on_internal_subset;
1159 sax->entityDecl = on_entity_declaration;
1160 sax->getEntity = on_get_entity;
1161
1162 // Setup libxml2 SAX parsing
1163 xmlInitParser();
1164 xmlSubstituteEntitiesDefault( 1 );
1165 // This is used to facilitate entity substitution in the SAX parser
1166 context->entity_doc = xmlNewDoc( "1.0" );
1167 xmlcontext = xmlCreateFileParserCtxt( filename );
1168 xmlcontext->sax = sax;
1169 xmlcontext->_private = ( void* )context;
1170
1171 // Parse
1172 xmlParseDocument( xmlcontext );
1173 well_formed = xmlcontext->wellFormed;
1174
1175 // Cleanup after parsing
1176 xmlFreeDoc( context->entity_doc );
1177 free( sax );
1178 xmlcontext->sax = NULL;
1179 xmlcontext->_private = NULL;
1180 xmlFreeParserCtxt( xmlcontext );
1181 xmlMemoryDump( ); // for debugging
1182
1183 // Get the last producer on the stack
1184 mlt_service service = context_pop_service( context );
1185 if ( well_formed && service != NULL )
1186 {
1187 // Verify it is a producer service (mlt_type="mlt_producer")
1188 // (producer, playlist, multitrack)
1189 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
1190 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1191 service = NULL;
1192 }
1193
1194 #ifdef DEBUG
1195 xmlDocPtr doc = westley_make_doc( service );
1196 xmlDocFormatDump( stdout, doc, 1 );
1197 xmlFreeDoc( doc );
1198 service = NULL;
1199 #endif
1200
1201 if ( well_formed && service != NULL )
1202 {
1203
1204 // Need the complete producer list for various reasons
1205 properties = context->destructors;
1206
1207 // Now make sure we don't have a reference to the service in the properties
1208 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1209 {
1210 char *name = mlt_properties_get_name( properties, i );
1211 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1212 {
1213 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1214 break;
1215 }
1216 }
1217
1218 // We are done referencing destructor property list
1219 // Set this var to service properties for convenience
1220 properties = mlt_service_properties( service );
1221
1222 // make the returned service destroy the connected services
1223 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1224
1225 // Now assign additional properties
1226 mlt_properties_set( properties, "resource", url );
1227
1228 // This tells consumer_westley not to deep copy
1229 mlt_properties_set( properties, "westley", "was here" );
1230 }
1231 else
1232 {
1233 // Return null if not well formed
1234 service = NULL;
1235
1236 // Clean up
1237 mlt_properties_close( context->destructors );
1238 }
1239
1240 // Clean up
1241 mlt_properties_close( context->producer_map );
1242 if ( context->params != NULL )
1243 mlt_properties_close( context->params );
1244 free( context );
1245 free( filename );
1246
1247 return MLT_PRODUCER( service );
1248 }