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