reorganized consumer_westley.
[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 service = MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties, "mlt_service" ) ) );
646 }
647 if ( service == NULL && resource != NULL )
648 {
649 char *root = mlt_properties_get( context->producer_map, "_root" );
650 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
651 if ( resource[ 0 ] != '/' )
652 {
653 strcpy( full_resource, root );
654 strcat( full_resource, resource );
655 }
656 else
657 {
658 strcpy( full_resource, resource );
659 }
660 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
661 free( full_resource );
662 }
663 if ( service == NULL )
664 return;
665 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
666
667 // Add the producer to the producer map
668 if ( mlt_properties_get( properties, "id" ) != NULL )
669 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
670
671 // Handle in/out properties separately
672 mlt_position in = -1;
673 mlt_position out = -1;
674
675 // Get in
676 if ( mlt_properties_get( properties, "in" ) != NULL )
677 in = mlt_properties_get_position( properties, "in" );
678 // Let Kino-SMIL clipBegin be a synonym for in
679 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
680 in = mlt_properties_get_position( properties, "clipBegin" );
681 // Get out
682 if ( mlt_properties_get( properties, "out" ) != NULL )
683 out = mlt_properties_get_position( properties, "out" );
684 // Let Kino-SMIL clipEnd be a synonym for out
685 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
686 out = mlt_properties_get_position( properties, "clipEnd" );
687
688 // Remove in and out
689 mlt_properties_set( properties, "in", NULL );
690 mlt_properties_set( properties, "out", NULL );
691
692 mlt_properties_inherit( mlt_service_properties( service ), properties );
693 mlt_properties_close( properties );
694 context->producer_properties = NULL;
695
696 // See if the producer should be added to a playlist or multitrack
697 if ( add_producer( context, service, in, out ) == 0 )
698 {
699 // Otherwise, set in and out on...
700 if ( in != -1 || out != -1 )
701 {
702 // Get the parent service
703 mlt_service parent = context_pop_service( context );
704 if ( parent != NULL )
705 {
706 // Get the parent properties
707 properties = mlt_service_properties( parent );
708
709 char *resource = mlt_properties_get( properties, "resource" );
710
711 // Put the parent producer back
712 context_push_service( context, parent );
713
714 // If the parent is a track or entry
715 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
716 {
717 mlt_properties_set_position( properties, "in", in );
718 mlt_properties_set_position( properties, "out", out );
719 }
720 else
721 {
722 // Otherwise, set in and out on producer directly
723 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
724 }
725 }
726 else
727 {
728 // Otherwise, set in and out on producer directly
729 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
730 }
731 }
732
733 // Push the producer onto the stack
734 context_push_service( context, service );
735 }
736 }
737
738 static void on_end_filter( deserialise_context context, const xmlChar *name )
739 {
740 mlt_properties properties = context->producer_properties;
741 if ( properties == NULL )
742 return;
743
744 char *id;
745 char key[11];
746 key[ 10 ] = '\0';
747 mlt_service tractor = NULL;
748
749 // Get the producer from the stack
750 mlt_service producer = context_pop_service( context );
751 if ( producer == NULL )
752 return;
753
754 // See if the producer is a tractor
755 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
756 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
757 {
758 // If so, then get the next producer
759 tractor = producer;
760 producer = context_pop_service( context );
761 }
762
763 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
764
765 // Create the filter
766 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
767 if ( service == NULL )
768 {
769 context_push_service( context, producer );
770 return;
771 }
772 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
773
774 // Connect the filter to the producer
775 mlt_filter_connect( MLT_FILTER( service ), producer,
776 mlt_properties_get_int( properties, "track" ) );
777
778 // Set in and out from producer if non existant
779 if ( mlt_properties_get( properties, "in" ) == NULL )
780 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
781 if ( mlt_properties_get( properties, "out" ) == NULL )
782 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
783
784 // Propogate the properties
785 mlt_properties_inherit( mlt_service_properties( service ), properties );
786 mlt_properties_close( properties );
787 context->producer_properties = NULL;
788 properties = mlt_service_properties( service );
789
790 // Set in and out again due to inheritance
791 mlt_filter_set_in_and_out( MLT_FILTER( service ),
792 mlt_properties_get_position( properties, "in" ),
793 mlt_properties_get_position( properties, "out" ) );
794
795 // If a producer alias is in the producer_map, get it
796 snprintf( key, 10, "%p", producer );
797 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
798 producer = mlt_properties_get_data( context->producer_map, key, NULL );
799
800 // Put the producer in the producer map
801 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
802 if ( id != NULL )
803 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
804
805 // For filter chain support, add an alias to the producer map
806 snprintf( key, 10, "%p", service );
807 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
808
809 // Push the filter onto the stack
810 context_push_service( context, service );
811
812 if ( tractor != NULL )
813 {
814 // Connect the tractor to the filter
815 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
816
817 // Push the tractor back onto the stack
818 context_push_service( context, tractor );
819 }
820 }
821
822 static void on_end_transition( deserialise_context context, const xmlChar *name )
823 {
824 mlt_service tractor = NULL;
825 mlt_properties properties = context->producer_properties;
826 if ( properties == NULL )
827 return;
828
829 // Get the producer from the stack
830 mlt_service producer = context_pop_service( context );
831 if ( producer == NULL )
832 return;
833
834 // See if the producer is a tractor
835 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
836 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
837 {
838 // If so, then get the next producer
839 tractor = producer;
840 producer = context_pop_service( context );
841 }
842
843 // Create the transition
844 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
845 if ( service == NULL )
846 {
847 context_push_service( context, producer );
848 return;
849 }
850 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
851
852 // Propogate the properties
853 mlt_properties_inherit( mlt_service_properties( service ), properties );
854 mlt_properties_close( properties );
855 context->producer_properties = NULL;
856 properties = mlt_service_properties( service );
857
858 // Set in and out again due to inheritance
859 mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
860 mlt_properties_get_position( properties, "in" ),
861 mlt_properties_get_position( properties, "out" ) );
862
863 // Connect the transition to the producer
864 mlt_transition_connect( MLT_TRANSITION( service ), producer,
865 mlt_properties_get_int( properties, "a_track" ),
866 mlt_properties_get_int( properties, "b_track" ) );
867
868 // Push the transition onto the stack
869 context_push_service( context, service );
870
871 if ( tractor != NULL )
872 {
873 // Connect the tractor to the transition
874 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
875
876 // Push the tractor back onto the stack
877 context_push_service( context, tractor );
878 }
879 }
880
881 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
882 {
883 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
884 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
885
886 //printf("on_start_element: %s\n", name );
887 context->branch[ context->depth ] ++;
888 context->depth ++;
889
890 // Build a tree from nodes within a property value
891 if ( context->is_value == 1 )
892 {
893 xmlNodePtr node = xmlNewNode( NULL, name );
894
895 if ( context->value_doc == NULL )
896 {
897 // Start a new tree
898 context->value_doc = xmlNewDoc( "1.0" );
899 xmlDocSetRootElement( context->value_doc, node );
900 }
901 else
902 {
903 // Append child to tree
904 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
905 }
906 context_push_node( context, node );
907
908 // Set the attributes
909 for ( ; atts != NULL && *atts != NULL; atts += 2 )
910 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
911 }
912 else if ( strcmp( name, "tractor" ) == 0 )
913 on_start_tractor( context, name, atts );
914 else if ( strcmp( name, "multitrack" ) == 0 )
915 on_start_multitrack( context, name, atts );
916 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
917 on_start_playlist( context, name, atts );
918 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
919 on_start_producer( context, name, atts );
920 else if ( strcmp( name, "blank" ) == 0 )
921 on_start_blank( context, name, atts );
922 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
923 on_start_entry_track( context, name, atts );
924 else if ( strcmp( name, "filter" ) == 0 )
925 on_start_filter( context, name, atts );
926 else if ( strcmp( name, "transition" ) == 0 )
927 on_start_transition( context, name, atts );
928 else if ( strcmp( name, "property" ) == 0 )
929 on_start_property( context, name, atts );
930 }
931
932 static void on_end_element( void *ctx, const xmlChar *name )
933 {
934 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
935 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
936
937 //printf("on_end_element: %s\n", name );
938 if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
939 context_pop_node( context );
940 else if ( strcmp( name, "multitrack" ) == 0 )
941 on_end_multitrack( context, name );
942 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
943 on_end_playlist( context, name );
944 else if ( strcmp( name, "track" ) == 0 )
945 on_end_track( context, name );
946 else if ( strcmp( name, "entry" ) == 0 )
947 on_end_entry( context, name );
948 else if ( strcmp( name, "tractor" ) == 0 )
949 on_end_tractor( context, name );
950 else if ( strcmp( name, "property" ) == 0 )
951 on_end_property( context, name );
952 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
953 on_end_producer( context, name );
954 else if ( strcmp( name, "filter" ) == 0 )
955 on_end_filter( context, name );
956 else if ( strcmp( name, "transition" ) == 0 )
957 on_end_transition( context, name );
958
959 context->branch[ context->depth ] = 0;
960 context->depth --;
961 }
962
963 static void on_characters( void *ctx, const xmlChar *ch, int len )
964 {
965 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
966 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
967 char *value = calloc( len + 1, 1 );
968
969 value[ len ] = 0;
970 strncpy( value, (const char*) ch, len );
971
972 if ( context->stack_node_size > 0 )
973 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
974
975 else if ( context->property != NULL && context->producer_properties != NULL )
976 mlt_properties_set( context->producer_properties, context->property, value );
977
978 free( value);
979 }
980
981 // The following 3 facilitate entity substitution in the SAX parser
982 static void on_internal_subset( void *ctx, const xmlChar* name,
983 const xmlChar* publicId, const xmlChar* systemId )
984 {
985 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
986 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
987
988 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
989 }
990
991 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
992 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
993 {
994 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
995 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
996
997 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
998 }
999
1000 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1001 {
1002 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1003 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1004
1005 return xmlGetDocEntity( context->entity_doc, name );
1006 }
1007
1008
1009 mlt_producer producer_westley_init( char *filename )
1010 {
1011 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1012 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1013 mlt_properties properties = NULL;
1014 int i = 0;
1015 struct _xmlParserCtxt *xmlcontext;
1016 int well_formed = 0;
1017
1018 context->producer_map = mlt_properties_new();
1019 context->destructors = mlt_properties_new();
1020
1021 // We need to track the number of registered filters
1022 mlt_properties_set_int( context->destructors, "registered", 0 );
1023
1024 // We need the directory prefix which was used for the westley
1025 mlt_properties_set( context->producer_map, "_root", "" );
1026 if ( strchr( filename, '/' ) )
1027 {
1028 char *root = NULL;
1029 mlt_properties_set( context->producer_map, "_root", filename );
1030 root = mlt_properties_get( context->producer_map, "_root" );
1031 *( strrchr( root, '/' ) + 1 ) = '\0';
1032 }
1033
1034 // Setup SAX callbacks
1035 sax->startElement = on_start_element;
1036 sax->endElement = on_end_element;
1037 sax->characters = on_characters;
1038 sax->cdataBlock = on_characters;
1039 sax->internalSubset = on_internal_subset;
1040 sax->entityDecl = on_entity_declaration;
1041 sax->getEntity = on_get_entity;
1042
1043 // Setup libxml2 SAX parsing
1044 xmlInitParser();
1045 xmlSubstituteEntitiesDefault( 1 );
1046 // This is used to facilitate entity substitution in the SAX parser
1047 context->entity_doc = xmlNewDoc( "1.0" );
1048 xmlcontext = xmlCreateFileParserCtxt( filename );
1049 xmlcontext->sax = sax;
1050 xmlcontext->_private = ( void* )context;
1051
1052 // Parse
1053 xmlParseDocument( xmlcontext );
1054 well_formed = xmlcontext->wellFormed;
1055
1056 // Cleanup after parsing
1057 xmlFreeDoc( context->entity_doc );
1058 free( sax );
1059 xmlcontext->sax = NULL;
1060 xmlcontext->_private = NULL;
1061 xmlFreeParserCtxt( xmlcontext );
1062 xmlMemoryDump( ); // for debugging
1063
1064 // Get the last producer on the stack
1065 mlt_service service = context_pop_service( context );
1066 if ( well_formed && service != NULL )
1067 {
1068 // Verify it is a producer service (mlt_type="mlt_producer")
1069 // (producer, playlist, multitrack)
1070 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
1071 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1072 service = NULL;
1073 }
1074
1075 #ifdef DEBUG
1076 xmlDocPtr doc = westley_make_doc( service );
1077 xmlDocFormatDump( stdout, doc, 1 );
1078 xmlFreeDoc( doc );
1079 service = NULL;
1080 #endif
1081
1082 if ( well_formed && service != NULL )
1083 {
1084
1085 // Need the complete producer list for various reasons
1086 properties = context->destructors;
1087
1088 // Now make sure we don't have a reference to the service in the properties
1089 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1090 {
1091 char *name = mlt_properties_get_name( properties, i );
1092 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1093 {
1094 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1095 break;
1096 }
1097 }
1098
1099 // We are done referencing destructor property list
1100 // Set this var to service properties for convenience
1101 properties = mlt_service_properties( service );
1102
1103 // make the returned service destroy the connected services
1104 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1105
1106 // Now assign additional properties
1107 mlt_properties_set( properties, "resource", filename );
1108
1109 // This tells consumer_westley not to deep copy
1110 mlt_properties_set( properties, "westley", "was here" );
1111 }
1112 else
1113 {
1114 // Return null if not well formed
1115 service = NULL;
1116
1117 // Clean up
1118 mlt_properties_close( context->destructors );
1119 }
1120
1121 free( context->stack_service );
1122 mlt_properties_close( context->producer_map );
1123 //free( context );
1124
1125 return MLT_PRODUCER( service );
1126 }